Allow a patch range to be specified for 'series'
[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 '+' and the unapplied
33 ones with a '-'. The current patch is prefixed with a '>'. Empty
34 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('-m', '--missing', metavar = 'BRANCH',
39 help = 'show patches in BRANCH missing in current'),
40 make_option('-c', '--count',
41 help = 'print the number of patches in the series',
42 action = 'store_true'),
43 make_option('-d', '--description',
44 help = 'show a short description for each patch',
45 action = 'store_true'),
46 make_option('-e', '--empty',
47 help = 'check whether patches are empty '
48 '(much slower)',
49 action = 'store_true'),
50 make_option('-s', '--short',
51 help = 'list just the patches around the topmost patch',
52 action = 'store_true'),
53 make_option('-g', '--graphical',
54 help = 'run gitk instead of printing',
55 action = 'store_true')]
56
57
58 def __get_description(patch):
59 """Extract and return a patch's short description
60 """
61 p = crt_series.get_patch(patch)
62 descr = (p.get_description() or '').strip()
63 descr_lines = descr.split('\n')
64 return descr_lines[0].rstrip()
65
66 def __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
74 def func(parser, options, args):
75 """Show the patch series
76 """
77 global crt_series
78
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
90 applied = crt_series.get_applied()
91 unapplied = crt_series.get_unapplied()
92
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]
104
105 if options.short:
106 if len(applied) > 5:
107 applied = applied[-6:]
108 if len(unapplied) > 5:
109 unapplied = unapplied[:5]
110
111 patches = applied + unapplied
112
113 if options.count:
114 print len(patches)
115 return
116
117 if not patches:
118 return
119
120 if options.graphical:
121 if options.missing:
122 raise CmdException, '--graphical not supported with --missing'
123
124 if applied:
125 gitk_args = ' %s^..%s' % (git_id(applied[0]), git_id(applied[-1]))
126 else:
127 gitk_args = ''
128
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])
139
140 if len(applied) > 0:
141 for p in applied [0:-1]:
142 __print_patch(p, '+ ', '0 ', max_len, options)
143
144 __print_patch(applied[-1], '> ', '0>', max_len, options)
145
146 for p in unapplied:
147 __print_patch(p, '- ', '0 ', max_len, options)