hgext/extdiff.py
changeset 2891 453097750fbf
parent 2888 ce967d96a1c1
child 2901 05f357b70cb0
equal deleted inserted replaced
2890:790fd342b6c7 2891:453097750fbf
    33 from mercurial.demandload import demandload
    33 from mercurial.demandload import demandload
    34 from mercurial.i18n import gettext as _
    34 from mercurial.i18n import gettext as _
    35 from mercurial.node import *
    35 from mercurial.node import *
    36 demandload(globals(), 'mercurial:commands,cmdutil,util os shutil tempfile')
    36 demandload(globals(), 'mercurial:commands,cmdutil,util os shutil tempfile')
    37 
    37 
    38 def dodiff(ui, repo, diffcmd, pats, opts):
    38 def dodiff(ui, repo, diffcmd, diffopts, pats, opts):
    39     def snapshot_node(files, node):
    39     def snapshot_node(files, node):
    40         '''snapshot files as of some revision'''
    40         '''snapshot files as of some revision'''
    41         changes = repo.changelog.read(node)
    41         changes = repo.changelog.read(node)
    42         mf = repo.manifest.read(changes[0])
    42         mf = repo.manifest.read(changes[0])
    43         dirname = '%s.%s' % (os.path.basename(repo.root), short(node))
    43         dirname = '%s.%s' % (os.path.basename(repo.root), short(node))
    90         dir1 = snapshot_node(modified + removed, node1)
    90         dir1 = snapshot_node(modified + removed, node1)
    91         if node2:
    91         if node2:
    92             dir2 = snapshot_node(modified + added, node2)
    92             dir2 = snapshot_node(modified + added, node2)
    93         else:
    93         else:
    94             dir2 = snapshot_wdir(modified + added)
    94             dir2 = snapshot_wdir(modified + added)
    95         util.system('%s %s %s %s' %
    95         cmdline = ('%s %s %s %s' %
    96                     (util.shellquote(diffcmd), ' '.join(opts['option']),
    96                    (util.shellquote(diffcmd),
    97                      util.shellquote(dir1), util.shellquote(dir2)),
    97                     ' '.join(map(util.shellquote, diffopts)),
    98                     cwd=tmproot)
    98                     util.shellquote(dir1), util.shellquote(dir2)))
       
    99         ui.debug('running %r in %s\n' % (cmdline, tmproot))
       
   100         util.system(cmdline, cwd=tmproot)
    99         return 1
   101         return 1
   100     finally:
   102     finally:
   101         ui.note(_('cleaning up temp directory\n'))
   103         ui.note(_('cleaning up temp directory\n'))
   102         shutil.rmtree(tmproot)
   104         shutil.rmtree(tmproot)
   103 
   105 
   104 def extdiff(ui, repo, *pats, **opts):
   106 def extdiff(ui, repo, *pats, **opts):
   105     '''use external program to diff repository (or selected files)
   107     '''use external program to diff repository (or selected files)
   106 
   108 
   107     Show differences between revisions for the specified files, using
   109     Show differences between revisions for the specified files, using
   108     an external program.  The default program used is "diff -Npru".
   110     an external program.  The default program used is diff, with
       
   111     default options "-Npru".
       
   112 
   109     To select a different program, use the -p option.  The program
   113     To select a different program, use the -p option.  The program
   110     will be passed the names of two directories to compare.  To pass
   114     will be passed the names of two directories to compare.  To pass
   111     additional options to the program, use the -o option.  These will
   115     additional options to the program, use the -o option.  These will
   112     be passed before the names of the directories to compare.
   116     be passed before the names of the directories to compare.
   113 
   117 
   114     When two revision arguments are given, then changes are
   118     When two revision arguments are given, then changes are
   115     shown between those revisions. If only one revision is
   119     shown between those revisions. If only one revision is
   116     specified then that revision is compared to the working
   120     specified then that revision is compared to the working
   117     directory, and, when no revisions are specified, the
   121     directory, and, when no revisions are specified, the
   118     working directory files are compared to its parent.'''
   122     working directory files are compared to its parent.'''
   119     return dodiff(ui, repo, opts['program'] or 'diff -Npru', pats, opts)
   123     return dodiff(ui, repo, opts['program'] or 'diff',
       
   124                   opts['option'] or ['-Npru'], pats, opts)
   120 
   125 
   121 cmdtable = {
   126 cmdtable = {
   122     "extdiff":
   127     "extdiff":
   123     (extdiff,
   128     (extdiff,
   124      [('p', 'program', '', _('comparison program to run')),
   129      [('p', 'program', '', _('comparison program to run')),
   132 def uisetup(ui):
   137 def uisetup(ui):
   133     for cmd, path in ui.configitems('extdiff'):
   138     for cmd, path in ui.configitems('extdiff'):
   134         if not cmd.startswith('cmd.'): continue
   139         if not cmd.startswith('cmd.'): continue
   135         cmd = cmd[4:]
   140         cmd = cmd[4:]
   136         if not path: path = cmd
   141         if not path: path = cmd
       
   142         diffopts = ui.config('extdiff', 'opts.' + cmd, '')
       
   143         diffopts = diffopts and [diffopts] or []
   137         def save(cmd, path):
   144         def save(cmd, path):
   138             '''use closure to save diff command to use'''
   145             '''use closure to save diff command to use'''
   139             def mydiff(ui, repo, *pats, **opts):
   146             def mydiff(ui, repo, *pats, **opts):
   140                 return dodiff(ui, repo, path, pats, opts)
   147                 return dodiff(ui, repo, path, diffopts, pats, opts)
   141             mydiff.__doc__ = '''use %s to diff repository (or selected files)
   148             mydiff.__doc__ = '''use %(path)r to diff repository (or selected files)
   142 
   149 
   143             Show differences between revisions for the specified
   150             Show differences between revisions for the specified
   144             files, using the %s program.
   151             files, using the %(path)r program.
   145 
   152 
   146             When two revision arguments are given, then changes are
   153             When two revision arguments are given, then changes are
   147             shown between those revisions. If only one revision is
   154             shown between those revisions. If only one revision is
   148             specified then that revision is compared to the working
   155             specified then that revision is compared to the working
   149             directory, and, when no revisions are specified, the
   156             directory, and, when no revisions are specified, the
   150             working directory files are compared to its parent.''' % (cmd, cmd)
   157             working directory files are compared to its parent.''' % {
       
   158                 'path': path,
       
   159                 }
   151             return mydiff
   160             return mydiff
   152         cmdtable[cmd] = (save(cmd, path),
   161         cmdtable[cmd] = (save(cmd, path),
   153                          cmdtable['extdiff'][1][1:],
   162                          cmdtable['extdiff'][1][1:],
   154                          _('hg %s [OPT]... [FILE]...') % cmd)
   163                          _('hg %s [OPT]... [FILE]...') % cmd)