Remove the applied/unapplied commands
[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 from optparse import make_option
20
21 from stgit.commands import common
22 from stgit.commands.common import parse_patches
23 from stgit.out import out
24
25 help = 'print the patch series'
26 usage = """%prog [options] [<patch-range>]
27
28 Show all the patches in the series or just those in the given
29 range. The applied patches are prefixed with a '+', the unapplied ones
30 with a '-' and the hidden ones with a '!'. The current patch is
31 prefixed with a '>'. Empty patches are prefixed with a '0'."""
32
33 directory = common.DirectoryHasRepositoryLib()
34
35 options = [make_option('-b', '--branch',
36 help = 'use BRANCH instead of the default one'),
37 make_option('-a', '--all',
38 help = 'show all patches, including the hidden ones',
39 action = 'store_true'),
40 make_option('-A', '--applied',
41 help = 'show the applied patches only',
42 action = 'store_true'),
43 make_option('-U', '--unapplied',
44 help = 'show the unapplied patches only',
45 action = 'store_true'),
46 make_option('-H', '--hidden',
47 help = 'show the hidden patches only',
48 action = 'store_true'),
49 make_option('-m', '--missing', metavar = 'BRANCH',
50 help = 'show patches in BRANCH missing in current'),
51 make_option('-c', '--count',
52 help = 'print the number of patches in the series',
53 action = 'store_true'),
54 make_option('-d', '--description',
55 help = 'show a short description for each patch',
56 action = 'store_true'),
57 make_option('--author',
58 help = 'show the author name for each patch',
59 action = 'store_true'),
60 make_option('-e', '--empty',
61 help = 'check whether patches are empty '
62 '(much slower)',
63 action = 'store_true'),
64 make_option('--showbranch',
65 help = 'append the branch name to the listed patches',
66 action = 'store_true'),
67 make_option('--noprefix',
68 help = 'do not show the patch status prefix',
69 action = 'store_true'),
70 make_option('-s', '--short',
71 help = 'list just the patches around the topmost patch',
72 action = 'store_true')]
73
74
75 def __get_description(stack, patch):
76 """Extract and return a patch's short description
77 """
78 cd = stack.patches.get(patch).commit.data
79 descr = cd.message.strip()
80 descr_lines = descr.split('\n')
81 return descr_lines[0].rstrip()
82
83 def __get_author(stack, patch):
84 """Extract and return a patch's short description
85 """
86 cd = stack.patches.get(patch).commit.data
87 return cd.author.name
88
89 def __print_patch(stack, patch, branch_str, prefix, empty_prefix, length, options):
90 """Print a patch name, description and various markers.
91 """
92 if options.noprefix:
93 prefix = ''
94 elif options.empty and stack.patches.get(patch).is_empty():
95 prefix = empty_prefix
96
97 patch_str = branch_str + patch
98
99 if options.description or options.author:
100 patch_str = patch_str.ljust(length)
101
102 if options.description:
103 out.stdout(prefix + patch_str + ' # ' + __get_description(stack, patch))
104 elif options.author:
105 out.stdout(prefix + patch_str + ' # ' + __get_author(stack, patch))
106 else:
107 out.stdout(prefix + patch_str)
108
109 def func(parser, options, args):
110 """Show the patch series
111 """
112 if options.all and options.short:
113 raise common.CmdException, 'combining --all and --short is meaningless'
114
115 stack = directory.repository.get_stack(options.branch)
116 if options.missing:
117 cmp_stack = stack
118 stack = directory.repository.get_stack(options.missing)
119
120 # current series patches
121 applied = unapplied = hidden = ()
122 if options.applied or options.unapplied or options.hidden:
123 if options.all:
124 raise common.CmdException, \
125 '--all cannot be used with --applied/unapplied/hidden'
126 if options.applied:
127 applied = stack.patchorder.applied
128 if options.unapplied:
129 unapplied = stack.patchorder.unapplied
130 if options.hidden:
131 hidden = stack.patchorder.hidden
132 elif options.all:
133 applied = stack.patchorder.applied
134 unapplied = stack.patchorder.unapplied
135 hidden = stack.patchorder.hidden
136 else:
137 applied = stack.patchorder.applied
138 unapplied = stack.patchorder.unapplied
139
140 if options.missing:
141 cmp_patches = cmp_stack.patchorder.all
142 else:
143 cmp_patches = ()
144
145 # the filtering range covers the whole series
146 if args:
147 show_patches = parse_patches(args, applied + unapplied + hidden,
148 len(applied))
149 else:
150 show_patches = applied + unapplied + hidden
151
152 # missing filtering
153 show_patches = [p for p in show_patches if p not in cmp_patches]
154
155 # filter the patches
156 applied = [p for p in applied if p in show_patches]
157 unapplied = [p for p in unapplied if p in show_patches]
158 hidden = [p for p in hidden if p in show_patches]
159
160 if options.short:
161 nr = int(config.get('stgit.shortnr'))
162 if len(applied) > nr:
163 applied = applied[-(nr+1):]
164 n = len(unapplied)
165 if n > nr:
166 unapplied = unapplied[:nr]
167 elif n < nr:
168 hidden = hidden[:nr-n]
169
170 patches = applied + unapplied + hidden
171
172 if options.count:
173 out.stdout(len(patches))
174 return
175
176 if not patches:
177 return
178
179 if options.showbranch:
180 branch_str = stack.name + ':'
181 else:
182 branch_str = ''
183
184 max_len = 0
185 if len(patches) > 0:
186 max_len = max([len(i + branch_str) for i in patches])
187
188 if applied:
189 for p in applied[:-1]:
190 __print_patch(stack, p, branch_str, '+ ', '0 ', max_len, options)
191 __print_patch(stack, applied[-1], branch_str, '> ', '0>', max_len,
192 options)
193
194 for p in unapplied:
195 __print_patch(stack, p, branch_str, '- ', '0 ', max_len, options)
196
197 for p in hidden:
198 __print_patch(stack, p, branch_str, '! ', '! ', max_len, options)