##// END OF EJS Templates
extdiff: make new diff commands pick up their options correctly
TK Soh -
r2959:7f5fc4b3 default
parent child Browse files
Show More
@@ -1,175 +1,175 b''
1 1 # extdiff.py - external diff program support for mercurial
2 2 #
3 3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7 #
8 8 # The `extdiff' Mercurial extension allows you to use external programs
9 9 # to compare revisions, or revision with working dir. The external diff
10 10 # programs are called with a configurable set of options and two
11 11 # non-option arguments: paths to directories containing snapshots of
12 12 # files to compare.
13 13 #
14 14 # To enable this extension:
15 15 #
16 16 # [extensions]
17 17 # hgext.extdiff =
18 18 #
19 19 # The `extdiff' extension also allows to configure new diff commands, so
20 20 # you do not need to type "hg extdiff -p kdiff3" always.
21 21 #
22 22 # [extdiff]
23 23 # # add new command that runs GNU diff(1) in 'context diff' mode
24 24 # cmd.cdiff = gdiff
25 25 # opts.cdiff = -Nprc5
26 26 # # add new command called vdiff, runs kdiff3
27 27 # cmd.vdiff = kdiff3
28 28 # # add new command called meld, runs meld (no need to name twice)
29 29 # cmd.meld =
30 30 # # add new command called vimdiff, runs gvimdiff with DirDiff plugin
31 31 # #(see http://www.vim.org/scripts/script.php?script_id=102)
32 32 # cmd.vimdiff = LC_ALL=C gvim -f '+bdel 1 2' '+ execute "DirDiff ".argv(0)." ".argv(1)'
33 33 #
34 34 # Each custom diff commands can have two parts: a `cmd' and an `opts'
35 35 # part. The cmd.xxx option defines the name of an executable program
36 36 # that will be run, and opts.xxx defines a set of command-line options
37 37 # which will be inserted to the command between the program name and
38 38 # the files/directories to diff (i.e. the cdiff example above).
39 39 #
40 40 # You can use -I/-X and list of file or directory names like normal
41 41 # "hg diff" command. The `extdiff' extension makes snapshots of only
42 42 # needed files, so running the external diff program will actually be
43 43 # pretty fast (at least faster than having to compare the entire tree).
44 44
45 45 from mercurial.demandload import demandload
46 46 from mercurial.i18n import gettext as _
47 47 from mercurial.node import *
48 48 demandload(globals(), 'mercurial:commands,cmdutil,util os shutil tempfile')
49 49
50 50 def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
51 51 def snapshot_node(files, node):
52 52 '''snapshot files as of some revision'''
53 53 changes = repo.changelog.read(node)
54 54 mf = repo.manifest.read(changes[0])
55 55 dirname = '%s.%s' % (os.path.basename(repo.root), short(node))
56 56 base = os.path.join(tmproot, dirname)
57 57 os.mkdir(base)
58 58 if not ui.quiet:
59 59 ui.write_err(_('making snapshot of %d files from rev %s\n') %
60 60 (len(files), short(node)))
61 61 for fn in files:
62 62 wfn = util.pconvert(fn)
63 63 ui.note(' %s\n' % wfn)
64 64 dest = os.path.join(base, wfn)
65 65 destdir = os.path.dirname(dest)
66 66 if not os.path.isdir(destdir):
67 67 os.makedirs(destdir)
68 68 repo.wwrite(wfn, repo.file(fn).read(mf[fn]), open(dest, 'w'))
69 69 return dirname
70 70
71 71 def snapshot_wdir(files):
72 72 '''snapshot files from working directory.
73 73 if not using snapshot, -I/-X does not work and recursive diff
74 74 in tools like kdiff3 and meld displays too many files.'''
75 75 dirname = os.path.basename(repo.root)
76 76 base = os.path.join(tmproot, dirname)
77 77 os.mkdir(base)
78 78 if not ui.quiet:
79 79 ui.write_err(_('making snapshot of %d files from working dir\n') %
80 80 (len(files)))
81 81 for fn in files:
82 82 wfn = util.pconvert(fn)
83 83 ui.note(' %s\n' % wfn)
84 84 dest = os.path.join(base, wfn)
85 85 destdir = os.path.dirname(dest)
86 86 if not os.path.isdir(destdir):
87 87 os.makedirs(destdir)
88 88 fp = open(dest, 'w')
89 89 for chunk in util.filechunkiter(repo.wopener(wfn)):
90 90 fp.write(chunk)
91 91 return dirname
92 92
93 93 node1, node2 = commands.revpair(ui, repo, opts['rev'])
94 94 files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)
95 95 modified, added, removed, deleted, unknown = repo.status(
96 96 node1, node2, files, match=matchfn)[:5]
97 97 if not (modified or added or removed):
98 98 return 0
99 99
100 100 tmproot = tempfile.mkdtemp(prefix='extdiff.')
101 101 try:
102 102 dir1 = snapshot_node(modified + removed, node1)
103 103 if node2:
104 104 dir2 = snapshot_node(modified + added, node2)
105 105 else:
106 106 dir2 = snapshot_wdir(modified + added)
107 107 cmdline = ('%s %s %s %s' %
108 108 (util.shellquote(diffcmd),
109 109 ' '.join(map(util.shellquote, diffopts)),
110 110 util.shellquote(dir1), util.shellquote(dir2)))
111 111 ui.debug('running %r in %s\n' % (cmdline, tmproot))
112 112 util.system(cmdline, cwd=tmproot)
113 113 return 1
114 114 finally:
115 115 ui.note(_('cleaning up temp directory\n'))
116 116 shutil.rmtree(tmproot)
117 117
118 118 def extdiff(ui, repo, *pats, **opts):
119 119 '''use external program to diff repository (or selected files)
120 120
121 121 Show differences between revisions for the specified files, using
122 122 an external program. The default program used is diff, with
123 123 default options "-Npru".
124 124
125 125 To select a different program, use the -p option. The program
126 126 will be passed the names of two directories to compare. To pass
127 127 additional options to the program, use the -o option. These will
128 128 be passed before the names of the directories to compare.
129 129
130 130 When two revision arguments are given, then changes are
131 131 shown between those revisions. If only one revision is
132 132 specified then that revision is compared to the working
133 133 directory, and, when no revisions are specified, the
134 134 working directory files are compared to its parent.'''
135 135 return dodiff(ui, repo, opts['program'] or 'diff',
136 136 opts['option'] or ['-Npru'], pats, opts)
137 137
138 138 cmdtable = {
139 139 "extdiff":
140 140 (extdiff,
141 141 [('p', 'program', '', _('comparison program to run')),
142 142 ('o', 'option', [], _('pass option to comparison program')),
143 143 ('r', 'rev', [], _('revision')),
144 144 ('I', 'include', [], _('include names matching the given patterns')),
145 145 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
146 146 _('hg extdiff [OPT]... [FILE]...')),
147 147 }
148 148
149 149 def uisetup(ui):
150 150 for cmd, path in ui.configitems('extdiff'):
151 151 if not cmd.startswith('cmd.'): continue
152 152 cmd = cmd[4:]
153 153 if not path: path = cmd
154 154 diffopts = ui.config('extdiff', 'opts.' + cmd, '')
155 155 diffopts = diffopts and [diffopts] or []
156 def save(cmd, path):
156 def save(cmd, path, diffopts):
157 157 '''use closure to save diff command to use'''
158 158 def mydiff(ui, repo, *pats, **opts):
159 159 return dodiff(ui, repo, path, diffopts, pats, opts)
160 160 mydiff.__doc__ = '''use %(path)r to diff repository (or selected files)
161 161
162 162 Show differences between revisions for the specified
163 163 files, using the %(path)r program.
164 164
165 165 When two revision arguments are given, then changes are
166 166 shown between those revisions. If only one revision is
167 167 specified then that revision is compared to the working
168 168 directory, and, when no revisions are specified, the
169 169 working directory files are compared to its parent.''' % {
170 170 'path': path,
171 171 }
172 172 return mydiff
173 cmdtable[cmd] = (save(cmd, path),
173 cmdtable[cmd] = (save(cmd, path, diffopts),
174 174 cmdtable['extdiff'][1][1:],
175 175 _('hg %s [OPT]... [FILE]...') % cmd)
General Comments 0
You need to be logged in to leave comments. Login now