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