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