##// END OF EJS Templates
extdiff: merge node and working dir snapshot modes
Patrick Mezard -
r8064:5c7bc1ae default
parent child Browse files
Show More
@@ -1,260 +1,230
1 # extdiff.py - external diff program support for mercurial
1 # extdiff.py - external diff program support for mercurial
2 #
2 #
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
3 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
4 #
4 #
5 # This software may be used and distributed according to the terms
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
6 # of the GNU General Public License, incorporated herein by reference.
7
7
8 '''
8 '''
9 The `extdiff' Mercurial extension allows you to use external programs
9 The `extdiff' Mercurial extension allows you to use external programs
10 to compare revisions, or revision with working dir. The external diff
10 to compare revisions, or revision with working dir. The external diff
11 programs are called with a configurable set of options and two
11 programs are called with a configurable set of options and two
12 non-option arguments: paths to directories containing snapshots of
12 non-option arguments: paths to directories containing snapshots of
13 files to compare.
13 files to compare.
14
14
15 To enable this extension:
15 To enable this extension:
16
16
17 [extensions]
17 [extensions]
18 hgext.extdiff =
18 hgext.extdiff =
19
19
20 The `extdiff' extension also allows to configure new diff commands, so
20 The `extdiff' extension also allows to configure new diff commands, so
21 you do not need to type "hg extdiff -p kdiff3" always.
21 you do not need to type "hg extdiff -p kdiff3" always.
22
22
23 [extdiff]
23 [extdiff]
24 # add new command that runs GNU diff(1) in 'context diff' mode
24 # add new command that runs GNU diff(1) in 'context diff' mode
25 cdiff = gdiff -Nprc5
25 cdiff = gdiff -Nprc5
26 ## or the old way:
26 ## or the old way:
27 #cmd.cdiff = gdiff
27 #cmd.cdiff = gdiff
28 #opts.cdiff = -Nprc5
28 #opts.cdiff = -Nprc5
29
29
30 # add new command called vdiff, runs kdiff3
30 # add new command called vdiff, runs kdiff3
31 vdiff = kdiff3
31 vdiff = kdiff3
32
32
33 # add new command called meld, runs meld (no need to name twice)
33 # add new command called meld, runs meld (no need to name twice)
34 meld =
34 meld =
35
35
36 # add new command called vimdiff, runs gvimdiff with DirDiff plugin
36 # add new command called vimdiff, runs gvimdiff with DirDiff plugin
37 #(see http://www.vim.org/scripts/script.php?script_id=102)
37 #(see http://www.vim.org/scripts/script.php?script_id=102)
38 # Non english user, be sure to put "let g:DirDiffDynamicDiffText = 1" in
38 # Non english user, be sure to put "let g:DirDiffDynamicDiffText = 1" in
39 # your .vimrc
39 # your .vimrc
40 vimdiff = gvim -f '+next' '+execute "DirDiff" argv(0) argv(1)'
40 vimdiff = gvim -f '+next' '+execute "DirDiff" argv(0) argv(1)'
41
41
42 You can use -I/-X and list of file or directory names like normal
42 You can use -I/-X and list of file or directory names like normal
43 "hg diff" command. The `extdiff' extension makes snapshots of only
43 "hg diff" command. The `extdiff' extension makes snapshots of only
44 needed files, so running the external diff program will actually be
44 needed files, so running the external diff program will actually be
45 pretty fast (at least faster than having to compare the entire tree).
45 pretty fast (at least faster than having to compare the entire tree).
46 '''
46 '''
47
47
48 from mercurial.i18n import _
48 from mercurial.i18n import _
49 from mercurial.node import short
49 from mercurial.node import short
50 from mercurial import cmdutil, util, commands
50 from mercurial import cmdutil, util, commands
51 import os, shlex, shutil, tempfile
51 import os, shlex, shutil, tempfile
52
52
53 def snapshot_node(ui, repo, files, node, tmproot):
53 def snapshot(ui, repo, files, node, tmproot):
54 '''snapshot files as of some revision'''
54 '''snapshot files as of some revision
55 if not using snapshot, -I/-X does not work and recursive diff
56 in tools like kdiff3 and meld displays too many files.'''
55 dirname = os.path.basename(repo.root)
57 dirname = os.path.basename(repo.root)
56 if dirname == "":
58 if dirname == "":
57 dirname = "root"
59 dirname = "root"
58 dirname = '%s.%s' % (dirname, short(node))
60 if node is not None:
61 dirname = '%s.%s' % (dirname, short(node))
59 base = os.path.join(tmproot, dirname)
62 base = os.path.join(tmproot, dirname)
60 os.mkdir(base)
63 os.mkdir(base)
61 ui.note(_('making snapshot of %d files from rev %s\n') %
64 if node is not None:
62 (len(files), short(node)))
65 ui.note(_('making snapshot of %d files from rev %s\n') %
66 (len(files), short(node)))
67 else:
68 ui.note(_('making snapshot of %d files from working dir\n') %
69 (len(files)))
70
71 fns_and_mtime = []
63 ctx = repo[node]
72 ctx = repo[node]
64 for fn in files:
73 for fn in files:
65 wfn = util.pconvert(fn)
74 wfn = util.pconvert(fn)
66 if not wfn in ctx:
75 if not wfn in ctx:
67 # skipping new file after a merge ?
76 # skipping new file after a merge ?
68 continue
77 continue
69 ui.note(' %s\n' % wfn)
78 ui.note(' %s\n' % wfn)
70 dest = os.path.join(base, wfn)
79 dest = os.path.join(base, wfn)
71 destdir = os.path.dirname(dest)
80 destdir = os.path.dirname(dest)
72 if not os.path.isdir(destdir):
81 if not os.path.isdir(destdir):
73 os.makedirs(destdir)
82 os.makedirs(destdir)
74 data = repo.wwritedata(wfn, ctx[wfn].data())
83 data = repo.wwritedata(wfn, ctx[wfn].data())
75 open(dest, 'wb').write(data)
84 open(dest, 'wb').write(data)
76 return dirname
85 if node is None:
77
86 fns_and_mtime.append((dest, repo.wjoin(fn), os.path.getmtime(dest)))
78
79 def snapshot_wdir(ui, repo, files, tmproot):
80 '''snapshot files from working directory.
81 if not using snapshot, -I/-X does not work and recursive diff
82 in tools like kdiff3 and meld displays too many files.'''
83 dirname = os.path.basename(repo.root)
84 if dirname == "":
85 dirname = "root"
86 base = os.path.join(tmproot, dirname)
87 os.mkdir(base)
88 ui.note(_('making snapshot of %d files from working dir\n') %
89 (len(files)))
90
91 fns_and_mtime = []
92
93 for fn in files:
94 wfn = util.pconvert(fn)
95 ui.note(' %s\n' % wfn)
96 dest = os.path.join(base, wfn)
97 destdir = os.path.dirname(dest)
98 if not os.path.isdir(destdir):
99 os.makedirs(destdir)
100
101 fp = open(dest, 'wb')
102 for chunk in util.filechunkiter(repo.wopener(wfn)):
103 fp.write(chunk)
104 fp.close()
105
106 fns_and_mtime.append((dest, repo.wjoin(fn), os.path.getmtime(dest)))
107
108
109 return dirname, fns_and_mtime
87 return dirname, fns_and_mtime
110
88
111
112 def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
89 def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
113 '''Do the actuall diff:
90 '''Do the actuall diff:
114
91
115 - copy to a temp structure if diffing 2 internal revisions
92 - copy to a temp structure if diffing 2 internal revisions
116 - copy to a temp structure if diffing working revision with
93 - copy to a temp structure if diffing working revision with
117 another one and more than 1 file is changed
94 another one and more than 1 file is changed
118 - just invoke the diff for a single file in the working dir
95 - just invoke the diff for a single file in the working dir
119 '''
96 '''
120
97
121 revs = opts.get('rev')
98 revs = opts.get('rev')
122 change = opts.get('change')
99 change = opts.get('change')
123
100
124 if revs and change:
101 if revs and change:
125 msg = _('cannot specify --rev and --change at the same time')
102 msg = _('cannot specify --rev and --change at the same time')
126 raise util.Abort(msg)
103 raise util.Abort(msg)
127 elif change:
104 elif change:
128 node2 = repo.lookup(change)
105 node2 = repo.lookup(change)
129 node1 = repo[node2].parents()[0].node()
106 node1 = repo[node2].parents()[0].node()
130 else:
107 else:
131 node1, node2 = cmdutil.revpair(repo, revs)
108 node1, node2 = cmdutil.revpair(repo, revs)
132
109
133 matcher = cmdutil.match(repo, pats, opts)
110 matcher = cmdutil.match(repo, pats, opts)
134 modified, added, removed = repo.status(node1, node2, matcher)[:3]
111 modified, added, removed = repo.status(node1, node2, matcher)[:3]
135 if not (modified or added or removed):
112 if not (modified or added or removed):
136 return 0
113 return 0
137
114
138 tmproot = tempfile.mkdtemp(prefix='extdiff.')
115 tmproot = tempfile.mkdtemp(prefix='extdiff.')
139 dir2root = ''
116 dir2root = ''
140 try:
117 try:
141 # Always make a copy of node1
118 # Always make a copy of node1
142 dir1 = snapshot_node(ui, repo, modified + removed, node1, tmproot)
119 dir1 = snapshot(ui, repo, modified + removed, node1, tmproot)[0]
143 changes = len(modified) + len(removed) + len(added)
120 changes = len(modified) + len(removed) + len(added)
144
121
145 fns_and_mtime = []
146
147 # If node2 in not the wc or there is >1 change, copy it
122 # If node2 in not the wc or there is >1 change, copy it
148 if node2:
123 if node2 or changes > 1:
149 dir2 = snapshot_node(ui, repo, modified + added, node2, tmproot)
124 dir2, fns_and_mtime = snapshot(ui, repo, modified + added, node2, tmproot)
150 elif changes > 1:
151 #we only actually need to get the files to copy back to the working
152 #dir in this case (because the other cases are: diffing 2 revisions
153 #or single file -- in which case the file is already directly passed
154 #to the diff tool).
155 dir2, fns_and_mtime = snapshot_wdir(ui, repo, modified + added, tmproot)
156 else:
125 else:
157 # This lets the diff tool open the changed file directly
126 # This lets the diff tool open the changed file directly
158 dir2 = ''
127 dir2 = ''
159 dir2root = repo.root
128 dir2root = repo.root
129 fns_and_mtime = []
160
130
161 # If only one change, diff the files instead of the directories
131 # If only one change, diff the files instead of the directories
162 if changes == 1 :
132 if changes == 1 :
163 if len(modified):
133 if len(modified):
164 dir1 = os.path.join(dir1, util.localpath(modified[0]))
134 dir1 = os.path.join(dir1, util.localpath(modified[0]))
165 dir2 = os.path.join(dir2root, dir2, util.localpath(modified[0]))
135 dir2 = os.path.join(dir2root, dir2, util.localpath(modified[0]))
166 elif len(removed) :
136 elif len(removed) :
167 dir1 = os.path.join(dir1, util.localpath(removed[0]))
137 dir1 = os.path.join(dir1, util.localpath(removed[0]))
168 dir2 = os.devnull
138 dir2 = os.devnull
169 else:
139 else:
170 dir1 = os.devnull
140 dir1 = os.devnull
171 dir2 = os.path.join(dir2root, dir2, util.localpath(added[0]))
141 dir2 = os.path.join(dir2root, dir2, util.localpath(added[0]))
172
142
173 cmdline = ('%s %s %s %s' %
143 cmdline = ('%s %s %s %s' %
174 (util.shellquote(diffcmd), ' '.join(diffopts),
144 (util.shellquote(diffcmd), ' '.join(diffopts),
175 util.shellquote(dir1), util.shellquote(dir2)))
145 util.shellquote(dir1), util.shellquote(dir2)))
176 ui.debug(_('running %r in %s\n') % (cmdline, tmproot))
146 ui.debug(_('running %r in %s\n') % (cmdline, tmproot))
177 util.system(cmdline, cwd=tmproot)
147 util.system(cmdline, cwd=tmproot)
178
148
179 for copy_fn, working_fn, mtime in fns_and_mtime:
149 for copy_fn, working_fn, mtime in fns_and_mtime:
180 if os.path.getmtime(copy_fn) != mtime:
150 if os.path.getmtime(copy_fn) != mtime:
181 ui.debug(_('file changed while diffing. '
151 ui.debug(_('file changed while diffing. '
182 'Overwriting: %s (src: %s)\n') % (working_fn, copy_fn))
152 'Overwriting: %s (src: %s)\n') % (working_fn, copy_fn))
183 util.copyfile(copy_fn, working_fn)
153 util.copyfile(copy_fn, working_fn)
184
154
185 return 1
155 return 1
186 finally:
156 finally:
187 ui.note(_('cleaning up temp directory\n'))
157 ui.note(_('cleaning up temp directory\n'))
188 shutil.rmtree(tmproot)
158 shutil.rmtree(tmproot)
189
159
190 def extdiff(ui, repo, *pats, **opts):
160 def extdiff(ui, repo, *pats, **opts):
191 '''use external program to diff repository (or selected files)
161 '''use external program to diff repository (or selected files)
192
162
193 Show differences between revisions for the specified files, using
163 Show differences between revisions for the specified files, using
194 an external program. The default program used is diff, with
164 an external program. The default program used is diff, with
195 default options "-Npru".
165 default options "-Npru".
196
166
197 To select a different program, use the -p option. The program
167 To select a different program, use the -p option. The program
198 will be passed the names of two directories to compare. To pass
168 will be passed the names of two directories to compare. To pass
199 additional options to the program, use the -o option. These will
169 additional options to the program, use the -o option. These will
200 be passed before the names of the directories to compare.
170 be passed before the names of the directories to compare.
201
171
202 When two revision arguments are given, then changes are
172 When two revision arguments are given, then changes are
203 shown between those revisions. If only one revision is
173 shown between those revisions. If only one revision is
204 specified then that revision is compared to the working
174 specified then that revision is compared to the working
205 directory, and, when no revisions are specified, the
175 directory, and, when no revisions are specified, the
206 working directory files are compared to its parent.'''
176 working directory files are compared to its parent.'''
207 program = opts['program'] or 'diff'
177 program = opts['program'] or 'diff'
208 if opts['program']:
178 if opts['program']:
209 option = opts['option']
179 option = opts['option']
210 else:
180 else:
211 option = opts['option'] or ['-Npru']
181 option = opts['option'] or ['-Npru']
212 return dodiff(ui, repo, program, option, pats, opts)
182 return dodiff(ui, repo, program, option, pats, opts)
213
183
214 cmdtable = {
184 cmdtable = {
215 "extdiff":
185 "extdiff":
216 (extdiff,
186 (extdiff,
217 [('p', 'program', '', _('comparison program to run')),
187 [('p', 'program', '', _('comparison program to run')),
218 ('o', 'option', [], _('pass option to comparison program')),
188 ('o', 'option', [], _('pass option to comparison program')),
219 ('r', 'rev', [], _('revision')),
189 ('r', 'rev', [], _('revision')),
220 ('c', 'change', '', _('change made by revision')),
190 ('c', 'change', '', _('change made by revision')),
221 ] + commands.walkopts,
191 ] + commands.walkopts,
222 _('hg extdiff [OPT]... [FILE]...')),
192 _('hg extdiff [OPT]... [FILE]...')),
223 }
193 }
224
194
225 def uisetup(ui):
195 def uisetup(ui):
226 for cmd, path in ui.configitems('extdiff'):
196 for cmd, path in ui.configitems('extdiff'):
227 if cmd.startswith('cmd.'):
197 if cmd.startswith('cmd.'):
228 cmd = cmd[4:]
198 cmd = cmd[4:]
229 if not path: path = cmd
199 if not path: path = cmd
230 diffopts = ui.config('extdiff', 'opts.' + cmd, '')
200 diffopts = ui.config('extdiff', 'opts.' + cmd, '')
231 diffopts = diffopts and [diffopts] or []
201 diffopts = diffopts and [diffopts] or []
232 elif cmd.startswith('opts.'):
202 elif cmd.startswith('opts.'):
233 continue
203 continue
234 else:
204 else:
235 # command = path opts
205 # command = path opts
236 if path:
206 if path:
237 diffopts = shlex.split(path)
207 diffopts = shlex.split(path)
238 path = diffopts.pop(0)
208 path = diffopts.pop(0)
239 else:
209 else:
240 path, diffopts = cmd, []
210 path, diffopts = cmd, []
241 def save(cmd, path, diffopts):
211 def save(cmd, path, diffopts):
242 '''use closure to save diff command to use'''
212 '''use closure to save diff command to use'''
243 def mydiff(ui, repo, *pats, **opts):
213 def mydiff(ui, repo, *pats, **opts):
244 return dodiff(ui, repo, path, diffopts, pats, opts)
214 return dodiff(ui, repo, path, diffopts, pats, opts)
245 mydiff.__doc__ = '''use %(path)s to diff repository (or selected files)
215 mydiff.__doc__ = '''use %(path)s to diff repository (or selected files)
246
216
247 Show differences between revisions for the specified
217 Show differences between revisions for the specified
248 files, using the %(path)s program.
218 files, using the %(path)s program.
249
219
250 When two revision arguments are given, then changes are
220 When two revision arguments are given, then changes are
251 shown between those revisions. If only one revision is
221 shown between those revisions. If only one revision is
252 specified then that revision is compared to the working
222 specified then that revision is compared to the working
253 directory, and, when no revisions are specified, the
223 directory, and, when no revisions are specified, the
254 working directory files are compared to its parent.''' % {
224 working directory files are compared to its parent.''' % {
255 'path': util.uirepr(path),
225 'path': util.uirepr(path),
256 }
226 }
257 return mydiff
227 return mydiff
258 cmdtable[cmd] = (save(cmd, path, diffopts),
228 cmdtable[cmd] = (save(cmd, path, diffopts),
259 cmdtable['extdiff'][1][1:],
229 cmdtable['extdiff'][1][1:],
260 _('hg %s [OPTION]... [FILE]...') % cmd)
230 _('hg %s [OPTION]... [FILE]...') % cmd)
General Comments 0
You need to be logged in to leave comments. Login now