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