Add support to hide and unhide patches
[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
CM
31Show all the patches in the series or just those in the given
32range. The applied patches are prefixed with a '+' and the unapplied
33ones with a '-'. The current patch is prefixed with a '>'. Empty
841c7b2a
CM
34patches are prefixed with a '0'. The '*' postfix is appended to hidden
35patches."""
fcee87cf 36
2f7c8b0b 37options = [make_option('-b', '--branch',
2f206b8f 38 help = 'use BRANCH instead of the default one'),
841c7b2a
CM
39 make_option('-a', '--all',
40 help = 'show all patches, including the hidden ones',
41 action = 'store_true'),
42 make_option('-i', '--invisible',
43 help = 'show the hidden patches only',
44 action = 'store_true'),
19ac8fe4
CM
45 make_option('-m', '--missing', metavar = 'BRANCH',
46 help = 'show patches in BRANCH missing in current'),
948dae34
CL
47 make_option('-c', '--count',
48 help = 'print the number of patches in the series',
49 action = 'store_true'),
f82e85bc 50 make_option('-d', '--description',
d33c40f1 51 help = 'show a short description for each patch',
f82e85bc 52 action = 'store_true'),
2f206b8f 53 make_option('-e', '--empty',
9f00453e
PBG
54 help = 'check whether patches are empty '
55 '(much slower)',
47d6ad47 56 action = 'store_true'),
f483998b
CM
57 make_option('--showbranch',
58 help = 'append the branch name to the listed patches',
59 action = 'store_true'),
60 make_option('--noprefix',
61 help = 'do not show the patch status prefix',
62 action = 'store_true'),
47d6ad47
CL
63 make_option('-s', '--short',
64 help = 'list just the patches around the topmost patch',
43e97a20
CM
65 action = 'store_true'),
66 make_option('-g', '--graphical',
67 help = 'run gitk instead of printing',
68 action = 'store_true')]
fcee87cf
CM
69
70
f82e85bc
CL
71def __get_description(patch):
72 """Extract and return a patch's short description
73 """
74 p = crt_series.get_patch(patch)
d2a327a8 75 descr = (p.get_description() or '').strip()
f82e85bc
CL
76 descr_lines = descr.split('\n')
77 return descr_lines[0].rstrip()
78
841c7b2a
CM
79def __print_patch(patch, hidden, branch_str, prefix, empty_prefix, length,
80 options):
81 """Print a patch name, description and various markers.
82 """
f483998b
CM
83 if options.noprefix:
84 prefix = ''
85 elif options.empty and crt_series.empty_patch(patch):
f82e85bc 86 prefix = empty_prefix
f483998b
CM
87
88 patch_str = patch + branch_str
841c7b2a
CM
89 if not options.noprefix and patch in hidden:
90 patch_str += '*'
f483998b 91
f82e85bc 92 if options.description:
841c7b2a 93 print prefix + patch_str.ljust(length) + ' | ' \
f483998b 94 + __get_description(patch)
f82e85bc 95 else:
f483998b 96 print prefix + patch_str
f82e85bc 97
fcee87cf
CM
98def func(parser, options, args):
99 """Show the patch series
100 """
19ac8fe4
CM
101 global crt_series
102
19ac8fe4
CM
103 if options.missing:
104 # switch the series, the one specified with --missing should
105 # become the current
106 cmp_series = crt_series
107 crt_series = stack.Series(options.missing)
108 stgit.commands.common.crt_series = crt_series
109
110 cmp_patches = cmp_series.get_applied() + cmp_series.get_unapplied()
111 else:
112 cmp_patches = []
113
0aebdb85
CM
114 applied = crt_series.get_applied()
115 unapplied = crt_series.get_unapplied()
47d6ad47 116
0aebdb85
CM
117 # the filtering range covers the whole series
118 if args:
119 show_patches = parse_patches(args, applied + unapplied, len(applied))
120 else:
121 show_patches = applied + unapplied
122
841c7b2a
CM
123 # missing filtering
124 show_patches = [p for p in show_patches if p not in cmp_patches]
125
126 # hidden patches filtering
127 hidden = crt_series.get_hidden()
128 if options.invisible:
129 show_patches = [p for p in show_patches if p in hidden]
130 elif not options.all:
131 show_patches = [p for p in show_patches if p not in hidden]
132
0aebdb85 133 # filter the patches
841c7b2a
CM
134 applied = [p for p in applied if p in show_patches]
135 unapplied = [p for p in unapplied if p in show_patches]
948dae34 136
47d6ad47
CL
137 if options.short:
138 if len(applied) > 5:
139 applied = applied[-6:]
140 if len(unapplied) > 5:
141 unapplied = unapplied[:5]
142
f82e85bc 143 patches = applied + unapplied
0aebdb85
CM
144
145 if options.count:
146 print len(patches)
147 return
148
43e97a20
CM
149 if not patches:
150 return
151
f483998b
CM
152 if options.showbranch:
153 branch_str = '@' + crt_series.get_branch()
154 else:
155 branch_str = ''
156
43e97a20 157 if options.graphical:
19ac8fe4
CM
158 if options.missing:
159 raise CmdException, '--graphical not supported with --missing'
160
43e97a20
CM
161 if applied:
162 gitk_args = ' %s^..%s' % (git_id(applied[0]), git_id(applied[-1]))
163 else:
164 gitk_args = ''
f82e85bc 165
43e97a20
CM
166 for p in unapplied:
167 patch_id = git_id(p)
168 gitk_args += ' %s^..%s' % (patch_id, patch_id)
169
170 if os.system('gitk%s' % gitk_args) != 0:
171 raise CmdException, 'gitk execution failed'
172 else:
173 max_len = 0
174 if len(patches) > 0:
f483998b 175 max_len = max([len(i + branch_str) for i in patches])
841c7b2a 176 max_len += 1
f82e85bc 177
43e97a20 178 if len(applied) > 0:
841c7b2a
CM
179 current = crt_series.get_current()
180 if applied[-1] == current:
181 del applied[-1]
182 else:
183 current = None
184
185 for p in applied:
186 __print_patch(p, hidden, branch_str, '+ ', '0 ', max_len,
187 options)
f82e85bc 188
841c7b2a
CM
189 if current:
190 __print_patch(current, hidden, branch_str, '> ', '0>', max_len,
191 options)
f82e85bc 192
43e97a20 193 for p in unapplied:
841c7b2a 194 __print_patch(p, hidden, branch_str, '- ', '0 ', max_len, options)