[PATCH] keyword: monkeypatch patch.diff for non-interactive diffs

Christian Ebert blacktrash at gmx.net
Wed Feb 13 16:55:54 CST 2008


Hi,

Correted version of previously sent patch.

Received an urgent phone call, and got stuck in the wrong patch
queue.

c



# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1202942997 -3600
# Node ID 9a77771cd1f42cc83168220787713422588f02d5
# Parent  e85deb6dcf9d9265a2a35b5ec6fac36dfed28aee
keyword: monkeypatch patch.diff for non-interactive diffs

Avoid clobbering diffs with expanded keywords eg. for
notify extension.

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -94,7 +94,7 @@
 
 # hg commands that trigger expansion only when writing to working dir,
 # not when reading filelog, and unexpand when reading from working dir
-restricted = 'diff1 record qfold qimport qnew qpush qrefresh qrecord'
+restricted = 'record qfold qimport qnew qpush qrefresh qrecord'
 
 def utcdate(date):
     '''Returns hgdate in cvs-like UTC format.'''
@@ -105,6 +105,7 @@
  
 # store originals of monkeypatches
 _patchfile_init = patch.patchfile.__init__
+_patch_diff = patch.diff
 _dispatch_parse = dispatch._parse
 
 def _kwpatchfile_init(self, ui, fname, missing=False):
@@ -115,6 +116,16 @@
         # shrink keywords read from working dir
         kwshrunk = _kwtemplater.shrink(''.join(self.lines))
         self.lines = kwshrunk.splitlines(True)
+
+def _kw_diff(repo, node1=None, node2=None, files=None, match=util.always,
+             fp=None, changes=None, opts=None):
+    # only expand if comparing against working dir
+    if node2 is not None:
+        _kwtemplater.matcher = util.never
+    if node1 is not None and node1 != repo.changectx().node():
+        _kwtemplater.restrict = True
+    _patch_diff(repo, node1=node1, node2=node2, files=files, match=match,
+                fp=fp, changes=changes, opts=opts)
 
 def _kwweb_changeset(web, req, tmpl):
     '''Wraps webcommands.changeset turning off keyword expansion.'''
@@ -127,11 +138,10 @@
     return web.filediff(tmpl, web.filectx(req))
 
 def _kwdispatch_parse(ui, args):
-    '''Monkeypatch dispatch._parse to obtain
-    current command and command options (global _cmd, _cmdoptions).'''
-    global _cmd, _cmdoptions
-    _cmd, func, args, options, _cmdoptions = _dispatch_parse(ui, args)
-    return _cmd, func, args, options, _cmdoptions
+    '''Monkeypatch dispatch._parse to obtain running hg command.'''
+    global _cmd
+    _cmd, func, args, options, cmdoptions = _dispatch_parse(ui, args)
+    return _cmd, func, args, options, cmdoptions
 
 # dispatch._parse is run before reposetup, so wrap it here
 dispatch._parse = _kwdispatch_parse
@@ -434,10 +444,9 @@
     files configured at all for keyword substitution.'''
 
     global _kwtemplater
-    hgcmd, hgcmdopts = _cmd, _cmdoptions
 
     try:
-        if (not repo.local() or hgcmd in nokwcommands.split() 
+        if (not repo.local() or _cmd in nokwcommands.split() 
             or '.hg' in util.splitpath(repo.root)
             or repo._url.startswith('bundle:')):
             return
@@ -453,16 +462,7 @@
     if not inc:
         return
 
-    if hgcmd == 'diff':
-        # only expand if comparing against working dir
-        node1, node2 = cmdutil.revpair(repo, hgcmdopts.get('rev'))
-        if node2 is not None:
-            return
-        # shrink if rev is not current node
-        if node1 is not None and node1 != repo.changectx().node():
-            hgcmd = 'diff1'
-
-    _kwtemplater = kwtemplater(ui, repo, inc, exc, hgcmd)
+    _kwtemplater = kwtemplater(ui, repo, inc, exc, _cmd)
 
     class kwrepo(repo.__class__):
         def file(self, f, kwmatch=False):
@@ -523,6 +523,7 @@
 
     repo.__class__ = kwrepo
     patch.patchfile.__init__ = _kwpatchfile_init
+    patch.diff = _kw_diff
     webcommands.changeset = webcommands.rev = _kwweb_changeset
     webcommands.filediff = webcommands.diff = _kwweb_filediff
 



More information about the Mercurial-devel mailing list