Show More
@@ -1,219 +1,251 b'' | |||||
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 * |
|
49 | from mercurial.node import * | |
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_node(ui, repo, files, node, tmproot): | |
54 | '''snapshot files as of some revision''' |
|
54 | '''snapshot files as of some revision''' | |
55 | mf = repo.changectx(node).manifest() |
|
55 | mf = repo.changectx(node).manifest() | |
56 | dirname = os.path.basename(repo.root) |
|
56 | dirname = os.path.basename(repo.root) | |
57 | if dirname == "": |
|
57 | if dirname == "": | |
58 | dirname = "root" |
|
58 | dirname = "root" | |
59 | dirname = '%s.%s' % (dirname, short(node)) |
|
59 | dirname = '%s.%s' % (dirname, short(node)) | |
60 | base = os.path.join(tmproot, dirname) |
|
60 | base = os.path.join(tmproot, dirname) | |
61 | os.mkdir(base) |
|
61 | os.mkdir(base) | |
62 | ui.note(_('making snapshot of %d files from rev %s\n') % |
|
62 | ui.note(_('making snapshot of %d files from rev %s\n') % | |
63 | (len(files), short(node))) |
|
63 | (len(files), short(node))) | |
64 | for fn in files: |
|
64 | for fn in files: | |
65 | if not fn in mf: |
|
65 | if not fn in mf: | |
66 | # skipping new file after a merge ? |
|
66 | # skipping new file after a merge ? | |
67 | continue |
|
67 | continue | |
68 | wfn = util.pconvert(fn) |
|
68 | wfn = util.pconvert(fn) | |
69 | ui.note(' %s\n' % wfn) |
|
69 | ui.note(' %s\n' % wfn) | |
70 | dest = os.path.join(base, wfn) |
|
70 | dest = os.path.join(base, wfn) | |
71 | destdir = os.path.dirname(dest) |
|
71 | destdir = os.path.dirname(dest) | |
72 | if not os.path.isdir(destdir): |
|
72 | if not os.path.isdir(destdir): | |
73 | os.makedirs(destdir) |
|
73 | os.makedirs(destdir) | |
74 | data = repo.wwritedata(wfn, repo.file(wfn).read(mf[wfn])) |
|
74 | data = repo.wwritedata(wfn, repo.file(wfn).read(mf[wfn])) | |
75 | open(dest, 'wb').write(data) |
|
75 | open(dest, 'wb').write(data) | |
76 | return dirname |
|
76 | return dirname | |
77 |
|
77 | |||
78 |
|
78 | |||
79 | def snapshot_wdir(ui, repo, files, tmproot): |
|
79 | def snapshot_wdir(ui, repo, files, tmproot): | |
80 | '''snapshot files from working directory. |
|
80 | '''snapshot files from working directory. | |
81 | if not using snapshot, -I/-X does not work and recursive diff |
|
81 | if not using snapshot, -I/-X does not work and recursive diff | |
82 | in tools like kdiff3 and meld displays too many files.''' |
|
82 | in tools like kdiff3 and meld displays too many files.''' | |
83 | dirname = os.path.basename(repo.root) |
|
83 | repo_root = repo.root | |
|
84 | ||||
|
85 | dirname = os.path.basename(repo_root) | |||
84 | if dirname == "": |
|
86 | if dirname == "": | |
85 | dirname = "root" |
|
87 | dirname = "root" | |
86 | base = os.path.join(tmproot, dirname) |
|
88 | base = os.path.join(tmproot, dirname) | |
87 | os.mkdir(base) |
|
89 | os.mkdir(base) | |
88 | ui.note(_('making snapshot of %d files from working dir\n') % |
|
90 | ui.note(_('making snapshot of %d files from working dir\n') % | |
89 | (len(files))) |
|
91 | (len(files))) | |
|
92 | ||||
|
93 | fns_and_mtime = [] | |||
|
94 | ||||
90 | for fn in files: |
|
95 | for fn in files: | |
91 | wfn = util.pconvert(fn) |
|
96 | wfn = util.pconvert(fn) | |
92 | ui.note(' %s\n' % wfn) |
|
97 | ui.note(' %s\n' % wfn) | |
93 | dest = os.path.join(base, wfn) |
|
98 | dest = os.path.join(base, wfn) | |
94 | destdir = os.path.dirname(dest) |
|
99 | destdir = os.path.dirname(dest) | |
95 | if not os.path.isdir(destdir): |
|
100 | if not os.path.isdir(destdir): | |
96 | os.makedirs(destdir) |
|
101 | os.makedirs(destdir) | |
|
102 | ||||
97 | fp = open(dest, 'wb') |
|
103 | fp = open(dest, 'wb') | |
98 | for chunk in util.filechunkiter(repo.wopener(wfn)): |
|
104 | for chunk in util.filechunkiter(repo.wopener(wfn)): | |
99 | fp.write(chunk) |
|
105 | fp.write(chunk) | |
100 | return dirname |
|
106 | fp.close() | |
|
107 | ||||
|
108 | fns_and_mtime.append((dest, os.path.join(repo_root, fn), | |||
|
109 | os.path.getmtime(dest))) | |||
|
110 | ||||
|
111 | ||||
|
112 | return dirname, fns_and_mtime | |||
101 |
|
113 | |||
102 |
|
114 | |||
103 | def dodiff(ui, repo, diffcmd, diffopts, pats, opts): |
|
115 | def dodiff(ui, repo, diffcmd, diffopts, pats, opts): | |
|
116 | '''Do the actuall diff: | |||
|
117 | ||||
|
118 | - copy to a temp structure if diffing 2 internal revisions | |||
|
119 | - copy to a temp structure if diffing working revision with | |||
|
120 | another one and more than 1 file is changed | |||
|
121 | - just invoke the diff for a single file in the working dir | |||
|
122 | ''' | |||
104 | node1, node2 = cmdutil.revpair(repo, opts['rev']) |
|
123 | node1, node2 = cmdutil.revpair(repo, opts['rev']) | |
105 | files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts) |
|
124 | files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts) | |
106 | modified, added, removed, deleted, unknown = repo.status( |
|
125 | modified, added, removed, deleted, unknown = repo.status( | |
107 | node1, node2, files, match=matchfn)[:5] |
|
126 | node1, node2, files, match=matchfn)[:5] | |
108 | if not (modified or added or removed): |
|
127 | if not (modified or added or removed): | |
109 | return 0 |
|
128 | return 0 | |
110 |
|
129 | |||
111 | tmproot = tempfile.mkdtemp(prefix='extdiff.') |
|
130 | tmproot = tempfile.mkdtemp(prefix='extdiff.') | |
112 | dir2root = '' |
|
131 | dir2root = '' | |
113 | try: |
|
132 | try: | |
114 | # Always make a copy of node1 |
|
133 | # Always make a copy of node1 | |
115 | dir1 = snapshot_node(ui, repo, modified + removed, node1, tmproot) |
|
134 | dir1 = snapshot_node(ui, repo, modified + removed, node1, tmproot) | |
116 | changes = len(modified) + len(removed) + len(added) |
|
135 | changes = len(modified) + len(removed) + len(added) | |
117 |
|
136 | |||
|
137 | fns_and_mtime = [] | |||
|
138 | ||||
118 | # If node2 in not the wc or there is >1 change, copy it |
|
139 | # If node2 in not the wc or there is >1 change, copy it | |
119 | if node2: |
|
140 | if node2: | |
120 | dir2 = snapshot_node(ui, repo, modified + added, node2, tmproot) |
|
141 | dir2 = snapshot_node(ui, repo, modified + added, node2, tmproot) | |
121 | elif changes > 1: |
|
142 | elif changes > 1: | |
122 | dir2 = snapshot_wdir(ui, repo, modified + added, tmproot) |
|
143 | #we only actually need to get the files to copy back to the working | |
|
144 | #dir in this case (because the other cases are: diffing 2 revisions | |||
|
145 | #or single file -- in which case the file is already directly passed | |||
|
146 | #to the diff tool). | |||
|
147 | dir2, fns_and_mtime = snapshot_wdir(ui, repo, modified + added, tmproot) | |||
123 | else: |
|
148 | else: | |
124 | # This lets the diff tool open the changed file directly |
|
149 | # This lets the diff tool open the changed file directly | |
125 | dir2 = '' |
|
150 | dir2 = '' | |
126 | dir2root = repo.root |
|
151 | dir2root = repo.root | |
127 |
|
152 | |||
128 | # If only one change, diff the files instead of the directories |
|
153 | # If only one change, diff the files instead of the directories | |
129 | if changes == 1 : |
|
154 | if changes == 1 : | |
130 | if len(modified): |
|
155 | if len(modified): | |
131 | dir1 = os.path.join(dir1, util.localpath(modified[0])) |
|
156 | dir1 = os.path.join(dir1, util.localpath(modified[0])) | |
132 | dir2 = os.path.join(dir2root, dir2, util.localpath(modified[0])) |
|
157 | dir2 = os.path.join(dir2root, dir2, util.localpath(modified[0])) | |
133 | elif len(removed) : |
|
158 | elif len(removed) : | |
134 | dir1 = os.path.join(dir1, util.localpath(removed[0])) |
|
159 | dir1 = os.path.join(dir1, util.localpath(removed[0])) | |
135 | dir2 = os.devnull |
|
160 | dir2 = os.devnull | |
136 | else: |
|
161 | else: | |
137 | dir1 = os.devnull |
|
162 | dir1 = os.devnull | |
138 | dir2 = os.path.join(dir2root, dir2, util.localpath(added[0])) |
|
163 | dir2 = os.path.join(dir2root, dir2, util.localpath(added[0])) | |
139 |
|
164 | |||
140 | cmdline = ('%s %s %s %s' % |
|
165 | cmdline = ('%s %s %s %s' % | |
141 | (util.shellquote(diffcmd), ' '.join(diffopts), |
|
166 | (util.shellquote(diffcmd), ' '.join(diffopts), | |
142 | util.shellquote(dir1), util.shellquote(dir2))) |
|
167 | util.shellquote(dir1), util.shellquote(dir2))) | |
143 | ui.debug('running %r in %s\n' % (cmdline, tmproot)) |
|
168 | ui.debug('running %r in %s\n' % (cmdline, tmproot)) | |
144 | util.system(cmdline, cwd=tmproot) |
|
169 | util.system(cmdline, cwd=tmproot) | |
|
170 | ||||
|
171 | for copy_fn, working_fn, mtime in fns_and_mtime: | |||
|
172 | if os.path.getmtime(copy_fn) != mtime: | |||
|
173 | ui.debug('File changed while diffing. ' | |||
|
174 | 'Overwriting: %s (src: %s)\n' % (working_fn, copy_fn)) | |||
|
175 | util.copyfile(copy_fn, working_fn) | |||
|
176 | ||||
145 | return 1 |
|
177 | return 1 | |
146 | finally: |
|
178 | finally: | |
147 | ui.note(_('cleaning up temp directory\n')) |
|
179 | ui.note(_('cleaning up temp directory\n')) | |
148 | shutil.rmtree(tmproot) |
|
180 | shutil.rmtree(tmproot) | |
149 |
|
181 | |||
150 | def extdiff(ui, repo, *pats, **opts): |
|
182 | def extdiff(ui, repo, *pats, **opts): | |
151 | '''use external program to diff repository (or selected files) |
|
183 | '''use external program to diff repository (or selected files) | |
152 |
|
184 | |||
153 | Show differences between revisions for the specified files, using |
|
185 | Show differences between revisions for the specified files, using | |
154 | an external program. The default program used is diff, with |
|
186 | an external program. The default program used is diff, with | |
155 | default options "-Npru". |
|
187 | default options "-Npru". | |
156 |
|
188 | |||
157 | To select a different program, use the -p option. The program |
|
189 | To select a different program, use the -p option. The program | |
158 | will be passed the names of two directories to compare. To pass |
|
190 | will be passed the names of two directories to compare. To pass | |
159 | additional options to the program, use the -o option. These will |
|
191 | additional options to the program, use the -o option. These will | |
160 | be passed before the names of the directories to compare. |
|
192 | be passed before the names of the directories to compare. | |
161 |
|
193 | |||
162 | When two revision arguments are given, then changes are |
|
194 | When two revision arguments are given, then changes are | |
163 | shown between those revisions. If only one revision is |
|
195 | shown between those revisions. If only one revision is | |
164 | specified then that revision is compared to the working |
|
196 | specified then that revision is compared to the working | |
165 | directory, and, when no revisions are specified, the |
|
197 | directory, and, when no revisions are specified, the | |
166 | working directory files are compared to its parent.''' |
|
198 | working directory files are compared to its parent.''' | |
167 | program = opts['program'] or 'diff' |
|
199 | program = opts['program'] or 'diff' | |
168 | if opts['program']: |
|
200 | if opts['program']: | |
169 | option = opts['option'] |
|
201 | option = opts['option'] | |
170 | else: |
|
202 | else: | |
171 | option = opts['option'] or ['-Npru'] |
|
203 | option = opts['option'] or ['-Npru'] | |
172 | return dodiff(ui, repo, program, option, pats, opts) |
|
204 | return dodiff(ui, repo, program, option, pats, opts) | |
173 |
|
205 | |||
174 | cmdtable = { |
|
206 | cmdtable = { | |
175 | "extdiff": |
|
207 | "extdiff": | |
176 | (extdiff, |
|
208 | (extdiff, | |
177 | [('p', 'program', '', _('comparison program to run')), |
|
209 | [('p', 'program', '', _('comparison program to run')), | |
178 | ('o', 'option', [], _('pass option to comparison program')), |
|
210 | ('o', 'option', [], _('pass option to comparison program')), | |
179 | ('r', 'rev', [], _('revision')), |
|
211 | ('r', 'rev', [], _('revision')), | |
180 | ] + commands.walkopts, |
|
212 | ] + commands.walkopts, | |
181 | _('hg extdiff [OPT]... [FILE]...')), |
|
213 | _('hg extdiff [OPT]... [FILE]...')), | |
182 | } |
|
214 | } | |
183 |
|
215 | |||
184 | def uisetup(ui): |
|
216 | def uisetup(ui): | |
185 | for cmd, path in ui.configitems('extdiff'): |
|
217 | for cmd, path in ui.configitems('extdiff'): | |
186 | if cmd.startswith('cmd.'): |
|
218 | if cmd.startswith('cmd.'): | |
187 | cmd = cmd[4:] |
|
219 | cmd = cmd[4:] | |
188 | if not path: path = cmd |
|
220 | if not path: path = cmd | |
189 | diffopts = ui.config('extdiff', 'opts.' + cmd, '') |
|
221 | diffopts = ui.config('extdiff', 'opts.' + cmd, '') | |
190 | diffopts = diffopts and [diffopts] or [] |
|
222 | diffopts = diffopts and [diffopts] or [] | |
191 | elif cmd.startswith('opts.'): |
|
223 | elif cmd.startswith('opts.'): | |
192 | continue |
|
224 | continue | |
193 | else: |
|
225 | else: | |
194 | # command = path opts |
|
226 | # command = path opts | |
195 | if path: |
|
227 | if path: | |
196 | diffopts = shlex.split(path) |
|
228 | diffopts = shlex.split(path) | |
197 | path = diffopts.pop(0) |
|
229 | path = diffopts.pop(0) | |
198 | else: |
|
230 | else: | |
199 | path, diffopts = cmd, [] |
|
231 | path, diffopts = cmd, [] | |
200 | def save(cmd, path, diffopts): |
|
232 | def save(cmd, path, diffopts): | |
201 | '''use closure to save diff command to use''' |
|
233 | '''use closure to save diff command to use''' | |
202 | def mydiff(ui, repo, *pats, **opts): |
|
234 | def mydiff(ui, repo, *pats, **opts): | |
203 | return dodiff(ui, repo, path, diffopts, pats, opts) |
|
235 | return dodiff(ui, repo, path, diffopts, pats, opts) | |
204 | mydiff.__doc__ = '''use %(path)s to diff repository (or selected files) |
|
236 | mydiff.__doc__ = '''use %(path)s to diff repository (or selected files) | |
205 |
|
237 | |||
206 | Show differences between revisions for the specified |
|
238 | Show differences between revisions for the specified | |
207 | files, using the %(path)s program. |
|
239 | files, using the %(path)s program. | |
208 |
|
240 | |||
209 | When two revision arguments are given, then changes are |
|
241 | When two revision arguments are given, then changes are | |
210 | shown between those revisions. If only one revision is |
|
242 | shown between those revisions. If only one revision is | |
211 | specified then that revision is compared to the working |
|
243 | specified then that revision is compared to the working | |
212 | directory, and, when no revisions are specified, the |
|
244 | directory, and, when no revisions are specified, the | |
213 | working directory files are compared to its parent.''' % { |
|
245 | working directory files are compared to its parent.''' % { | |
214 | 'path': util.uirepr(path), |
|
246 | 'path': util.uirepr(path), | |
215 | } |
|
247 | } | |
216 | return mydiff |
|
248 | return mydiff | |
217 | cmdtable[cmd] = (save(cmd, path, diffopts), |
|
249 | cmdtable[cmd] = (save(cmd, path, diffopts), | |
218 | cmdtable['extdiff'][1][1:], |
|
250 | cmdtable['extdiff'][1][1:], | |
219 | _('hg %s [OPTION]... [FILE]...') % cmd) |
|
251 | _('hg %s [OPTION]... [FILE]...') % cmd) |
General Comments 0
You need to be logged in to leave comments.
Login now