788e57bbc151152397dd5f7d0cce5d9651667f2e
[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 '+' and the unapplied
33 ones with a '-'. The current patch is prefixed with a '>'. Empty
34 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('-m', '--missing', metavar = 'BRANCH',
39 help = 'show patches in BRANCH missing in current'),
40 make_option('-c', '--count',
41 help = 'print the number of patches in the series',
42 action = 'store_true'),
43 make_option('-d', '--description',
44 help = 'show a short description for each patch',
45 action = 'store_true'),
46 make_option('-e', '--empty',
47 help = 'check whether patches are empty '
48 '(much slower)',
49 action = 'store_true'),
50 make_option('--showbranch',
51 help = 'append the branch name to the listed patches',
52 action = 'store_true'),
53 make_option('--noprefix',
54 help = 'do not show the patch status prefix',
55 action = 'store_true'),
56 make_option('-s', '--short',
57 help = 'list just the patches around the topmost patch',
58 action = 'store_true'),
59 make_option('-g', '--graphical',
60 help = 'run gitk instead of printing',
61 action = 'store_true')]
62
63
64 def __get_description(patch):
65 """Extract and return a patch's short description
66 """
67 p = crt_series.get_patch(patch)
68 descr = (p.get_description() or '').strip()
69 descr_lines = descr.split('\n')
70 return descr_lines[0].rstrip()
71
72 def __print_patch(patch, branch_str, prefix, empty_prefix, length, options):
73 if options.noprefix:
74 prefix = ''
75 elif options.empty and crt_series.empty_patch(patch):
76 prefix = empty_prefix
77
78 patch_str = patch + branch_str
79
80 if options.description:
81 print prefix + patch_str.ljust(length) + ' | ' \
82 + __get_description(patch)
83 else:
84 print prefix + patch_str
85
86 def func(parser, options, args):
87 """Show the patch series
88 """
89 global crt_series
90
91 if options.missing:
92 # switch the series, the one specified with --missing should
93 # become the current
94 cmp_series = crt_series
95 crt_series = stack.Series(options.missing)
96 stgit.commands.common.crt_series = crt_series
97
98 cmp_patches = cmp_series.get_applied() + cmp_series.get_unapplied()
99 else:
100 cmp_patches = []
101
102 applied = crt_series.get_applied()
103 unapplied = crt_series.get_unapplied()
104
105 # the filtering range covers the whole series
106 if args:
107 show_patches = parse_patches(args, applied + unapplied, len(applied))
108 else:
109 show_patches = applied + unapplied
110
111 # filter the patches
112 applied = [p for p in applied
113 if p in show_patches and p not in cmp_patches]
114 unapplied = [p for p in unapplied
115 if p in show_patches and p not in cmp_patches]
116
117 if options.short:
118 if len(applied) > 5:
119 applied = applied[-6:]
120 if len(unapplied) > 5:
121 unapplied = unapplied[:5]
122
123 patches = applied + unapplied
124
125 if options.count:
126 print len(patches)
127 return
128
129 if not patches:
130 return
131
132 if options.showbranch:
133 branch_str = '@' + crt_series.get_branch()
134 else:
135 branch_str = ''
136
137 if options.graphical:
138 if options.missing:
139 raise CmdException, '--graphical not supported with --missing'
140
141 if applied:
142 gitk_args = ' %s^..%s' % (git_id(applied[0]), git_id(applied[-1]))
143 else:
144 gitk_args = ''
145
146 for p in unapplied:
147 patch_id = git_id(p)
148 gitk_args += ' %s^..%s' % (patch_id, patch_id)
149
150 if os.system('gitk%s' % gitk_args) != 0:
151 raise CmdException, 'gitk execution failed'
152 else:
153 max_len = 0
154 if len(patches) > 0:
155 max_len = max([len(i + branch_str) for i in patches])
156
157 if len(applied) > 0:
158 for p in applied [0:-1]:
159 __print_patch(p, branch_str, '+ ', '0 ', max_len, options)
160
161 __print_patch(applied[-1], branch_str, '> ', '0>', max_len,
162 options)
163
164 for p in unapplied:
165 __print_patch(p, branch_str, '- ', '0 ', max_len, options)