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