Print conflict details with the new infrastructure (bug #11181)
[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 stgit.argparse import opt
20 from stgit.commands import common
21 from stgit.commands.common import parse_patches
22 from stgit.out import out
23 from stgit.config import config
24 from stgit import argparse
25
26 help = 'Print the patch series'
27 kind = 'stack'
28 usage = ['[options] [<patch-range>]']
29 description = """
30 Show all the patches in the series or just those in the given
31 range. The applied patches are prefixed with a '+', the unapplied ones
32 with a '-' and the hidden ones with a '!'. The current patch is
33 prefixed with a '>'. Empty patches are prefixed with a '0'."""
34
35 args = [argparse.patch_range(argparse.applied_patches,
36 argparse.unapplied_patches,
37 argparse.hidden_patches)]
38 options = [
39 opt('-b', '--branch', args = [argparse.stg_branches],
40 short = 'Use BRANCH instead of the default branch'),
41 opt('-a', '--all', action = 'store_true',
42 short = 'Show all patches, including the hidden ones'),
43 opt('-A', '--applied', action = 'store_true',
44 short = 'Show the applied patches only'),
45 opt('-U', '--unapplied', action = 'store_true',
46 short = 'Show the unapplied patches only'),
47 opt('-H', '--hidden', action = 'store_true',
48 short = 'Show the hidden patches only'),
49 opt('-m', '--missing', metavar = 'BRANCH', args = [argparse.stg_branches],
50 short = 'Show patches in BRANCH missing in current'),
51 opt('-c', '--count', action = 'store_true',
52 short = 'Print the number of patches in the series'),
53 opt('-d', '--description', action = 'store_true',
54 short = 'Show a short description for each patch'),
55 opt('--author', action = 'store_true',
56 short = 'Show the author name for each patch'),
57 opt('-e', '--empty', action = 'store_true',
58 short = 'Check whether patches are empty'),
59 opt('--showbranch', action = 'store_true',
60 short = 'Append the branch name to the listed patches'),
61 opt('--noprefix', action = 'store_true',
62 short = 'Do not show the patch status prefix'),
63 opt('-s', '--short', action = 'store_true',
64 short = 'List just the patches around the topmost patch')]
65
66 directory = common.DirectoryHasRepositoryLib()
67
68 def __get_description(stack, patch):
69 """Extract and return a patch's short description
70 """
71 cd = stack.patches.get(patch).commit.data
72 descr = cd.message.strip()
73 descr_lines = descr.split('\n')
74 return descr_lines[0].rstrip()
75
76 def __get_author(stack, patch):
77 """Extract and return a patch's short description
78 """
79 cd = stack.patches.get(patch).commit.data
80 return cd.author.name
81
82 def __print_patch(stack, patch, branch_str, prefix, empty_prefix, length, options):
83 """Print a patch name, description and various markers.
84 """
85 if options.noprefix:
86 prefix = ''
87 elif options.empty and stack.patches.get(patch).is_empty():
88 prefix = empty_prefix
89
90 patch_str = branch_str + patch
91
92 if options.description or options.author:
93 patch_str = patch_str.ljust(length)
94
95 if options.description:
96 out.stdout(prefix + patch_str + ' # ' + __get_description(stack, patch))
97 elif options.author:
98 out.stdout(prefix + patch_str + ' # ' + __get_author(stack, patch))
99 else:
100 out.stdout(prefix + patch_str)
101
102 def func(parser, options, args):
103 """Show the patch series
104 """
105 if options.all and options.short:
106 raise common.CmdException, 'combining --all and --short is meaningless'
107
108 stack = directory.repository.get_stack(options.branch)
109 if options.missing:
110 cmp_stack = stack
111 stack = directory.repository.get_stack(options.missing)
112
113 # current series patches
114 applied = unapplied = hidden = ()
115 if options.applied or options.unapplied or options.hidden:
116 if options.all:
117 raise common.CmdException, \
118 '--all cannot be used with --applied/unapplied/hidden'
119 if options.applied:
120 applied = stack.patchorder.applied
121 if options.unapplied:
122 unapplied = stack.patchorder.unapplied
123 if options.hidden:
124 hidden = stack.patchorder.hidden
125 elif options.all:
126 applied = stack.patchorder.applied
127 unapplied = stack.patchorder.unapplied
128 hidden = stack.patchorder.hidden
129 else:
130 applied = stack.patchorder.applied
131 unapplied = stack.patchorder.unapplied
132
133 if options.missing:
134 cmp_patches = cmp_stack.patchorder.all
135 else:
136 cmp_patches = ()
137
138 # the filtering range covers the whole series
139 if args:
140 show_patches = parse_patches(args, applied + unapplied + hidden,
141 len(applied))
142 else:
143 show_patches = applied + unapplied + hidden
144
145 # missing filtering
146 show_patches = [p for p in show_patches if p not in cmp_patches]
147
148 # filter the patches
149 applied = [p for p in applied if p in show_patches]
150 unapplied = [p for p in unapplied if p in show_patches]
151 hidden = [p for p in hidden if p in show_patches]
152
153 if options.short:
154 nr = int(config.get('stgit.shortnr'))
155 if len(applied) > nr:
156 applied = applied[-(nr+1):]
157 n = len(unapplied)
158 if n > nr:
159 unapplied = unapplied[:nr]
160 elif n < nr:
161 hidden = hidden[:nr-n]
162
163 patches = applied + unapplied + hidden
164
165 if options.count:
166 out.stdout(len(patches))
167 return
168
169 if not patches:
170 return
171
172 if options.showbranch:
173 branch_str = stack.name + ':'
174 else:
175 branch_str = ''
176
177 max_len = 0
178 if len(patches) > 0:
179 max_len = max([len(i + branch_str) for i in patches])
180
181 if applied:
182 for p in applied[:-1]:
183 __print_patch(stack, p, branch_str, '+ ', '0 ', max_len, options)
184 __print_patch(stack, applied[-1], branch_str, '> ', '0>', max_len,
185 options)
186
187 for p in unapplied:
188 __print_patch(stack, p, branch_str, '- ', '0 ', max_len, options)
189
190 for p in hidden:
191 __print_patch(stack, p, branch_str, '! ', '! ', max_len, options)