Add range comparison support to stg-mdiff.
[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
1f396835
YD
112 if options.all and options.short:
113 raise CmdException, 'combining --all and --short is meaningless'
114
ca8b854c
CM
115 # current series patches
116 if options.invisible:
117 applied = unapplied = []
118 hidden = crt_series.get_hidden()
119 elif options.all:
120 applied = crt_series.get_applied()
121 unapplied = crt_series.get_unapplied()
122 hidden = crt_series.get_hidden()
123 else:
124 applied = crt_series.get_applied()
125 unapplied = crt_series.get_unapplied()
126 hidden = []
127
19ac8fe4
CM
128 if options.missing:
129 # switch the series, the one specified with --missing should
130 # become the current
131 cmp_series = crt_series
132 crt_series = stack.Series(options.missing)
133 stgit.commands.common.crt_series = crt_series
134
ca8b854c
CM
135 cmp_patches = applied + unapplied + hidden
136
137 # new current series patches
138 if options.invisible:
139 applied = unapplied = []
140 hidden = crt_series.get_hidden()
141 elif options.all:
142 applied = crt_series.get_applied()
143 unapplied = crt_series.get_unapplied()
144 hidden = crt_series.get_hidden()
145 else:
146 applied = crt_series.get_applied()
147 unapplied = crt_series.get_unapplied()
148 hidden = []
19ac8fe4
CM
149 else:
150 cmp_patches = []
151
0aebdb85
CM
152 # the filtering range covers the whole series
153 if args:
ca8b854c
CM
154 show_patches = parse_patches(args, applied + unapplied + hidden,
155 len(applied))
0aebdb85 156 else:
ca8b854c 157 show_patches = applied + unapplied + hidden
0aebdb85 158
841c7b2a
CM
159 # missing filtering
160 show_patches = [p for p in show_patches if p not in cmp_patches]
161
0aebdb85 162 # filter the patches
841c7b2a
CM
163 applied = [p for p in applied if p in show_patches]
164 unapplied = [p for p in unapplied if p in show_patches]
ca8b854c 165 hidden = [p for p in hidden if p in show_patches]
948dae34 166
47d6ad47 167 if options.short:
c73e63b7 168 nr = int(config.get('stgit.shortnr'))
c6f366f6
CM
169 if len(applied) > nr:
170 applied = applied[-(nr+1):]
ca8b854c
CM
171 n = len(unapplied)
172 if n > nr:
c6f366f6 173 unapplied = unapplied[:nr]
ca8b854c
CM
174 elif n < nr:
175 hidden = hidden[:nr-n]
47d6ad47 176
ca8b854c 177 patches = applied + unapplied + hidden
0aebdb85
CM
178
179 if options.count:
27ac2b7e 180 out.stdout(len(patches))
0aebdb85
CM
181 return
182
43e97a20
CM
183 if not patches:
184 return
185
f483998b 186 if options.showbranch:
d37ff079 187 branch_str = '@' + crt_series.get_name()
f483998b
CM
188 else:
189 branch_str = ''
190
43e97a20 191 if options.graphical:
19ac8fe4
CM
192 if options.missing:
193 raise CmdException, '--graphical not supported with --missing'
194
43e97a20
CM
195 if applied:
196 gitk_args = ' %s^..%s' % (git_id(applied[0]), git_id(applied[-1]))
197 else:
198 gitk_args = ''
f82e85bc 199
43e97a20
CM
200 for p in unapplied:
201 patch_id = git_id(p)
202 gitk_args += ' %s^..%s' % (patch_id, patch_id)
203
204 if os.system('gitk%s' % gitk_args) != 0:
205 raise CmdException, 'gitk execution failed'
206 else:
207 max_len = 0
208 if len(patches) > 0:
f483998b 209 max_len = max([len(i + branch_str) for i in patches])
841c7b2a 210
ca8b854c
CM
211 if applied:
212 for p in applied[:-1]:
213 __print_patch(p, branch_str, '+ ', '0 ', max_len, options)
214 __print_patch(applied[-1], branch_str, '> ', '0>', max_len,
215 options)
f82e85bc 216
43e97a20 217 for p in unapplied:
ca8b854c
CM
218 __print_patch(p, branch_str, '- ', '0 ', max_len, options)
219
220 for p in hidden:
2270bc4d 221 __print_patch(p, branch_str, '! ', '! ', max_len, options)