Show More
@@ -1,214 +1,219 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 | # The `extdiff' Mercurial extension allows you to use external programs |
|
8 | ''' | |
9 | # to compare revisions, or revision with working dir. The external diff |
|
9 | The `extdiff' Mercurial extension allows you to use external programs | |
10 | # programs are called with a configurable set of options and two |
|
10 | to compare revisions, or revision with working dir. The external diff | |
11 | # non-option arguments: paths to directories containing snapshots of |
|
11 | programs are called with a configurable set of options and two | |
12 | # files to compare. |
|
12 | non-option arguments: paths to directories containing snapshots of | |
13 | # |
|
13 | files to compare. | |
14 | # To enable this extension: |
|
14 | ||
15 | # |
|
15 | To enable this extension: | |
16 | # [extensions] |
|
16 | ||
17 | # hgext.extdiff = |
|
17 | [extensions] | |
18 | # |
|
18 | hgext.extdiff = | |
19 | # The `extdiff' extension also allows to configure new diff commands, so |
|
19 | ||
20 | # you do not need to type "hg extdiff -p kdiff3" always. |
|
20 | The `extdiff' extension also allows to configure new diff commands, so | |
21 | # |
|
21 | you do not need to type "hg extdiff -p kdiff3" always. | |
22 | # [extdiff] |
|
|||
23 | # # add new command that runs GNU diff(1) in 'context diff' mode |
|
|||
24 | # cmd.cdiff = gdiff |
|
|||
25 | # opts.cdiff = -Nprc5 |
|
|||
26 |
|
|
22 | ||
27 | # # add new command called vdiff, runs kdiff3 |
|
23 | [extdiff] | |
28 | # cmd.vdiff = kdiff3 |
|
24 | # add new command that runs GNU diff(1) in 'context diff' mode | |
|
25 | cdiff = gdiff -Nprc5 | |||
|
26 | ## or the old way: | |||
|
27 | #cmd.cdiff = gdiff | |||
|
28 | #opts.cdiff = -Nprc5 | |||
29 |
|
|
29 | ||
30 |
|
|
30 | # add new command called vdiff, runs kdiff3 | |
31 | # cmd.meld = |
|
31 | vdiff = kdiff3 | |
32 |
|
|
32 | ||
33 | # # add new command called vimdiff, runs gvimdiff with DirDiff plugin |
|
33 | # add new command called meld, runs meld (no need to name twice) | |
34 | # #(see http://www.vim.org/scripts/script.php?script_id=102) |
|
34 | meld = | |
35 | # # Non english user, be sure to put "let g:DirDiffDynamicDiffText = 1" in |
|
35 | ||
36 | # # your .vimrc |
|
36 | # add new command called vimdiff, runs gvimdiff with DirDiff plugin | |
37 | # cmd.vimdiff = gvim |
|
37 | #(see http://www.vim.org/scripts/script.php?script_id=102) | |
38 | # opts.vimdiff = -f '+next' '+execute "DirDiff" argv(0) argv(1)' |
|
38 | # Non english user, be sure to put "let g:DirDiffDynamicDiffText = 1" in | |
39 | # |
|
39 | # your .vimrc | |
40 | # Each custom diff commands can have two parts: a `cmd' and an `opts' |
|
40 | vimdiff = gvim -f '+next' '+execute "DirDiff" argv(0) argv(1)' | |
41 | # part. The cmd.xxx option defines the name of an executable program |
|
41 | ||
42 | # that will be run, and opts.xxx defines a set of command-line options |
|
42 | You can use -I/-X and list of file or directory names like normal | |
43 | # which will be inserted to the command between the program name and |
|
43 | "hg diff" command. The `extdiff' extension makes snapshots of only | |
44 | # the files/directories to diff (i.e. the cdiff example above). |
|
44 | needed files, so running the external diff program will actually be | |
45 | # |
|
45 | pretty fast (at least faster than having to compare the entire tree). | |
46 | # You can use -I/-X and list of file or directory names like normal |
|
46 | ''' | |
47 | # "hg diff" command. The `extdiff' extension makes snapshots of only |
|
|||
48 | # needed files, so running the external diff program will actually be |
|
|||
49 | # pretty fast (at least faster than having to compare the entire tree). |
|
|||
50 |
|
47 | |||
51 | from mercurial.i18n import _ |
|
48 | from mercurial.i18n import _ | |
52 | from mercurial.node import * |
|
49 | from mercurial.node import * | |
53 | from mercurial import cmdutil, util, commands |
|
50 | from mercurial import cmdutil, util, commands | |
54 | import os, shutil, tempfile |
|
51 | import os, shlex, shutil, tempfile | |
55 |
|
||||
56 |
|
52 | |||
57 | def snapshot_node(ui, repo, files, node, tmproot): |
|
53 | def snapshot_node(ui, repo, files, node, tmproot): | |
58 | '''snapshot files as of some revision''' |
|
54 | '''snapshot files as of some revision''' | |
59 | mf = repo.changectx(node).manifest() |
|
55 | mf = repo.changectx(node).manifest() | |
60 | dirname = os.path.basename(repo.root) |
|
56 | dirname = os.path.basename(repo.root) | |
61 | if dirname == "": |
|
57 | if dirname == "": | |
62 | dirname = "root" |
|
58 | dirname = "root" | |
63 | dirname = '%s.%s' % (dirname, short(node)) |
|
59 | dirname = '%s.%s' % (dirname, short(node)) | |
64 | base = os.path.join(tmproot, dirname) |
|
60 | base = os.path.join(tmproot, dirname) | |
65 | os.mkdir(base) |
|
61 | os.mkdir(base) | |
66 | ui.note(_('making snapshot of %d files from rev %s\n') % |
|
62 | ui.note(_('making snapshot of %d files from rev %s\n') % | |
67 | (len(files), short(node))) |
|
63 | (len(files), short(node))) | |
68 | for fn in files: |
|
64 | for fn in files: | |
69 | if not fn in mf: |
|
65 | if not fn in mf: | |
70 | # skipping new file after a merge ? |
|
66 | # skipping new file after a merge ? | |
71 | continue |
|
67 | continue | |
72 | wfn = util.pconvert(fn) |
|
68 | wfn = util.pconvert(fn) | |
73 | ui.note(' %s\n' % wfn) |
|
69 | ui.note(' %s\n' % wfn) | |
74 | dest = os.path.join(base, wfn) |
|
70 | dest = os.path.join(base, wfn) | |
75 | destdir = os.path.dirname(dest) |
|
71 | destdir = os.path.dirname(dest) | |
76 | if not os.path.isdir(destdir): |
|
72 | if not os.path.isdir(destdir): | |
77 | os.makedirs(destdir) |
|
73 | os.makedirs(destdir) | |
78 | data = repo.wwritedata(wfn, repo.file(wfn).read(mf[wfn])) |
|
74 | data = repo.wwritedata(wfn, repo.file(wfn).read(mf[wfn])) | |
79 | open(dest, 'wb').write(data) |
|
75 | open(dest, 'wb').write(data) | |
80 | return dirname |
|
76 | return dirname | |
81 |
|
77 | |||
82 |
|
78 | |||
83 | def snapshot_wdir(ui, repo, files, tmproot): |
|
79 | def snapshot_wdir(ui, repo, files, tmproot): | |
84 | '''snapshot files from working directory. |
|
80 | '''snapshot files from working directory. | |
85 | 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 | |
86 | in tools like kdiff3 and meld displays too many files.''' |
|
82 | in tools like kdiff3 and meld displays too many files.''' | |
87 | dirname = os.path.basename(repo.root) |
|
83 | dirname = os.path.basename(repo.root) | |
88 | if dirname == "": |
|
84 | if dirname == "": | |
89 | dirname = "root" |
|
85 | dirname = "root" | |
90 | base = os.path.join(tmproot, dirname) |
|
86 | base = os.path.join(tmproot, dirname) | |
91 | os.mkdir(base) |
|
87 | os.mkdir(base) | |
92 | ui.note(_('making snapshot of %d files from working dir\n') % |
|
88 | ui.note(_('making snapshot of %d files from working dir\n') % | |
93 | (len(files))) |
|
89 | (len(files))) | |
94 | for fn in files: |
|
90 | for fn in files: | |
95 | wfn = util.pconvert(fn) |
|
91 | wfn = util.pconvert(fn) | |
96 | ui.note(' %s\n' % wfn) |
|
92 | ui.note(' %s\n' % wfn) | |
97 | dest = os.path.join(base, wfn) |
|
93 | dest = os.path.join(base, wfn) | |
98 | destdir = os.path.dirname(dest) |
|
94 | destdir = os.path.dirname(dest) | |
99 | if not os.path.isdir(destdir): |
|
95 | if not os.path.isdir(destdir): | |
100 | os.makedirs(destdir) |
|
96 | os.makedirs(destdir) | |
101 | fp = open(dest, 'wb') |
|
97 | fp = open(dest, 'wb') | |
102 | for chunk in util.filechunkiter(repo.wopener(wfn)): |
|
98 | for chunk in util.filechunkiter(repo.wopener(wfn)): | |
103 | fp.write(chunk) |
|
99 | fp.write(chunk) | |
104 | return dirname |
|
100 | return dirname | |
105 |
|
101 | |||
106 |
|
102 | |||
107 | def dodiff(ui, repo, diffcmd, diffopts, pats, opts): |
|
103 | def dodiff(ui, repo, diffcmd, diffopts, pats, opts): | |
108 | node1, node2 = cmdutil.revpair(repo, opts['rev']) |
|
104 | node1, node2 = cmdutil.revpair(repo, opts['rev']) | |
109 | files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts) |
|
105 | files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts) | |
110 | modified, added, removed, deleted, unknown = repo.status( |
|
106 | modified, added, removed, deleted, unknown = repo.status( | |
111 | node1, node2, files, match=matchfn)[:5] |
|
107 | node1, node2, files, match=matchfn)[:5] | |
112 | if not (modified or added or removed): |
|
108 | if not (modified or added or removed): | |
113 | return 0 |
|
109 | return 0 | |
114 |
|
110 | |||
115 | tmproot = tempfile.mkdtemp(prefix='extdiff.') |
|
111 | tmproot = tempfile.mkdtemp(prefix='extdiff.') | |
116 | dir2root = '' |
|
112 | dir2root = '' | |
117 | try: |
|
113 | try: | |
118 | # Always make a copy of node1 |
|
114 | # Always make a copy of node1 | |
119 | dir1 = snapshot_node(ui, repo, modified + removed, node1, tmproot) |
|
115 | dir1 = snapshot_node(ui, repo, modified + removed, node1, tmproot) | |
120 | changes = len(modified) + len(removed) + len(added) |
|
116 | changes = len(modified) + len(removed) + len(added) | |
121 |
|
117 | |||
122 | # If node2 in not the wc or there is >1 change, copy it |
|
118 | # If node2 in not the wc or there is >1 change, copy it | |
123 | if node2: |
|
119 | if node2: | |
124 | dir2 = snapshot_node(ui, repo, modified + added, node2, tmproot) |
|
120 | dir2 = snapshot_node(ui, repo, modified + added, node2, tmproot) | |
125 | elif changes > 1: |
|
121 | elif changes > 1: | |
126 | dir2 = snapshot_wdir(ui, repo, modified + added, tmproot) |
|
122 | dir2 = snapshot_wdir(ui, repo, modified + added, tmproot) | |
127 | else: |
|
123 | else: | |
128 | # This lets the diff tool open the changed file directly |
|
124 | # This lets the diff tool open the changed file directly | |
129 | dir2 = '' |
|
125 | dir2 = '' | |
130 | dir2root = repo.root |
|
126 | dir2root = repo.root | |
131 |
|
127 | |||
132 | # If only one change, diff the files instead of the directories |
|
128 | # If only one change, diff the files instead of the directories | |
133 | if changes == 1 : |
|
129 | if changes == 1 : | |
134 | if len(modified): |
|
130 | if len(modified): | |
135 | dir1 = os.path.join(dir1, util.localpath(modified[0])) |
|
131 | dir1 = os.path.join(dir1, util.localpath(modified[0])) | |
136 | dir2 = os.path.join(dir2root, dir2, util.localpath(modified[0])) |
|
132 | dir2 = os.path.join(dir2root, dir2, util.localpath(modified[0])) | |
137 | elif len(removed) : |
|
133 | elif len(removed) : | |
138 | dir1 = os.path.join(dir1, util.localpath(removed[0])) |
|
134 | dir1 = os.path.join(dir1, util.localpath(removed[0])) | |
139 | dir2 = os.devnull |
|
135 | dir2 = os.devnull | |
140 | else: |
|
136 | else: | |
141 | dir1 = os.devnull |
|
137 | dir1 = os.devnull | |
142 | dir2 = os.path.join(dir2root, dir2, util.localpath(added[0])) |
|
138 | dir2 = os.path.join(dir2root, dir2, util.localpath(added[0])) | |
143 |
|
139 | |||
144 | cmdline = ('%s %s %s %s' % |
|
140 | cmdline = ('%s %s %s %s' % | |
145 | (util.shellquote(diffcmd), ' '.join(diffopts), |
|
141 | (util.shellquote(diffcmd), ' '.join(diffopts), | |
146 | util.shellquote(dir1), util.shellquote(dir2))) |
|
142 | util.shellquote(dir1), util.shellquote(dir2))) | |
147 | ui.debug('running %r in %s\n' % (cmdline, tmproot)) |
|
143 | ui.debug('running %r in %s\n' % (cmdline, tmproot)) | |
148 | util.system(cmdline, cwd=tmproot) |
|
144 | util.system(cmdline, cwd=tmproot) | |
149 | return 1 |
|
145 | return 1 | |
150 | finally: |
|
146 | finally: | |
151 | ui.note(_('cleaning up temp directory\n')) |
|
147 | ui.note(_('cleaning up temp directory\n')) | |
152 | shutil.rmtree(tmproot) |
|
148 | shutil.rmtree(tmproot) | |
153 |
|
149 | |||
154 | def extdiff(ui, repo, *pats, **opts): |
|
150 | def extdiff(ui, repo, *pats, **opts): | |
155 | '''use external program to diff repository (or selected files) |
|
151 | '''use external program to diff repository (or selected files) | |
156 |
|
152 | |||
157 | Show differences between revisions for the specified files, using |
|
153 | Show differences between revisions for the specified files, using | |
158 | an external program. The default program used is diff, with |
|
154 | an external program. The default program used is diff, with | |
159 | default options "-Npru". |
|
155 | default options "-Npru". | |
160 |
|
156 | |||
161 | To select a different program, use the -p option. The program |
|
157 | To select a different program, use the -p option. The program | |
162 | will be passed the names of two directories to compare. To pass |
|
158 | will be passed the names of two directories to compare. To pass | |
163 | additional options to the program, use the -o option. These will |
|
159 | additional options to the program, use the -o option. These will | |
164 | be passed before the names of the directories to compare. |
|
160 | be passed before the names of the directories to compare. | |
165 |
|
161 | |||
166 | When two revision arguments are given, then changes are |
|
162 | When two revision arguments are given, then changes are | |
167 | shown between those revisions. If only one revision is |
|
163 | shown between those revisions. If only one revision is | |
168 | specified then that revision is compared to the working |
|
164 | specified then that revision is compared to the working | |
169 | directory, and, when no revisions are specified, the |
|
165 | directory, and, when no revisions are specified, the | |
170 | working directory files are compared to its parent.''' |
|
166 | working directory files are compared to its parent.''' | |
171 | program = opts['program'] or 'diff' |
|
167 | program = opts['program'] or 'diff' | |
172 | if opts['program']: |
|
168 | if opts['program']: | |
173 | option = opts['option'] |
|
169 | option = opts['option'] | |
174 | else: |
|
170 | else: | |
175 | option = opts['option'] or ['-Npru'] |
|
171 | option = opts['option'] or ['-Npru'] | |
176 | return dodiff(ui, repo, program, option, pats, opts) |
|
172 | return dodiff(ui, repo, program, option, pats, opts) | |
177 |
|
173 | |||
178 | cmdtable = { |
|
174 | cmdtable = { | |
179 | "extdiff": |
|
175 | "extdiff": | |
180 | (extdiff, |
|
176 | (extdiff, | |
181 | [('p', 'program', '', _('comparison program to run')), |
|
177 | [('p', 'program', '', _('comparison program to run')), | |
182 | ('o', 'option', [], _('pass option to comparison program')), |
|
178 | ('o', 'option', [], _('pass option to comparison program')), | |
183 | ('r', 'rev', [], _('revision')), |
|
179 | ('r', 'rev', [], _('revision')), | |
184 | ] + commands.walkopts, |
|
180 | ] + commands.walkopts, | |
185 | _('hg extdiff [OPT]... [FILE]...')), |
|
181 | _('hg extdiff [OPT]... [FILE]...')), | |
186 | } |
|
182 | } | |
187 |
|
183 | |||
188 | def uisetup(ui): |
|
184 | def uisetup(ui): | |
189 | for cmd, path in ui.configitems('extdiff'): |
|
185 | for cmd, path in ui.configitems('extdiff'): | |
190 |
if |
|
186 | if cmd.startswith('cmd.'): | |
191 | cmd = cmd[4:] |
|
187 | cmd = cmd[4:] | |
192 | if not path: path = cmd |
|
188 | if not path: path = cmd | |
193 | diffopts = ui.config('extdiff', 'opts.' + cmd, '') |
|
189 | diffopts = ui.config('extdiff', 'opts.' + cmd, '') | |
194 | diffopts = diffopts and [diffopts] or [] |
|
190 | diffopts = diffopts and [diffopts] or [] | |
|
191 | elif cmd.startswith('opts.'): | |||
|
192 | continue | |||
|
193 | else: | |||
|
194 | # command = path opts | |||
|
195 | if path: | |||
|
196 | diffopts = shlex.split(path) | |||
|
197 | path = diffopts.pop(0) | |||
|
198 | else: | |||
|
199 | path, diffopts = cmd, [] | |||
195 | def save(cmd, path, diffopts): |
|
200 | def save(cmd, path, diffopts): | |
196 | '''use closure to save diff command to use''' |
|
201 | '''use closure to save diff command to use''' | |
197 | def mydiff(ui, repo, *pats, **opts): |
|
202 | def mydiff(ui, repo, *pats, **opts): | |
198 | return dodiff(ui, repo, path, diffopts, pats, opts) |
|
203 | return dodiff(ui, repo, path, diffopts, pats, opts) | |
199 | mydiff.__doc__ = '''use %(path)r to diff repository (or selected files) |
|
204 | mydiff.__doc__ = '''use %(path)r to diff repository (or selected files) | |
200 |
|
205 | |||
201 | Show differences between revisions for the specified |
|
206 | Show differences between revisions for the specified | |
202 | files, using the %(path)r program. |
|
207 | files, using the %(path)r program. | |
203 |
|
208 | |||
204 | When two revision arguments are given, then changes are |
|
209 | When two revision arguments are given, then changes are | |
205 | shown between those revisions. If only one revision is |
|
210 | shown between those revisions. If only one revision is | |
206 | specified then that revision is compared to the working |
|
211 | specified then that revision is compared to the working | |
207 | directory, and, when no revisions are specified, the |
|
212 | directory, and, when no revisions are specified, the | |
208 | working directory files are compared to its parent.''' % { |
|
213 | working directory files are compared to its parent.''' % { | |
209 | 'path': path, |
|
214 | 'path': path, | |
210 | } |
|
215 | } | |
211 | return mydiff |
|
216 | return mydiff | |
212 | cmdtable[cmd] = (save(cmd, path, diffopts), |
|
217 | cmdtable[cmd] = (save(cmd, path, diffopts), | |
213 | cmdtable['extdiff'][1][1:], |
|
218 | cmdtable['extdiff'][1][1:], | |
214 | _('hg %s [OPTION]... [FILE]...') % cmd) |
|
219 | _('hg %s [OPTION]... [FILE]...') % cmd) |
General Comments 0
You need to be logged in to leave comments.
Login now