(0.9.2compat) get mq support, improved diff output from default branch 0.9.2compat
authorChristian Ebert <blacktrash@gmx.net>
Sat, 19 Jan 2008 18:26:17 +0100
branch0.9.2compat
changeset 343 f5927e5574e6
parent 339 98336da24c5e
child 348 63ebc698d06b
(0.9.2compat) get mq support, improved diff output from default branch Reimplement _parse wrapper obtain diff options.
hgkw/keyword.py
tests/test-keyword
--- a/hgkw/keyword.py	Mon Jan 14 15:21:57 2008 +0100
+++ b/hgkw/keyword.py	Sat Jan 19 18:26:17 2008 +0100
@@ -71,6 +71,10 @@
 To force expansion after enabling it, or a configuration change, run
 "hg kwexpand".
 
+Also, when committing with the record extension or using mq's qrecord, be aware
+that keywords cannot be updated. Again, run "hg kwexpand" on the files in
+question to update keyword expansions after all changes have been checked in.
+
 Expansions spanning more than one line and incremental expansions,
 like CVS' $Log$, are not supported. A keyword template map
 "Log = {desc}" expands to the first line of the changeset description.
@@ -80,7 +84,7 @@
         Or, better, use bundle/unbundle to share changes.
 '''
 
-from mercurial import commands, cmdutil, context
+from mercurial import commands, cmdutil, context, fancyopts
 from mercurial import filelog, localrepo, revlog, templater, util
 from mercurial.node import *
 from mercurial.i18n import gettext as _
@@ -88,6 +92,17 @@
 
 # backwards compatibility hacks
 
+try:
+    # cmdutil.parse moves to dispatch._parse in 18a9fbb5cd78
+    # also avoid name conflict with other dispatch package(s)
+    from mercurial.dispatch import _parse
+except ImportError:
+    try:
+        # commands.parse moves to cmdutil.parse in 0c61124ad877
+        _parse = cmdutil.parse
+    except AttributeError:
+        _parse = commands.parse
+
 def _wwrite(repo, f, data, mf):
     '''Makes repo.wwrite backwards compatible.'''
     # 656e06eebda7 removed file descriptor argument
@@ -115,7 +130,7 @@
 
 # commands.parse/cmdutil.parse returned nothing for
 # "hg diff --rev" before 88803a69b24a due to bug in fancyopts
-def fancyopts(args, options, state):
+def _fancyopts(args, options, state):
     '''Fixed fancyopts from a9b7e425674f.'''
     namelist = []
     shortlist = ''
@@ -169,19 +184,7 @@
     # return unparsed args
     return args
 
-def findcmd(ui, cmd, table):
-    '''findcmd has table argument since 18a9fbb5cd78.'''
-    try:
-        return findcmd.findcmd(ui, cmd, table)
-    except TypeError:
-        return findcmd.findcmd(ui, cmd)
-# findcmd in commands until 0c61124ad877
-try:
-    findcmd.findcmd = cmdutil.findcmd
-    findcmd.__doc__ = cmdutil.findcmd.__doc__
-except AttributeError:
-    findcmd.findcmd = commands.findcmd
-    findcmd.__doc__ = commands.findcmd.__doc__
+fancyopts.fancyopts = _fancyopts
 
 
 commands.optionalrepo += ' kwdemo'
@@ -190,6 +193,14 @@
     '''Returns hgdate in cvs-like UTC format.'''
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
+def _kwrestrict(cmd):
+    '''Returns True if cmd should trigger restricted expansion.
+    Keywords will only expanded when writing to working dir.
+    Crucial for mq as expanded keywords should not make it into patches.'''
+    return cmd in ('diff1', 
+                   'qimport', 'qnew', 'qpush', 'qrefresh', 'record', 'qrecord')
+
+
 _kwtemplater = None
 
 class kwtemplater(object):
@@ -207,10 +218,11 @@
         'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
     }
 
-    def __init__(self, ui, repo, inc, exc):
+    def __init__(self, ui, repo, inc, exc, hgcmd):
         self.ui = ui
         self.repo = repo
         self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
+        self.hgcmd = hgcmd
         self.commitnode = None
         self.path = ''
 
@@ -257,7 +269,7 @@
 
     def expand(self, node, data):
         '''Returns data with keywords expanded.'''
-        if util.binary(data):
+        if util.binary(data) or _kwrestrict(self.hgcmd):
             return data
         return self.substitute(node, data, self.re_kw.sub)
 
@@ -497,22 +509,27 @@
     This is done for local repos only, and only if there are
     files configured at all for keyword substitution.'''
 
