3 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
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.
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.
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
19 from optparse import make_option
21 from stgit.commands import common
22 from stgit.commands.common import parse_patches
23 from stgit.out import out
24 from stgit.config import config
26 help = 'print the patch series'
27 usage = """%prog [options] [<patch-range>]
29 Show all the patches in the series or just those in the given
30 range. The applied patches are prefixed with a '+', the unapplied ones
31 with a '-' and the hidden ones with a '!'. The current patch is
32 prefixed with a '>'. Empty patches are prefixed with a '0'."""
34 directory = common.DirectoryHasRepositoryLib()
36 options = [make_option('-b', '--branch',
37 help = 'use BRANCH instead of the default one'),
38 make_option('-a', '--all',
39 help = 'show all patches, including the hidden ones',
40 action = 'store_true'),
41 make_option('-A', '--applied',
42 help = 'show the applied patches only',
43 action = 'store_true'),
44 make_option('-U', '--unapplied',
45 help = 'show the unapplied patches only',
46 action = 'store_true'),
47 make_option('-H', '--hidden',
48 help = 'show the hidden patches only',
49 action = 'store_true'),
50 make_option('-m', '--missing', metavar = 'BRANCH',
51 help = 'show patches in BRANCH missing in current'),
52 make_option('-c', '--count',
53 help = 'print the number of patches in the series',
54 action = 'store_true'),
55 make_option('-d', '--description',
56 help = 'show a short description for each patch',
57 action = 'store_true'),
58 make_option('--author',
59 help = 'show the author name for each patch',
60 action = 'store_true'),
61 make_option('-e', '--empty',
62 help = 'check whether patches are empty '
64 action = 'store_true'),
65 make_option('--showbranch',
66 help = 'append the branch name to the listed patches',
67 action = 'store_true'),
68 make_option('--noprefix',
69 help = 'do not show the patch status prefix',
70 action = 'store_true'),
71 make_option('-s', '--short',
72 help = 'list just the patches around the topmost patch',
73 action = 'store_true')]
76 def __get_description(stack, patch):
77 """Extract and return a patch's short description
79 cd = stack.patches.get(patch).commit.data
80 descr = cd.message.strip()
81 descr_lines = descr.split('\n')
82 return descr_lines[0].rstrip()
84 def __get_author(stack, patch):
85 """Extract and return a patch's short description
87 cd = stack.patches.get(patch).commit.data
90 def __print_patch(stack, patch, branch_str, prefix, empty_prefix, length, options):
91 """Print a patch name, description and various markers.
95 elif options.empty and stack.patches.get(patch).is_empty():
98 patch_str = branch_str + patch
100 if options.description or options.author:
101 patch_str = patch_str.ljust(length)
103 if options.description:
104 out.stdout(prefix + patch_str + ' # ' + __get_description(stack, patch))
106 out.stdout(prefix + patch_str + ' # ' + __get_author(stack, patch))
108 out.stdout(prefix + patch_str)
110 def func(parser, options, args):
111 """Show the patch series
113 if options.all and options.short:
114 raise common.CmdException, 'combining --all and --short is meaningless'
116 stack = directory.repository.get_stack(options.branch)
119 stack = directory.repository.get_stack(options.missing)
121 # current series patches
122 applied = unapplied = hidden = ()
123 if options.applied or options.unapplied or options.hidden:
125 raise common.CmdException, \
126 '--all cannot be used with --applied/unapplied/hidden'
128 applied = stack.patchorder.applied
129 if options.unapplied:
130 unapplied = stack.patchorder.unapplied
132 hidden = stack.patchorder.hidden
134 applied = stack.patchorder.applied
135 unapplied = stack.patchorder.unapplied
136 hidden = stack.patchorder.hidden
138 applied = stack.patchorder.applied
139 unapplied = stack.patchorder.unapplied
142 cmp_patches = cmp_stack.patchorder.all
146 # the filtering range covers the whole series
148 show_patches = parse_patches(args, applied + unapplied + hidden,
151 show_patches = applied + unapplied + hidden
154 show_patches = [p for p in show_patches if p not in cmp_patches]
157 applied = [p for p in applied if p in show_patches]
158 unapplied = [p for p in unapplied if p in show_patches]
159 hidden = [p for p in hidden if p in show_patches]
162 nr = int(config.get('stgit.shortnr'))
163 if len(applied) > nr:
164 applied = applied[-(nr+1):]
167 unapplied = unapplied[:nr]
169 hidden = hidden[:nr-n]
171 patches = applied + unapplied + hidden
174 out.stdout(len(patches))
180 if options.showbranch:
181 branch_str = stack.name + ':'
187 max_len = max([len(i + branch_str) for i in patches])
190 for p in applied[:-1]:
191 __print_patch(stack, p, branch_str, '+ ', '0 ', max_len, options)
192 __print_patch(stack, applied[-1], branch_str, '> ', '0>', max_len,
196 __print_patch(stack, p, branch_str, '- ', '0 ', max_len, options)
199 __print_patch(stack, p, branch_str, '! ', '! ', max_len, options)