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