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