Have series show empty hidden patches as hidden rather than empty.
[stgit] / stgit / commands / series.py
CommitLineData
fcee87cf
CM
1
2__copyright__ = """
3Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License version 2 as
7published by the Free Software Foundation.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17"""
18
19import sys, os
20from optparse import OptionParser, make_option
21
19ac8fe4 22import stgit.commands.common
fcee87cf
CM
23from stgit.commands.common import *
24from stgit.utils import *
25from stgit import stack, git
26
27
28help = 'print the patch series'
0aebdb85 29usage = """%prog [options] [<patch-range>]
26aab5b0 30
0aebdb85 31Show all the patches in the series or just those in the given
ca8b854c
CM
32range. The applied patches are prefixed with a '+', the unapplied ones
33with a '-' and the hidden ones with a '!'. The current patch is
34prefixed with a '>'. Empty patches are prefixed with a '0'."""
fcee87cf 35
2f7c8b0b 36options = [make_option('-b', '--branch',
2f206b8f 37 help = 'use BRANCH instead of the default one'),
841c7b2a
CM
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'),
19ac8fe4
CM
44 make_option('-m', '--missing', metavar = 'BRANCH',
45 help = 'show patches in BRANCH missing in current'),
948dae34
CL
46 make_option('-c', '--count',
47 help = 'print the number of patches in the series',
48 action = 'store_true'),
f82e85bc 49 make_option('-d', '--description',
d33c40f1 50 help = 'show a short description for each patch',
f82e85bc 51 action = 'store_true'),
54fac8e6
CM
52 make_option('--author',
53 help = 'show the author name for each patch',
54 action = 'store_true'),
2f206b8f 55 make_option('-e', '--empty',
9f00453e
PBG
56 help = 'check whether patches are empty '
57 '(much slower)',
47d6ad47 58 action = 'store_true'),
f483998b
CM
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'),
47d6ad47
CL
65 make_option('-s', '--short',
66 help = 'list just the patches around the topmost patch',
43e97a20
CM
67 action = 'store_true'),
68 make_option('-g', '--graphical',
69 help = 'run gitk instead of printing',
70 action = 'store_true')]
fcee87cf
CM
71
72
f82e85bc
CL
73def __get_description(patch):
74 """Extract and return a patch's short description
75 """
76 p = crt_series.get_patch(patch)
d2a327a8 77 descr = (p.get_description() or '').strip()
f82e85bc
CL
78 descr_lines = descr.split('\n')
79 return descr_lines[0].rstrip()
80
54fac8e6
CM
81def __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
ca8b854c 87def __print_patch(patch, branch_str, prefix, empty_prefix, length, options):
841c7b2a
CM
88 """Print a patch name, description and various markers.
89 """
f483998b
CM
90 if options.noprefix:
91 prefix = ''
92 elif options.empty and crt_series.empty_patch(patch):
f82e85bc 93 prefix = empty_prefix
f483998b
CM
94
95 patch_str = patch + branch_str
96
54fac8e6
CM
97 if options.description or options.author:
98 patch_str = patch_str.ljust(length)
99
f82e85bc 100 if options.description:
27ac2b7e 101 out.stdout(prefix + patch_str + ' | ' + __get_description(patch))
54fac8e6 102 elif options.author:
27ac2b7e 103 out.stdout(prefix + patch_str + ' | ' + __get_author(patch))
f82e85bc 104 else:
27ac2b7e 105 out.stdout(prefix + patch_str)
f82e85bc 106
fcee87cf
CM
107def func(parser, options, args):
108 """Show the patch series
109 """
19ac8fe4
CM
110 global crt_series
111
ca8b854c
CM
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
19ac8fe4
CM
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
ca8b854c
CM
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 = []
19ac8fe4
CM
146 else:
147 cmp_patches = []
148
0aebdb85
CM
149 # the filtering range covers the whole series
150 if args:
ca8b854c
CM
151 show_patches = parse_patches(args, applied + unapplied + hidden,
152 len(applied))
0aebdb85 153 else:
ca8b854c 154 show_patches = applied + unapplied + hidden
0aebdb85 155
841c7b2a
CM
156 # missing filtering
157 show_patches = [p for p in show_patches if p not in cmp_patches]
158
0aebdb85 159 # filter the patches
841c7b2a
CM
160 applied = [p for p in applied if p in show_patches]
161 unapplied = [p for p in unapplied if p in show_patches]
ca8b854c 162 hidden = [p for p in hidden if p in show_patches]
948dae34 163
47d6ad47 164 if options.short:
c73e63b7 165 nr = int(config.get('stgit.shortnr'))
c6f366f6
CM
166 if len(applied) > nr:
167 applied = applied[-(nr+1):]
ca8b854c
CM
168 n = len(unapplied)
169 if n > nr:
c6f366f6 170 unapplied = unapplied[:nr]
ca8b854c
CM
171 elif n < nr:
172 hidden = hidden[:nr-n]
47d6ad47 173
ca8b854c 174 patches = applied + unapplied + hidden
0aebdb85
CM
175
176 if options.count:
27ac2b7e 177 out.stdout(len(patches))
0aebdb85
CM
178 return
179
43e97a20
CM
180 if not patches:
181 return
182
f483998b
CM
183 if options.showbranch:
184 branch_str = '@' + crt_series.get_branch()
185 else:
186 branch_str = ''
187
43e97a20 188 if options.graphical:
19ac8fe4
CM
189 if options.missing:
190 raise CmdException, '--graphical not supported with --missing'
191
43e97a20
CM
192 if applied:
193 gitk_args = ' %s^..%s' % (git_id(applied[0]), git_id(applied[-1]))
194 else:
195 gitk_args = ''
f82e85bc 196
43e97a20
CM
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:
f483998b 206 max_len = max([len(i + branch_str) for i in patches])
841c7b2a 207
ca8b854c
CM
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)
f82e85bc 213
43e97a20 214 for p in unapplied:
ca8b854c
CM
215 __print_patch(p, branch_str, '- ', '0 ', max_len, options)
216
217 for p in hidden:
2270bc4d 218 __print_patch(p, branch_str, '! ', '! ', max_len, options)