Preserve picked patch name when possible
[stgit.git] / stgit / commands / diff.py
blob445e40d404d540be5fd5f4b66f0dbea910ff1262
1 from stgit.argparse import diff_opts_option, opt, patch_range
2 from stgit.commands.common import DirectoryHasRepository, color_diff_flags, git_commit
3 from stgit.pager import pager
5 __copyright__ = """
6 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see http://www.gnu.org/licenses/.
19 """
21 help = 'Show the tree diff'
22 kind = 'wc'
23 usage = ['[options] [--] [<files or dirs>]']
24 description = """
25 Show the diff (default) or diffstat between the current working copy
26 or a tree-ish object and another tree-ish object (defaulting to HEAD).
27 File names can also be given to restrict the diff output. The
28 tree-ish object has the format accepted by the linkstg:id[] command."""
30 args = ['known_files', 'dirty_files']
31 options = [
32 opt(
33 '-r',
34 '--range',
35 metavar='rev1[..[rev2]]',
36 dest='revs',
37 args=[
38 patch_range(
39 'applied_patches',
40 'unapplied_patches',
41 'hidden_patches',
44 short='Show the diff between revisions',
46 opt(
47 '-s',
48 '--stat',
49 action='store_true',
50 short='Show the stat instead of the diff',
52 ] + diff_opts_option()
54 directory = DirectoryHasRepository()
57 def func(parser, options, args):
58 """Show the tree diff."""
59 repository = directory.repository
61 if options.revs:
62 rev_list = options.revs.split('..')
63 if len(rev_list) not in [1, 2] or not rev_list[0]:
64 parser.error('incorrect parameters to -r')
65 elif len(rev_list) == 1:
66 rev1 = git_commit(rev_list[0], repository)
67 rev2 = None
68 else:
69 rev1 = git_commit(rev_list[0], repository)
70 if rev_list[1]:
71 rev2 = git_commit(rev_list[1], repository)
72 else:
73 rev2 = None
74 else:
75 rev1 = repository.rev_parse('HEAD')
76 rev2 = None
78 iw = repository.default_iw
80 files = iw.ls_files(rev1.data.tree, args)
82 diff_opts = color_diff_flags()
83 diff_opts.extend(options.diff_flags)
85 if rev1 and rev2:
86 diff = repository.diff_tree(
87 rev1.data.tree,
88 rev2.data.tree,
89 diff_opts=diff_opts,
90 pathlimits=files,
91 stat=options.stat,
92 binary=False,
94 else:
95 diff = iw.diff(
96 rev1.data.tree,
97 diff_opts=diff_opts,
98 pathlimits=files,
99 stat=options.stat,
100 binary=False,
103 pager(diff)