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