PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/mercurial-2.2.3/hgext/extdiff.py

#
Python | 329 lines | 295 code | 7 blank | 27 comment | 19 complexity | 8b5863f7637e4a6d9a2b76a8b476aaa7 MD5 | raw file
Possible License(s): GPL-2.0
  1. # extdiff.py - external diff program support for mercurial
  2. #
  3. # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
  4. #
  5. # This software may be used and distributed according to the terms of the
  6. # GNU General Public License version 2 or any later version.
  7. '''command to allow external programs to compare revisions
  8. The extdiff Mercurial extension allows you to use external programs
  9. to compare revisions, or revision with working directory. The external
  10. diff programs are called with a configurable set of options and two
  11. non-option arguments: paths to directories containing snapshots of
  12. files to compare.
  13. The extdiff extension also allows you to configure new diff commands, so
  14. you do not need to type :hg:`extdiff -p kdiff3` always. ::
  15. [extdiff]
  16. # add new command that runs GNU diff(1) in 'context diff' mode
  17. cdiff = gdiff -Nprc5
  18. ## or the old way:
  19. #cmd.cdiff = gdiff
  20. #opts.cdiff = -Nprc5
  21. # add new command called vdiff, runs kdiff3
  22. vdiff = kdiff3
  23. # add new command called meld, runs meld (no need to name twice)
  24. meld =
  25. # add new command called vimdiff, runs gvimdiff with DirDiff plugin
  26. # (see http://www.vim.org/scripts/script.php?script_id=102) Non
  27. # English user, be sure to put "let g:DirDiffDynamicDiffText = 1" in
  28. # your .vimrc
  29. vimdiff = gvim -f "+next" \\
  30. "+execute 'DirDiff' fnameescape(argv(0)) fnameescape(argv(1))"
  31. Tool arguments can include variables that are expanded at runtime::
  32. $parent1, $plabel1 - filename, descriptive label of first parent
  33. $child, $clabel - filename, descriptive label of child revision
  34. $parent2, $plabel2 - filename, descriptive label of second parent
  35. $root - repository root
  36. $parent is an alias for $parent1.
  37. The extdiff extension will look in your [diff-tools] and [merge-tools]
  38. sections for diff tool arguments, when none are specified in [extdiff].
  39. ::
  40. [extdiff]
  41. kdiff3 =
  42. [diff-tools]
  43. kdiff3.diffargs=--L1 '$plabel1' --L2 '$clabel' $parent $child
  44. You can use -I/-X and list of file or directory names like normal
  45. :hg:`diff` command. The extdiff extension makes snapshots of only
  46. needed files, so running the external diff program will actually be
  47. pretty fast (at least faster than having to compare the entire tree).
  48. '''
  49. from mercurial.i18n import _
  50. from mercurial.node import short, nullid
  51. from mercurial import scmutil, scmutil, util, commands, encoding
  52. import os, shlex, shutil, tempfile, re
  53. def snapshot(ui, repo, files, node, tmproot):
  54. '''snapshot files as of some revision
  55. if not using snapshot, -I/-X does not work and recursive diff
  56. in tools like kdiff3 and meld displays too many files.'''
  57. dirname = os.path.basename(repo.root)
  58. if dirname == "":
  59. dirname = "root"
  60. if node is not None:
  61. dirname = '%s.%s' % (dirname, short(node))
  62. base = os.path.join(tmproot, dirname)
  63. os.mkdir(base)
  64. if node is not None:
  65. ui.note(_('making snapshot of %d files from rev %s\n') %
  66. (len(files), short(node)))
  67. else:
  68. ui.note(_('making snapshot of %d files from working directory\n') %
  69. (len(files)))
  70. wopener = scmutil.opener(base)
  71. fns_and_mtime = []
  72. ctx = repo[node]
  73. for fn in files:
  74. wfn = util.pconvert(fn)
  75. if not wfn in ctx:
  76. # File doesn't exist; could be a bogus modify
  77. continue
  78. ui.note(' %s\n' % wfn)
  79. dest = os.path.join(base, wfn)
  80. fctx = ctx[wfn]
  81. data = repo.wwritedata(wfn, fctx.data())
  82. if 'l' in fctx.flags():
  83. wopener.symlink(data, wfn)
  84. else:
  85. wopener.write(wfn, data)
  86. if 'x' in fctx.flags():
  87. util.setflags(dest, False, True)
  88. if node is None:
  89. fns_and_mtime.append((dest, repo.wjoin(fn),
  90. os.lstat(dest).st_mtime))
  91. return dirname, fns_and_mtime
  92. def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
  93. '''Do the actuall diff:
  94. - copy to a temp structure if diffing 2 internal revisions
  95. - copy to a temp structure if diffing working revision with
  96. another one and more than 1 file is changed
  97. - just invoke the diff for a single file in the working dir
  98. '''
  99. revs = opts.get('rev')
  100. change = opts.get('change')
  101. args = ' '.join(diffopts)
  102. do3way = '$parent2' in args
  103. if revs and change:
  104. msg = _('cannot specify --rev and --change at the same time')
  105. raise util.Abort(msg)
  106. elif change:
  107. node2 = scmutil.revsingle(repo, change, None).node()
  108. node1a, node1b = repo.changelog.parents(node2)
  109. else:
  110. node1a, node2 = scmutil.revpair(repo, revs)
  111. if not revs:
  112. node1b = repo.dirstate.p2()
  113. else:
  114. node1b = nullid
  115. # Disable 3-way merge if there is only one parent
  116. if do3way:
  117. if node1b == nullid:
  118. do3way = False
  119. matcher = scmutil.match(repo[node2], pats, opts)
  120. mod_a, add_a, rem_a = map(set, repo.status(node1a, node2, matcher)[:3])
  121. if do3way:
  122. mod_b, add_b, rem_b = map(set, repo.status(node1b, node2, matcher)[:3])
  123. else:
  124. mod_b, add_b, rem_b = set(), set(), set()
  125. modadd = mod_a | add_a | mod_b | add_b
  126. common = modadd | rem_a | rem_b
  127. if not common:
  128. return 0
  129. tmproot = tempfile.mkdtemp(prefix='extdiff.')
  130. try:
  131. # Always make a copy of node1a (and node1b, if applicable)
  132. dir1a_files = mod_a | rem_a | ((mod_b | add_b) - add_a)
  133. dir1a = snapshot(ui, repo, dir1a_files, node1a, tmproot)[0]
  134. rev1a = '@%d' % repo[node1a].rev()
  135. if do3way:
  136. dir1b_files = mod_b | rem_b | ((mod_a | add_a) - add_b)
  137. dir1b = snapshot(ui, repo, dir1b_files, node1b, tmproot)[0]
  138. rev1b = '@%d' % repo[node1b].rev()
  139. else:
  140. dir1b = None
  141. rev1b = ''
  142. fns_and_mtime = []
  143. # If node2 in not the wc or there is >1 change, copy it
  144. dir2root = ''
  145. rev2 = ''
  146. if node2:
  147. dir2 = snapshot(ui, repo, modadd, node2, tmproot)[0]
  148. rev2 = '@%d' % repo[node2].rev()
  149. elif len(common) > 1:
  150. #we only actually need to get the files to copy back to
  151. #the working dir in this case (because the other cases
  152. #are: diffing 2 revisions or single file -- in which case
  153. #the file is already directly passed to the diff tool).
  154. dir2, fns_and_mtime = snapshot(ui, repo, modadd, None, tmproot)
  155. else:
  156. # This lets the diff tool open the changed file directly
  157. dir2 = ''
  158. dir2root = repo.root
  159. label1a = rev1a
  160. label1b = rev1b
  161. label2 = rev2
  162. # If only one change, diff the files instead of the directories
  163. # Handle bogus modifies correctly by checking if the files exist
  164. if len(common) == 1:
  165. common_file = util.localpath(common.pop())
  166. dir1a = os.path.join(tmproot, dir1a, common_file)
  167. label1a = common_file + rev1a
  168. if not os.path.isfile(dir1a):
  169. dir1a = os.devnull
  170. if do3way:
  171. dir1b = os.path.join(tmproot, dir1b, common_file)
  172. label1b = common_file + rev1b
  173. if not os.path.isfile(dir1b):
  174. dir1b = os.devnull
  175. dir2 = os.path.join(dir2root, dir2, common_file)
  176. label2 = common_file + rev2
  177. # Function to quote file/dir names in the argument string.
  178. # When not operating in 3-way mode, an empty string is
  179. # returned for parent2
  180. replace = dict(parent=dir1a, parent1=dir1a, parent2=dir1b,
  181. plabel1=label1a, plabel2=label1b,
  182. clabel=label2, child=dir2,
  183. root=repo.root)
  184. def quote(match):
  185. key = match.group()[1:]
  186. if not do3way and key == 'parent2':
  187. return ''
  188. return util.shellquote(replace[key])
  189. # Match parent2 first, so 'parent1?' will match both parent1 and parent
  190. regex = '\$(parent2|parent1?|child|plabel1|plabel2|clabel|root)'
  191. if not do3way and not re.search(regex, args):
  192. args += ' $parent1 $child'
  193. args = re.sub(regex, quote, args)
  194. cmdline = util.shellquote(diffcmd) + ' ' + args
  195. ui.debug('running %r in %s\n' % (cmdline, tmproot))
  196. util.system(cmdline, cwd=tmproot, out=ui.fout)
  197. for copy_fn, working_fn, mtime in fns_and_mtime:
  198. if os.lstat(copy_fn).st_mtime != mtime:
  199. ui.debug('file changed while diffing. '
  200. 'Overwriting: %s (src: %s)\n' % (working_fn, copy_fn))
  201. util.copyfile(copy_fn, working_fn)
  202. return 1
  203. finally:
  204. ui.note(_('cleaning up temp directory\n'))
  205. shutil.rmtree(tmproot)
  206. def extdiff(ui, repo, *pats, **opts):
  207. '''use external program to diff repository (or selected files)
  208. Show differences between revisions for the specified files, using
  209. an external program. The default program used is diff, with
  210. default options "-Npru".
  211. To select a different program, use the -p/--program option. The
  212. program will be passed the names of two directories to compare. To
  213. pass additional options to the program, use -o/--option. These
  214. will be passed before the names of the directories to compare.
  215. When two revision arguments are given, then changes are shown
  216. between those revisions. If only one revision is specified then
  217. that revision is compared to the working directory, and, when no
  218. revisions are specified, the working directory files are compared
  219. to its parent.'''
  220. program = opts.get('program')
  221. option = opts.get('option')
  222. if not program:
  223. program = 'diff'
  224. option = option or ['-Npru']
  225. return dodiff(ui, repo, program, option, pats, opts)
  226. cmdtable = {
  227. "extdiff":
  228. (extdiff,
  229. [('p', 'program', '',
  230. _('comparison program to run'), _('CMD')),
  231. ('o', 'option', [],
  232. _('pass option to comparison program'), _('OPT')),
  233. ('r', 'rev', [],
  234. _('revision'), _('REV')),
  235. ('c', 'change', '',
  236. _('change made by revision'), _('REV')),
  237. ] + commands.walkopts,
  238. _('hg extdiff [OPT]... [FILE]...')),
  239. }
  240. def uisetup(ui):
  241. for cmd, path in ui.configitems('extdiff'):
  242. if cmd.startswith('cmd.'):
  243. cmd = cmd[4:]
  244. if not path:
  245. path = cmd
  246. diffopts = ui.config('extdiff', 'opts.' + cmd, '')
  247. diffopts = diffopts and [diffopts] or []
  248. elif cmd.startswith('opts.'):
  249. continue
  250. else:
  251. # command = path opts
  252. if path:
  253. diffopts = shlex.split(path)
  254. path = diffopts.pop(0)
  255. else:
  256. path, diffopts = cmd, []
  257. # look for diff arguments in [diff-tools] then [merge-tools]
  258. if diffopts == []:
  259. args = ui.config('diff-tools', cmd+'.diffargs') or \
  260. ui.config('merge-tools', cmd+'.diffargs')
  261. if args:
  262. diffopts = shlex.split(args)
  263. def save(cmd, path, diffopts):
  264. '''use closure to save diff command to use'''
  265. def mydiff(ui, repo, *pats, **opts):
  266. return dodiff(ui, repo, path, diffopts + opts['option'],
  267. pats, opts)
  268. doc = _('''\
  269. use %(path)s to diff repository (or selected files)
  270. Show differences between revisions for the specified files, using
  271. the %(path)s program.
  272. When two revision arguments are given, then changes are shown
  273. between those revisions. If only one revision is specified then
  274. that revision is compared to the working directory, and, when no
  275. revisions are specified, the working directory files are compared
  276. to its parent.\
  277. ''') % dict(path=util.uirepr(path))
  278. # We must translate the docstring right away since it is
  279. # used as a format string. The string will unfortunately
  280. # be translated again in commands.helpcmd and this will
  281. # fail when the docstring contains non-ASCII characters.
  282. # Decoding the string to a Unicode string here (using the
  283. # right encoding) prevents that.
  284. mydiff.__doc__ = doc.decode(encoding.encoding)
  285. return mydiff
  286. cmdtable[cmd] = (save(cmd, path, diffopts),
  287. cmdtable['extdiff'][1][1:],
  288. _('hg %s [OPTION]... [FILE]...') % cmd)