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