1 # extdiff.py - external diff program support for mercurial
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
8 '''command to allow external programs to compare revisions
10 The extdiff Mercurial extension allows you to use external programs
11 to compare revisions, or revision with working directory. The external
12 diff programs are called with a configurable set of options and two
13 non-option arguments: paths to directories containing snapshots of
16 The extdiff extension also allows to configure new diff commands, so
17 you do not need to type "hg extdiff -p kdiff3" always. ::
20 # add new command that runs GNU diff(1) in 'context diff' mode
26 # add new command called vdiff, runs kdiff3
29 # add new command called meld, runs meld (no need to name twice)
32 # add new command called vimdiff, runs gvimdiff with DirDiff plugin
33 # (see http://www.vim.org/scripts/script.php?script_id=102) Non
34 # English user, be sure to put "let g:DirDiffDynamicDiffText = 1" in
36 vimdiff = gvim -f '+next' '+execute "DirDiff" argv(0) argv(1)'
38 You can use -I/-X and list of file or directory names like normal "hg
39 diff" command. The extdiff extension makes snapshots of only needed
40 files, so running the external diff program will actually be pretty
41 fast (at least faster than having to compare the entire tree).
44 from mercurial.i18n import _
45 from mercurial.node import short
46 from mercurial import cmdutil, util, commands
47 import os, shlex, shutil, tempfile
49 def snapshot(ui, repo, files, node, tmproot):
50 '''snapshot files as of some revision
51 if not using snapshot, -I/-X does not work and recursive diff
52 in tools like kdiff3 and meld displays too many files.'''
53 dirname = os.path.basename(repo.root)
57 dirname = '%s.%s' % (dirname, short(node))
58 base = os.path.join(tmproot, dirname)
61 ui.note(_('making snapshot of %d files from rev %s\n') %
62 (len(files), short(node)))
64 ui.note(_('making snapshot of %d files from working directory\n') %
66 wopener = util.opener(base)
70 wfn = util.pconvert(fn)
72 # skipping new file after a merge ?
74 ui.note(' %s\n' % wfn)
75 dest = os.path.join(base, wfn)
77 data = repo.wwritedata(wfn, fctx.data())
78 if 'l' in fctx.flags():
79 wopener.symlink(data, wfn)
81 wopener(wfn, 'w').write(data)
82 if 'x' in fctx.flags():
83 util.set_flags(dest, False, True)
85 fns_and_mtime.append((dest, repo.wjoin(fn), os.path.getmtime(dest)))
86 return dirname, fns_and_mtime
88 def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
89 '''Do the actuall diff:
91 - copy to a temp structure if diffing 2 internal revisions
92 - copy to a temp structure if diffing working revision with
93 another one and more than 1 file is changed
94 - just invoke the diff for a single file in the working dir
97 revs = opts.get('rev')
98 change = opts.get('change')
101 msg = _('cannot specify --rev and --change at the same time')
102 raise util.Abort(msg)
104 node2 = repo.lookup(change)
105 node1 = repo[node2].parents()[0].node()
107 node1, node2 = cmdutil.revpair(repo, revs)
109 matcher = cmdutil.match(repo, pats, opts)
110 modified, added, removed = repo.status(node1, node2, matcher)[:3]
111 if not (modified or added or removed):
114 tmproot = tempfile.mkdtemp(prefix='extdiff.')
117 # Always make a copy of node1
118 dir1 = snapshot(ui, repo, modified + removed, node1, tmproot)[0]
119 changes = len(modified) + len(removed) + len(added)
121 # If node2 in not the wc or there is >1 change, copy it
122 if node2 or changes > 1:
123 dir2, fns_and_mtime = snapshot(ui, repo, modified + added, node2, tmproot)
125 # This lets the diff tool open the changed file directly
130 # If only one change, diff the files instead of the directories
133 dir1 = os.path.join(dir1, util.localpath(modified[0]))
134 dir2 = os.path.join(dir2root, dir2, util.localpath(modified[0]))
136 dir1 = os.path.join(dir1, util.localpath(removed[0]))
140 dir2 = os.path.join(dir2root, dir2, util.localpath(added[0]))
142 cmdline = ('%s %s %s %s' %
143 (util.shellquote(diffcmd), ' '.join(diffopts),
144 util.shellquote(dir1), util.shellquote(dir2)))
145 ui.debug(_('running %r in %s\n') % (cmdline, tmproot))
146 util.system(cmdline, cwd=tmproot)
148 for copy_fn, working_fn, mtime in fns_and_mtime:
149 if os.path.getmtime(copy_fn) != mtime:
150 ui.debug(_('file changed while diffing. '
151 'Overwriting: %s (src: %s)\n') % (working_fn, copy_fn))
152 util.copyfile(copy_fn, working_fn)
156 ui.note(_('cleaning up temp directory\n'))
157 shutil.rmtree(tmproot)
159 def extdiff(ui, repo, *pats, **opts):
160 '''use external program to diff repository (or selected files)
162 Show differences between revisions for the specified files, using
163 an external program. The default program used is diff, with
164 default options "-Npru".
166 To select a different program, use the -p/--program option. The
167 program will be passed the names of two directories to compare. To
168 pass additional options to the program, use -o/--option. These
169 will be passed before the names of the directories to compare.
171 When two revision arguments are given, then changes are shown
172 between those revisions. If only one revision is specified then
173 that revision is compared to the working directory, and, when no
174 revisions are specified, the working directory files are compared
176 program = opts['program'] or 'diff'
178 option = opts['option']
180 option = opts['option'] or ['-Npru']
181 return dodiff(ui, repo, program, option, pats, opts)
186 [('p', 'program', '', _('comparison program to run')),
187 ('o', 'option', [], _('pass option to comparison program')),
188 ('r', 'rev', [], _('revision')),
189 ('c', 'change', '', _('change made by revision')),
190 ] + commands.walkopts,
191 _('hg extdiff [OPT]... [FILE]...')),
195 for cmd, path in ui.configitems('extdiff'):
196 if cmd.startswith('cmd.'):
198 if not path: path = cmd
199 diffopts = ui.config('extdiff', 'opts.' + cmd, '')
200 diffopts = diffopts and [diffopts] or []
201 elif cmd.startswith('opts.'):
204 # command = path opts
206 diffopts = shlex.split(path)
207 path = diffopts.pop(0)
209 path, diffopts = cmd, []
210 def save(cmd, path, diffopts):
211 '''use closure to save diff command to use'''
212 def mydiff(ui, repo, *pats, **opts):
213 return dodiff(ui, repo, path, diffopts, pats, opts)
214 mydiff.__doc__ = _('''\
215 use %(path)s to diff repository (or selected files)
217 Show differences between revisions for the specified files, using the
220 When two revision arguments are given, then changes are shown between
221 those revisions. If only one revision is specified then that revision is
222 compared to the working directory, and, when no revisions are specified,
223 the working directory files are compared to its parent.\
224 ''') % dict(path=util.uirepr(path))
226 cmdtable[cmd] = (save(cmd, path, diffopts),
227 cmdtable['extdiff'][1][1:],
228 _('hg %s [OPTION]... [FILE]...') % cmd)