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