Allow a patch range to be specified for 'series'
[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
34patches are prefixed with a '0'."""
fcee87cf 35
2f7c8b0b 36options = [make_option('-b', '--branch',
2f206b8f 37 help = 'use BRANCH instead of the default one'),
19ac8fe4
CM
38 make_option('-m', '--missing', metavar = 'BRANCH',
39 help = 'show patches in BRANCH missing in current'),
948dae34
CL
40 make_option('-c', '--count',
41 help = 'print the number of patches in the series',
42 action = 'store_true'),
f82e85bc 43 make_option('-d', '--description',
d33c40f1 44 help = 'show a short description for each patch',
f82e85bc 45 action = 'store_true'),
2f206b8f 46 make_option('-e', '--empty',
9f00453e
PBG
47 help = 'check whether patches are empty '
48 '(much slower)',
47d6ad47
CL
49 action = 'store_true'),
50 make_option('-s', '--short',
51 help = 'list just the patches around the topmost patch',
43e97a20
CM
52 action = 'store_true'),
53 make_option('-g', '--graphical',
54 help = 'run gitk instead of printing',
55 action = 'store_true')]
fcee87cf
CM
56
57
f82e85bc
CL
58def __get_description(patch):
59 """Extract and return a patch's short description
60 """
61 p = crt_series.get_patch(patch)
d2a327a8 62 descr = (p.get_description() or '').strip()
f82e85bc
CL
63 descr_lines = descr.split('\n')
64 return descr_lines[0].rstrip()
65
66def __print_patch(patch, prefix, empty_prefix, length, options):
67 if options.empty and crt_series.empty_patch(patch):
68 prefix = empty_prefix
69 if options.description:
70 print prefix + patch.ljust(length) + ' | ' + __get_description(patch)
71 else:
72 print prefix + patch
73
fcee87cf
CM
74def func(parser, options, args):
75 """Show the patch series
76 """
19ac8fe4
CM
77 global crt_series
78
19ac8fe4
CM
79 if options.missing:
80 # switch the series, the one specified with --missing should
81 # become the current
82 cmp_series = crt_series
83 crt_series = stack.Series(options.missing)
84 stgit.commands.common.crt_series = crt_series
85
86 cmp_patches = cmp_series.get_applied() + cmp_series.get_unapplied()
87 else:
88 cmp_patches = []
89
0aebdb85
CM
90 applied = crt_series.get_applied()
91 unapplied = crt_series.get_unapplied()
47d6ad47 92
0aebdb85
CM
93 # the filtering range covers the whole series
94 if args:
95 show_patches = parse_patches(args, applied + unapplied, len(applied))
96 else:
97 show_patches = applied + unapplied
98
99 # filter the patches
100 applied = [p for p in applied
101 if p in show_patches and p not in cmp_patches]
102 unapplied = [p for p in unapplied
103 if p in show_patches and p not in cmp_patches]
948dae34 104
47d6ad47
CL
105 if options.short:
106 if len(applied) > 5:
107 applied = applied[-6:]
108 if len(unapplied) > 5:
109 unapplied = unapplied[:5]
110
f82e85bc 111 patches = applied + unapplied
0aebdb85
CM
112
113 if options.count:
114 print len(patches)
115 return
116
43e97a20
CM
117 if not patches:
118 return
119
120 if options.graphical:
19ac8fe4
CM
121 if options.missing:
122 raise CmdException, '--graphical not supported with --missing'
123
43e97a20
CM
124 if applied:
125 gitk_args = ' %s^..%s' % (git_id(applied[0]), git_id(applied[-1]))
126 else:
127 gitk_args = ''
f82e85bc 128
43e97a20
CM
129 for p in unapplied:
130 patch_id = git_id(p)
131 gitk_args += ' %s^..%s' % (patch_id, patch_id)
132
133 if os.system('gitk%s' % gitk_args) != 0:
134 raise CmdException, 'gitk execution failed'
135 else:
136 max_len = 0
137 if len(patches) > 0:
138 max_len = max([len(i) for i in patches])
f82e85bc 139
43e97a20
CM
140 if len(applied) > 0:
141 for p in applied [0:-1]:
142 __print_patch(p, '+ ', '0 ', max_len, options)
f82e85bc 143
43e97a20 144 __print_patch(applied[-1], '> ', '0>', max_len, options)
f82e85bc 145
43e97a20
CM
146 for p in unapplied:
147 __print_patch(p, '- ', '0 ', max_len, options)