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