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