##// END OF EJS Templates
extdiff: use the default option only if the default program is used
TK Soh -
r3129:f145d048 default
parent child Browse files
Show More
@@ -1,180 +1,184 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
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.demandload import demandload
51 from mercurial.demandload import demandload
52 from mercurial.i18n import gettext as _
52 from mercurial.i18n import gettext as _
53 from mercurial.node import *
53 from mercurial.node import *
54 demandload(globals(), 'mercurial:cmdutil,util os shutil tempfile')
54 demandload(globals(), 'mercurial:cmdutil,util os shutil tempfile')
55
55
56 def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
56 def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
57 def snapshot_node(files, node):
57 def snapshot_node(files, node):
58 '''snapshot files as of some revision'''
58 '''snapshot files as of some revision'''
59 changes = repo.changelog.read(node)
59 changes = repo.changelog.read(node)
60 mf = repo.manifest.read(changes[0])
60 mf = repo.manifest.read(changes[0])
61 dirname = '%s.%s' % (os.path.basename(repo.root), short(node))
61 dirname = '%s.%s' % (os.path.basename(repo.root), short(node))
62 base = os.path.join(tmproot, dirname)
62 base = os.path.join(tmproot, dirname)
63 os.mkdir(base)
63 os.mkdir(base)
64 if not ui.quiet:
64 if not ui.quiet:
65 ui.write_err(_('making snapshot of %d files from rev %s\n') %
65 ui.write_err(_('making snapshot of %d files from rev %s\n') %
66 (len(files), short(node)))
66 (len(files), short(node)))
67 for fn in files:
67 for fn in files:
68 wfn = util.pconvert(fn)
68 wfn = util.pconvert(fn)
69 ui.note(' %s\n' % wfn)
69 ui.note(' %s\n' % wfn)
70 dest = os.path.join(base, wfn)
70 dest = os.path.join(base, wfn)
71 destdir = os.path.dirname(dest)
71 destdir = os.path.dirname(dest)
72 if not os.path.isdir(destdir):
72 if not os.path.isdir(destdir):
73 os.makedirs(destdir)
73 os.makedirs(destdir)
74 repo.wwrite(wfn, repo.file(fn).read(mf[fn]), open(dest, 'w'))
74 repo.wwrite(wfn, repo.file(fn).read(mf[fn]), open(dest, 'w'))
75 return dirname
75 return dirname
76
76
77 def snapshot_wdir(files):
77 def snapshot_wdir(files):
78 '''snapshot files from working directory.
78 '''snapshot files from working directory.
79 if not using snapshot, -I/-X does not work and recursive diff
79 if not using snapshot, -I/-X does not work and recursive diff
80 in tools like kdiff3 and meld displays too many files.'''
80 in tools like kdiff3 and meld displays too many files.'''
81 dirname = os.path.basename(repo.root)
81 dirname = os.path.basename(repo.root)
82 base = os.path.join(tmproot, dirname)
82 base = os.path.join(tmproot, dirname)
83 os.mkdir(base)
83 os.mkdir(base)
84 if not ui.quiet:
84 if not ui.quiet:
85 ui.write_err(_('making snapshot of %d files from working dir\n') %
85 ui.write_err(_('making snapshot of %d files from working dir\n') %
86 (len(files)))
86 (len(files)))
87 for fn in files:
87 for fn in files:
88 wfn = util.pconvert(fn)
88 wfn = util.pconvert(fn)
89 ui.note(' %s\n' % wfn)
89 ui.note(' %s\n' % wfn)
90 dest = os.path.join(base, wfn)
90 dest = os.path.join(base, wfn)
91 destdir = os.path.dirname(dest)
91 destdir = os.path.dirname(dest)
92 if not os.path.isdir(destdir):
92 if not os.path.isdir(destdir):
93 os.makedirs(destdir)
93 os.makedirs(destdir)
94 fp = open(dest, 'w')
94 fp = open(dest, 'w')
95 for chunk in util.filechunkiter(repo.wopener(wfn)):
95 for chunk in util.filechunkiter(repo.wopener(wfn)):
96 fp.write(chunk)
96 fp.write(chunk)
97 return dirname
97 return dirname
98
98
99 node1, node2 = cmdutil.revpair(ui, repo, opts['rev'])
99 node1, node2 = cmdutil.revpair(ui, repo, opts['rev'])
100 files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)
100 files, matchfn, anypats = cmdutil.matchpats(repo, pats, opts)
101 modified, added, removed, deleted, unknown = repo.status(
101 modified, added, removed, deleted, unknown = repo.status(
102 node1, node2, files, match=matchfn)[:5]
102 node1, node2, files, match=matchfn)[:5]
103 if not (modified or added or removed):
103 if not (modified or added or removed):
104 return 0
104 return 0
105
105
106 tmproot = tempfile.mkdtemp(prefix='extdiff.')
106 tmproot = tempfile.mkdtemp(prefix='extdiff.')
107 try:
107 try:
108 dir1 = snapshot_node(modified + removed, node1)
108 dir1 = snapshot_node(modified + removed, node1)
109 if node2:
109 if node2:
110 dir2 = snapshot_node(modified + added, node2)
110 dir2 = snapshot_node(modified + added, node2)
111 else:
111 else:
112 dir2 = snapshot_wdir(modified + added)
112 dir2 = snapshot_wdir(modified + added)
113 cmdline = ('%s %s %s %s' %
113 cmdline = ('%s %s %s %s' %
114 (util.shellquote(diffcmd), ' '.join(diffopts),
114 (util.shellquote(diffcmd), ' '.join(diffopts),
115 util.shellquote(dir1), util.shellquote(dir2)))
115 util.shellquote(dir1), util.shellquote(dir2)))
116 ui.debug('running %r in %s\n' % (cmdline, tmproot))
116 ui.debug('running %r in %s\n' % (cmdline, tmproot))
117 util.system(cmdline, cwd=tmproot)
117 util.system(cmdline, cwd=tmproot)
118 return 1
118 return 1
119 finally:
119 finally:
120 ui.note(_('cleaning up temp directory\n'))
120 ui.note(_('cleaning up temp directory\n'))
121 shutil.rmtree(tmproot)
121 shutil.rmtree(tmproot)
122
122
123 def extdiff(ui, repo, *pats, **opts):
123 def extdiff(ui, repo, *pats, **opts):
124 '''use external program to diff repository (or selected files)
124 '''use external program to diff repository (or selected files)
125
125
126 Show differences between revisions for the specified files, using
126 Show differences between revisions for the specified files, using
127 an external program. The default program used is diff, with
127 an external program. The default program used is diff, with
128 default options "-Npru".
128 default options "-Npru".
129
129
130 To select a different program, use the -p option. The program
130 To select a different program, use the -p option. The program
131 will be passed the names of two directories to compare. To pass
131 will be passed the names of two directories to compare. To pass
132 additional options to the program, use the -o option. These will
132 additional options to the program, use the -o option. These will
133 be passed before the names of the directories to compare.
133 be passed before the names of the directories to compare.
134
134
135 When two revision arguments are given, then changes are
135 When two revision arguments are given, then changes are
136 shown between those revisions. If only one revision is
136 shown between those revisions. If only one revision is
137 specified then that revision is compared to the working
137 specified then that revision is compared to the working
138 directory, and, when no revisions are specified, the
138 directory, and, when no revisions are specified, the
139 working directory files are compared to its parent.'''
139 working directory files are compared to its parent.'''
140 return dodiff(ui, repo, opts['program'] or 'diff',
140 program = opts['program'] or 'diff'
141 opts['option'] or ['-Npru'], pats, opts)
141 if opts['program']:
142 option = opts['option']
143 else:
144 option = opts['option'] or ['-Npru']
145 return dodiff(ui, repo, program, option, pats, opts)
142
146
143 cmdtable = {
147 cmdtable = {
144 "extdiff":
148 "extdiff":
145 (extdiff,
149 (extdiff,
146 [('p', 'program', '', _('comparison program to run')),
150 [('p', 'program', '', _('comparison program to run')),
147 ('o', 'option', [], _('pass option to comparison program')),
151 ('o', 'option', [], _('pass option to comparison program')),
148 ('r', 'rev', [], _('revision')),
152 ('r', 'rev', [], _('revision')),
149 ('I', 'include', [], _('include names matching the given patterns')),
153 ('I', 'include', [], _('include names matching the given patterns')),
150 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
154 ('X', 'exclude', [], _('exclude names matching the given patterns'))],
151 _('hg extdiff [OPT]... [FILE]...')),
155 _('hg extdiff [OPT]... [FILE]...')),
152 }
156 }
153
157
154 def uisetup(ui):
158 def uisetup(ui):
155 for cmd, path in ui.configitems('extdiff'):
159 for cmd, path in ui.configitems('extdiff'):
156 if not cmd.startswith('cmd.'): continue
160 if not cmd.startswith('cmd.'): continue
157 cmd = cmd[4:]
161 cmd = cmd[4:]
158 if not path: path = cmd
162 if not path: path = cmd
159 diffopts = ui.config('extdiff', 'opts.' + cmd, '')
163 diffopts = ui.config('extdiff', 'opts.' + cmd, '')
160 diffopts = diffopts and [diffopts] or []
164 diffopts = diffopts and [diffopts] or []
161 def save(cmd, path, diffopts):
165 def save(cmd, path, diffopts):
162 '''use closure to save diff command to use'''
166 '''use closure to save diff command to use'''
163 def mydiff(ui, repo, *pats, **opts):
167 def mydiff(ui, repo, *pats, **opts):
164 return dodiff(ui, repo, path, diffopts, pats, opts)
168 return dodiff(ui, repo, path, diffopts, pats, opts)
165 mydiff.__doc__ = '''use %(path)r to diff repository (or selected files)
169 mydiff.__doc__ = '''use %(path)r to diff repository (or selected files)
166
170
167 Show differences between revisions for the specified
171 Show differences between revisions for the specified
168 files, using the %(path)r program.
172 files, using the %(path)r program.
169
173
170 When two revision arguments are given, then changes are
174 When two revision arguments are given, then changes are
171 shown between those revisions. If only one revision is
175 shown between those revisions. If only one revision is
172 specified then that revision is compared to the working
176 specified then that revision is compared to the working
173 directory, and, when no revisions are specified, the
177 directory, and, when no revisions are specified, the
174 working directory files are compared to its parent.''' % {
178 working directory files are compared to its parent.''' % {
175 'path': path,
179 'path': path,
176 }
180 }
177 return mydiff
181 return mydiff
178 cmdtable[cmd] = (save(cmd, path, diffopts),
182 cmdtable[cmd] = (save(cmd, path, diffopts),
179 cmdtable['extdiff'][1][1:],
183 cmdtable['extdiff'][1][1:],
180 _('hg %s [OPT]... [FILE]...') % cmd)
184 _('hg %s [OPT]... [FILE]...') % cmd)
General Comments 0
You need to be logged in to leave comments. Login now