chiark / gitweb /
c525b9aee08cb8c324f0e1d38b4b360044b05414
[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 = patch + branch_str
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     if options.branch:
110         stack = directory.repository.get_stack(options.branch)
111     else:
112         stack = directory.repository.current_stack
113     if options.missing:
114         cmp_stack = stack
115         stack = directory.repository.get_stack(options.missing)
116
117     # current series patches
118     if options.all:
119         applied = stack.patchorder.applied
120         unapplied = stack.patchorder.unapplied
121         hidden = stack.patchorder.hidden
122     elif options.hidden:
123         applied = unapplied = ()
124         hidden = stack.patchorder.hidden
125     else:
126         applied = stack.patchorder.applied
127         unapplied = stack.patchorder.unapplied
128         hidden = ()
129
130     if options.missing:
131         cmp_patches = cmp_stack.patchorder.all
132     else:
133         cmp_patches = ()
134
135     # the filtering range covers the whole series
136     if args:
137         show_patches = parse_patches(args, applied + unapplied + hidden,
138                                      len(applied))
139     else:
140         show_patches = applied + unapplied + hidden
141
142     # missing filtering
143     show_patches = [p for p in show_patches if p not in cmp_patches]
144
145     # filter the patches
146     applied = [p for p in applied if p in show_patches]
147     unapplied = [p for p in unapplied if p in show_patches]
148     hidden = [p for p in hidden if p in show_patches]
149
150     if options.short:
151         nr = int(config.get('stgit.shortnr'))
152         if len(applied) > nr:
153             applied = applied[-(nr+1):]
154         n = len(unapplied)
155         if n > nr:
156             unapplied = unapplied[:nr]
157         elif n < nr:
158             hidden = hidden[:nr-n]
159
160     patches = applied + unapplied + hidden
161
162     if options.count:
163         out.stdout(len(patches))
164         return
165
166     if not patches:
167         return
168
169     if options.showbranch:
170         branch_str = '@' + stack.name
171     else:
172         branch_str = ''
173
174     max_len = 0
175     if len(patches) > 0:
176         max_len = max([len(i + branch_str) for i in patches])
177
178     if applied:
179         for p in applied[:-1]:
180             __print_patch(stack, p, branch_str, '+ ', '0 ', max_len, options)
181         __print_patch(stack, applied[-1], branch_str, '> ', '0>', max_len,
182                       options)
183
184     for p in unapplied:
185         __print_patch(stack, p, branch_str, '- ', '0 ', max_len, options)
186
187     for p in hidden:
188         __print_patch(stack, p, branch_str, '! ', '! ', max_len, options)