Preserve picked patch name when possible
[stgit.git] / stgit / commands / files.py
blob834b415d00a22bf94ecb11811699e03cea3084ea
1 from stgit import argparse
2 from stgit.argparse import opt
3 from stgit.commands.common import (
4 CmdException,
5 DirectoryHasRepository,
6 color_diff_flags,
7 parse_rev,
9 from stgit.out import out
11 __copyright__ = """
12 Copyright (C) 2005, 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 files modified by a patch (or the current patch)'
28 kind = 'patch'
29 usage = ['[options] [--] [[<branch>:]<patch>]']
30 description = """
31 List the files modified by the given patch (defaulting to the current
32 one). Passing the '--stat' option shows the diff statistics for the
33 given patch. Note that this command doesn't show the files modified in
34 the working tree and not yet included in the patch by a 'refresh'
35 command. Use the 'diff' or 'status' commands for these files."""
37 args = ['applied_patches', 'unapplied_patches', 'hidden_patches']
38 options = [
39 opt('-s', '--stat', action='store_true', short='Show the diffstat'),
40 opt('--bare', action='store_true', short='Bare file names (useful for scripting)'),
41 ] + argparse.diff_opts_option()
43 directory = DirectoryHasRepository()
46 def func(parser, options, args):
47 """Show the files modified by a patch (or the current patch)"""
48 if options.bare and options.stat:
49 raise CmdException('Cannot specify both --bare and --stat')
50 repository = directory.repository
51 if len(args) == 0:
52 stack = repository.current_stack
53 commit = stack.top
54 elif len(args) == 1:
55 branch, name = parse_rev(args[0])
56 stack = repository.get_stack(branch)
57 if name not in stack.patches:
58 raise CmdException('%s: Unknown patch name' % name)
59 commit = stack.patches[name]
60 else:
61 parser.error('incorrect number of arguments')
63 if options.stat:
64 cmd = ['git', 'diff-tree', '--stat', '--summary', '--no-commit-id']
65 cmd.extend(options.diff_flags)
66 cmd.extend(color_diff_flags())
67 cmd.append(commit.sha1)
68 out.stdout_bytes(repository.run(cmd).decoding(None).raw_output())
69 else:
70 used = set()
71 for dt in repository.diff_tree_files(
72 commit.data.parent.data.tree, commit.data.tree
74 _, _, _, _, status, oldname, newname = dt
75 for filename in [oldname, newname]:
76 if filename in used:
77 continue
78 else:
79 used.add(filename)
81 if options.bare:
82 out.stdout(filename)
83 else:
84 out.stdout('%s %s' % (status, filename))