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