chiark / gitweb /
c11c74f3f5569a18852e4bff43a36611849f1d50
[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('--hidden',
41                        help = 'show the hidden patches only',
42                        action = 'store_true'),
43            make_option('-m', '--missing', metavar = 'BRANCH',
44                        help = 'show patches in BRANCH missing in current'),
45            make_option('-c', '--count',
46                        help = 'print the number of patches in the series',
47                        action = 'store_true'),
48            make_option('-d', '--description',
49                        help = 'show a short description for each patch',
50                        action = 'store_true'),
51            make_option('--author',
52                        help = 'show the author name for each patch',
53                        action = 'store_true'),
54            make_option('-e', '--empty',
55                        help = 'check whether patches are empty '
56                        '(much slower)',
57                        action = 'store_true'),
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'),
64            make_option('-s', '--short',
65                        help = 'list just the patches around the topmost patch',
66                        action = 'store_true')]
67
68
69 def __get_description(stack, patch):
70     """Extract and return a patch's short description
71     """
72     cd = stack.patches.get(patch).commit.data
73     descr = cd.message.strip()
74     descr_lines = descr.split('\n')
75     return descr_lines[0].rstrip()
76
77 def __get_author(stack, patch):
78     """Extract and return a patch's short description
79     """
80     cd = stack.patches.get(patch).commit.data
81     return cd.author.name
82
83 def __print_patch(stack, patch, branch_str, prefix, empty_prefix, length, options):
84     """Print a patch name, description and various markers.
85     """
86     if options.noprefix:
87         prefix = ''
88     elif options.empty and stack.patches.get(patch).is_empty():
89         prefix = empty_prefix
90
91     patch_str = branch_str + patch
92
93     if options.description or options.author:
94         patch_str = patch_str.ljust(length)
95
96     if options.description:
97         out.stdout(prefix + patch_str + ' # ' + __get_description(stack, patch))
98     elif options.author:
99         out.stdout(prefix + patch_str + ' # ' + __get_author(stack, patch))
100     else:
101         out.stdout(prefix + patch_str)
102
103 def func(parser, options, args):
104     """Show the patch series
105     """
106     if options.all and options.short:
107         raise common.CmdException, 'combining --all and --short is meaningless'
108
109     stack = directory.repository.get_stack(options.branch)
110     if options.missing:
111         cmp_stack = stack
112         stack = directory.repository.get_stack(options.missing)
113
114     # current series patches
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
122     else:
123         applied = stack.patchorder.applied
124         unapplied = stack.patchorder.unapplied
125         hidden = ()
126
127     if options.missing:
128         cmp_patches = cmp_stack.patchorder.all
129     else:
130         cmp_patches = ()
131
132     # the filtering range covers the whole series
133     if args:
134         show_patches = parse_patches(args, applied + unapplied + hidden,
135                                      len(applied))
136     else:
137         show_patches = applied + unapplied + hidden
138
139     # missing filtering
140     show_patches = [p for p in show_patches if p not in cmp_patches]
141
142     # filter the patches
143     applied = [p for p in applied if p in show_patches]
144     unapplied = [p for p in unapplied if p in show_patches]
145     hidden = [p for p in hidden if p in show_patches]
146
147     if options.short:
148         nr = int(config.get('stgit.shortnr'))
149         if len(applied) > nr:
150             applied = applied[-(nr+1):]
151         n = len(unapplied)
152         if n > nr:
153             unapplied = unapplied[:nr]
154         elif n < nr:
155             hidden = hidden[:nr-n]
156
157     patches = applied + unapplied + hidden
158
159     if options.count:
160         out.stdout(len(patches))
161         return
162
163     if not patches:
164         return
165
166     if options.showbranch:
167         branch_str = stack.name + ':'
168     else:
169         branch_str = ''
170
171     max_len = 0
172     if len(patches) > 0:
173         max_len = max([len(i + branch_str) for i in patches])
174
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)
180
181     for p in unapplied:
182         __print_patch(stack, p, branch_str, '- ', '0 ', max_len, options)
183
184     for p in hidden:
185         __print_patch(stack, p, branch_str, '! ', '! ', max_len, options)