More changelog updates
[stgit.git] / stgit / commands / show.py
blob9943e3ce1c28ec60990798b5e63b8a229329cc3a
1 from stgit.argparse import diff_opts_option, opt, patch_range
2 from stgit.commands.common import (
3 DirectoryHasRepository,
4 color_diff_flags,
5 parse_patches,
7 from stgit.lib.git import RepositoryException
8 from stgit.pager import pager
9 from stgit.run import Run
11 __copyright__ = """
12 Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License version 2 as
16 published by the Free Software Foundation.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see http://www.gnu.org/licenses/.
25 """
27 help = 'Show the commit corresponding to a patch'
28 kind = 'patch'
29 usage = [
30 '[-b] [-s] [-O] [--] [<patch1>] [<patch2>] [<patch3>..<patch4>]',
31 '(--applied | --unapplied) [-b] [-s] [-O]',
34 description = """
35 Show the commit log and the diff corresponding to the given patches.
36 The output is similar to that generated by 'git show'."""
38 args = [patch_range('applied_patches', 'unapplied_patches', 'hidden_patches')]
39 options = [
40 opt(
41 '-b',
42 '--branch',
43 args=['stg_branches'],
44 short='Use BRANCH instead of the default branch',
46 opt(
47 '-a',
48 '--applied',
49 action='store_true',
50 short='Show the applied patches',
52 opt(
53 '-u',
54 '--unapplied',
55 action='store_true',
56 short='Show the unapplied patches',
58 opt(
59 '-s',
60 '--stat',
61 action='store_true',
62 short='Show a diffstat summary of the specified patches',
64 ] + diff_opts_option()
66 directory = DirectoryHasRepository()
69 def func(parser, options, args):
70 """Show commit log and diff"""
71 if args and (options.applied or options.unapplied):
72 parser.error('patches may not be given with --applied or --unapplied')
73 elif options.applied and options.unapplied:
74 parser.error('cannot use both --applied and --unapplied')
76 repository = directory.repository
77 stack = repository.get_stack(options.branch)
78 patchorder = stack.patchorder
80 if options.applied:
81 commits = [stack.patches[pn] for pn in patchorder.applied]
82 elif options.unapplied:
83 commits = [stack.patches[pn] for pn in patchorder.unapplied]
84 elif not args:
85 commits = [stack.top]
86 elif '..' in ' '.join(args):
87 # patch ranges
88 patch_names = parse_patches(
89 args,
90 patchorder.all,
91 len(patchorder.applied),
93 commits = [stack.patches[pn] for pn in patch_names]
94 else:
95 commits = []
96 for name in args:
97 if name in stack.patches:
98 commits.append(stack.patches[name])
99 else:
100 try:
101 commits.append(
102 repository.rev_parse(
103 name, object_type='commit', discard_stderr=True
106 except RepositoryException:
107 raise RepositoryException(
108 '%s: Unknown patch or revision name' % name
111 cmd = ['git', 'show']
112 if options.stat:
113 cmd.extend(['--stat', '--summary'])
114 else:
115 cmd.append('--patch')
116 cmd.extend(options.diff_flags)
117 cmd.extend(color_diff_flags())
118 cmd.extend(commit.sha1 for commit in commits)
119 pager(Run(*cmd).decoding(None).raw_output())