Have series show empty hidden patches as hidden rather than empty.
[stgit] / stgit / commands / series.py
1
2 __copyright__ = """
3 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """
18
19 import sys, os
20 from optparse import OptionParser, make_option
21
22 import stgit.commands.common
23 from stgit.commands.common import *
24 from stgit.utils import *
25 from stgit import stack, git
26
27
28 help = 'print the patch series'
29 usage = """%prog [options] [<patch-range>]
30
31 Show all the patches in the series or just those in the given
32 range. The applied patches are prefixed with a '+', the unapplied ones
33 with a '-' and the hidden ones with a '!'. The current patch is
34 prefixed with a '>'. Empty patches are prefixed with a '0'."""
35
36 options = [make_option('-b', '--branch',
37 help = 'use BRANCH instead of the default one'),
38 make_option('-a', '--all',
39 help = 'show all patches, including the hidden ones',
40 action = 'store_true'),
41 make_option('-i', '--invisible',
42 help = 'show the hidden patches only',
43 action = 'store_true'),
44 make_option('-m', '--missing', metavar = 'BRANCH',
45 help = 'show patches in BRANCH missing in current'),
46 make_option('-c', '--count',
47 help = 'print the number of patches in the series',
48 action = 'store_true'),
49 make_option('-d', '--description',
50 help = 'show a short description for each patch',
51 action = 'store_true'),
52 make_option('--author',
53 help = 'show the author name for each patch',
54 action = 'store_true'),
55 make_option('-e', '--empty',
56 help = 'check whether patches are empty '
57 '(much slower)',
58 action = 'store_true'),
59 make_option('--showbranch',
60 help = 'append the branch name to the listed patches',
61 action = 'store_true'),
62 make_option('--noprefix',
63 help = 'do not show the patch status prefix',
64 action = 'store_true'),
65 make_option('-s', '--short',
66 help = 'list just the patches around the topmost patch',
67 action = 'store_true'),
68 make_option('-g', '--graphical',
69 help = 'run gitk instead of printing',
70 action = 'store_true')]
71
72
73 def __get_description(patch):
74 """Extract and return a patch's short description
75 """
76 p = crt_series.get_patch(patch)
77 descr = (p.get_description() or '').strip()
78 descr_lines = descr.split('\n')
79 return descr_lines[0].rstrip()
80
81 def __get_author(patch):
82 """Extract and return a patch's short description
83 """
84 p = crt_series.get_patch(patch)
85 return p.get_authname();
86
87 def __print_patch(patch, branch_str, prefix, empty_prefix, length, options):
88 """Print a patch name, description and various markers.
89 """
90 if options.noprefix:
91 prefix = ''
92 elif options.empty and crt_series.empty_patch(patch):
93 prefix = empty_prefix
94
95 patch_str = patch + branch_str
96
97 if options.description or options.author:
98 patch_str = patch_str.ljust(length)
99
100 if options.description:
101 out.stdout(prefix + patch_str + ' | ' + __get_description(patch))
102 elif options.author:
103 out.stdout(prefix + patch_str + ' | ' + __get_author(patch))
104 else:
105 out.stdout(prefix + patch_str)
106
107 def func(parser, options, args):
108 """Show the patch series
109 """
110 global crt_series
111
112 # current series patches
113 if options.invisible:
114 applied = unapplied = []
115 hidden = crt_series.get_hidden()
116 elif options.all:
117 applied = crt_series.get_applied()
118 unapplied = crt_series.get_unapplied()
119 hidden = crt_series.get_hidden()
120 else:
121 applied = crt_series.get_applied()
122 unapplied = crt_series.get_unapplied()
123 hidden = []
124
125 if options.missing:
126 # switch the series, the one specified with --missing should
127 # become the current
128 cmp_series = crt_series
129 crt_series = stack.Series(options.missing)
130 stgit.commands.common.crt_series = crt_series
131
132 cmp_patches = applied + unapplied + hidden
133
134 # new current series patches
135 if options.invisible:
136 applied = unapplied = []
137 hidden = crt_series.get_hidden()
138 elif options.all:
139 applied = crt_series.get_applied()
140 unapplied = crt_series.get_unapplied()
141 hidden = crt_series.get_hidden()
142 else:
143 applied = crt_series.get_applied()
144 unapplied = crt_series.get_unapplied()
145 hidden = []
146 else:
147 cmp_patches = []
148
149 # the filtering range covers the whole series
150 if args:
151 show_patches = parse_patches(args, applied + unapplied + hidden,
152 len(applied))
153 else:
154 show_patches = applied + unapplied + hidden
155
156 # missing filtering
157 show_patches = [p for p in show_patches if p not in cmp_patches]
158
159 # filter the patches
160 applied = [p for p in applied if p in show_patches]
161 unapplied = [p for p in unapplied if p in show_patches]
162 hidden = [p for p in hidden if p in show_patches]
163
164 if options.short:
165 nr = int(config.get('stgit.shortnr'))
166 if len(applied) > nr:
167 applied = applied[-(nr+1):]
168 n = len(unapplied)
169 if n > nr:
170 unapplied = unapplied[:nr]
171 elif n < nr:
172 hidden = hidden[:nr-n]
173
174 patches = applied + unapplied + hidden
175
176 if options.count:
177 out.stdout(len(patches))
178 return
179
180 if not patches:
181 return
182
183 if options.showbranch:
184 branch_str = '@' + crt_series.get_branch()
185 else:
186 branch_str = ''
187
188 if options.graphical:
189 if options.missing:
190 raise CmdException, '--graphical not supported with --missing'
191
192 if applied:
193 gitk_args = ' %s^..%s' % (git_id(applied[0]), git_id(applied[-1]))
194 else:
195 gitk_args = ''
196
197 for p in unapplied:
198 patch_id = git_id(p)
199 gitk_args += ' %s^..%s' % (patch_id, patch_id)
200
201 if os.system('gitk%s' % gitk_args) != 0:
202 raise CmdException, 'gitk execution failed'
203 else:
204 max_len = 0
205 if len(patches) > 0:
206 max_len = max([len(i + branch_str) for i in patches])
207
208 if applied:
209 for p in applied[:-1]:
210 __print_patch(p, branch_str, '+ ', '0 ', max_len, options)
211 __print_patch(applied[-1], branch_str, '> ', '0>', max_len,
212 options)
213
214 for p in unapplied:
215 __print_patch(p, branch_str, '- ', '0 ', max_len, options)
216
217 for p in hidden:
218 __print_patch(p, branch_str, '! ', '! ', max_len, options)