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