chiark / gitweb /
9e0b0ff79a963315df6ad74378139c9e0f39c3b1
[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 import sys, os
20 from optparse import OptionParser, make_option
21
22 import stgit.commands.common
23 from stgit.commands.common import *
24 from stgit.utils import *
25 from stgit import stack, git
26
27
28 help = 'print the patch series'
29 usage = """%prog [options] [<patch-range>]
30
31 Show all the patches in the series or just those in the given
32 range. The applied patches are prefixed with a '+', the unapplied ones
33 with a '-' and the hidden ones with a '!'. The current patch is
34 prefixed with a '>'. Empty patches are prefixed with a '0'."""
35
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('-i', '--invisible',
42                        help = 'show the hidden patches only',
43                        action = 'store_true'),
44            make_option('-m', '--missing', metavar = 'BRANCH',
45                        help = 'show patches in BRANCH missing in current'),
46            make_option('-c', '--count',
47                        help = 'print the number of patches in the series',
48                        action = 'store_true'),
49            make_option('-d', '--description',
50                        help = 'show a short description for each patch',
51                        action = 'store_true'),
52            make_option('--author',
53                        help = 'show the author name for each patch',
54                        action = 'store_true'),
55            make_option('-e', '--empty',
56                        help = 'check whether patches are empty '
57                        '(much slower)',
58                        action = 'store_true'),
59            make_option('--showbranch',
60                        help = 'append the branch name to the listed patches',
61                        action = 'store_true'),
62            make_option('--noprefix',
63                        help = 'do not show the patch status prefix',
64                        action = 'store_true'),
65            make_option('-s', '--short',
66                        help = 'list just the patches around the topmost patch',
67                        action = 'store_true'),
68            make_option('-g', '--graphical',
69                        help = 'run gitk instead of printing',
70                        action = 'store_true')]
71
72
73 def __get_description(patch):
74     """Extract and return a patch's short description
75     """
76     p = crt_series.get_patch(patch)
77     descr = (p.get_description() or '').strip()
78     descr_lines = descr.split('\n')
79     return descr_lines[0].rstrip()
80
81 def __get_author(patch):
82     """Extract and return a patch's short description
83     """
84     p = crt_series.get_patch(patch)
85     return p.get_authname();
86
87 def __print_patch(patch, branch_str, prefix, empty_prefix, length, options):
88     """Print a patch name, description and various markers.
89     """
90     if options.noprefix:
91         prefix = ''
92     elif options.empty and crt_series.empty_patch(patch):
93         prefix = empty_prefix
94
95     patch_str = patch + branch_str
96
97     if options.description or options.author:
98         patch_str = patch_str.ljust(length)
99
100     if options.description:
101         out.stdout(prefix + patch_str + ' | ' + __get_description(patch))
102     elif options.author:
103         out.stdout(prefix + patch_str + ' | ' + __get_author(patch))
104     else:
105         out.stdout(prefix + patch_str)
106
107 def func(parser, options, args):
108     """Show the patch series
109     """
110     global crt_series
111
112     if options.all and options.short:
113         raise CmdException, 'combining --all and --short is meaningless'
114     
115     # current series patches
116     if options.invisible:
117         applied = unapplied = []
118         hidden = crt_series.get_hidden()
119     elif options.all:
120         applied = crt_series.get_applied()
121         unapplied = crt_series.get_unapplied()
122         hidden = crt_series.get_hidden()
123     else:
124         applied = crt_series.get_applied()
125         unapplied = crt_series.get_unapplied()
126         hidden = []
127
128     if options.missing:
129         # switch the series, the one specified with --missing should
130         # become the current
131         cmp_series = crt_series
132         crt_series = stack.Series(options.missing)
133         stgit.commands.common.crt_series = crt_series
134
135         cmp_patches = applied + unapplied + hidden
136
137         # new current series patches
138         if options.invisible:
139             applied = unapplied = []
140             hidden = crt_series.get_hidden()
141         elif options.all:
142             applied = crt_series.get_applied()
143             unapplied = crt_series.get_unapplied()
144             hidden = crt_series.get_hidden()
145         else:
146             applied = crt_series.get_applied()
147             unapplied = crt_series.get_unapplied()
148             hidden = []
149     else:
150         cmp_patches = []
151
152     # the filtering range covers the whole series
153     if args:
154         show_patches = parse_patches(args, applied + unapplied + hidden,
155                                      len(applied))
156     else:
157         show_patches = applied + unapplied + hidden
158
159     # missing filtering
160     show_patches = [p for p in show_patches if p not in cmp_patches]
161
162     # filter the patches
163     applied = [p for p in applied if p in show_patches]
164     unapplied = [p for p in unapplied if p in show_patches]
165     hidden = [p for p in hidden if p in show_patches]
166
167     if options.short:
168         nr = int(config.get('stgit.shortnr'))
169         if len(applied) > nr:
170             applied = applied[-(nr+1):]
171         n = len(unapplied)
172         if n > nr:
173             unapplied = unapplied[:nr]
174         elif n < nr:
175             hidden = hidden[:nr-n]
176
177     patches = applied + unapplied + hidden
178
179     if options.count:
180         out.stdout(len(patches))
181         return
182
183     if not patches:
184         return
185
186     if options.showbranch:
187         branch_str = '@' + crt_series.get_name()
188     else:
189         branch_str = ''
190
191     if options.graphical:
192         if options.missing:
193             raise CmdException, '--graphical not supported with --missing'
194
195         if applied:
196             gitk_args = ' %s^..%s' % (git_id(applied[0]), git_id(applied[-1]))
197         else:
198             gitk_args = ''
199
200         for p in unapplied:
201             patch_id = git_id(p)
202             gitk_args += ' %s^..%s' % (patch_id, patch_id)
203
204         if os.system('gitk%s' % gitk_args) != 0:
205             raise CmdException, 'gitk execution failed'
206     else:
207         max_len = 0
208         if len(patches) > 0:
209             max_len = max([len(i + branch_str) for i in patches])
210
211         if applied:
212             for p in applied[:-1]:
213                 __print_patch(p, branch_str, '+ ', '0 ', max_len, options)
214             __print_patch(applied[-1], branch_str, '> ', '0>', max_len,
215                           options)
216
217         for p in unapplied:
218             __print_patch(p, branch_str, '- ', '0 ', max_len, options)
219
220         for p in hidden:
221             __print_patch(p, branch_str, '! ', '! ', max_len, options)