-    def kwbailout():
-        '''Obtains command via simplified cmdline parsing,
-        returns True if keyword expansion not needed.'''
-        nokwcommands = ('add', 'addremove', 'bundle', 'clone', 'copy',
-                        'export', 'grep', 'identify', 'incoming', 'init',
-                        'log', 'outgoing', 'push', 'remove', 'rename',
-                        'rollback', 'tip',
-                        'convert')
-        args = fancyopts(sys.argv[1:], commands.globalopts, {})
-        if args:
-            aliases, i = findcmd(ui, args[0], commands.table)
-            return aliases[0] in nokwcommands
+    if not repo.local():
+        return
 
-    if not repo.local() or kwbailout():
+    nokwcommands = ('add', 'addremove', 'bundle', 'clone', 'copy',
+                    'export', 'grep', 'identify', 'incoming', 'init',
+                    'log', 'outgoing', 'push', 'remove', 'rename',
+                    'rollback', 'tip',
+                    'convert')
+    hgcmd, func, args, opts, cmdopts = _parse(ui, sys.argv[1:])
+    if hgcmd in nokwcommands:
         return
 
+    if hgcmd == 'diff':
+        # only expand if comparing against working dir
+        node1, node2 = cmdutil.revpair(repo, cmdopts.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'
+
     inc, exc = [], ['.hgtags']
     for pat, opt in ui.configitems('keyword'):
         if opt != 'ignore':
@@ -523,7 +540,7 @@
         return
 
     global _kwtemplater
-    _kwtemplater = kwtemplater(ui, repo, inc, exc)
+    _kwtemplater = kwtemplater(ui, repo, inc, exc, hgcmd)
 
     class kwrepo(repo.__class__):
         def file(self, f, kwmatch=False):
@@ -533,6 +550,12 @@
                 return kwfilelog(self.sopener, f)
             return filelog.filelog(self.sopener, f)
 
+        def wread(self, filename):
+            data = super(kwrepo, self).wread(filename)
+            if _kwrestrict(hgcmd) and _kwtemplater.matcher(filename):
+                return _kwtemplater.shrink(data)
+            return data
+
         def _commit(self, files, text, user, date, match, force, lock, wlock,
                     force_editor, p1, p2, extra):
             '''Private commit wrapper for backwards compatibility.'''
--- a/tests/test-keyword	Mon Jan 14 15:21:57 2008 +0100
+++ b/tests/test-keyword	Sat Jan 19 18:26:17 2008 +0100
@@ -3,6 +3,7 @@
 cat <<EOF >> $HGRCPATH
 [extensions]
 hgext.keyword =
+hgext.mq =
 [keyword]
 * =
 b = ignore
@@ -89,9 +90,19 @@
 hg -v kwexpand
 echo % compare changenodes in a c
 cat a c
-echo % rollback and remove c
-hg rollback
-rm c
+
+echo % qimport
+hg qimport -r tip -n mqtest.diff
+echo % keywords should not be expanded in patch
+cat .hg/patches/mqtest.diff
+echo % qpop
+hg qpop
+echo % qpush
+hg qpush
+echo % cat
+cat c
+echo % qpop and move on
+hg qpop
 
 echo % copy
 hg cp a c