hgkw/keyword.py
branch0.9.2compat
changeset 387 ecb2935f9bb8
parent 380 0ed26effe190
child 388 05fbd226cb5c
--- a/hgkw/keyword.py	Wed Feb 06 18:38:14 2008 +0100
+++ b/hgkw/keyword.py	Fri Feb 08 10:14:47 2008 +0100
@@ -89,13 +89,13 @@
 from mercurial import filelog, localrepo, revlog, templater, util
 from mercurial.node import *
 from mercurial.i18n import gettext as _
-import getopt, os.path, re, shutil, sys, tempfile, time
+import getopt, os.path, re, shutil, tempfile, time
 
 commands.optionalrepo += ' kwdemo'
 
 # hg commands that do not act on keywords
 nokwcommands = ('add addremove bundle copy export grep identify incoming init'
-                ' log outgoing push remove rename rollback tip convert')
+                ' log outgoing push remove rename rollback tip convert email')
 
 # hg commands that trigger expansion only when writing to working dir,
 # not when reading filelog, and unexpand when reading from working dir
@@ -106,11 +106,38 @@
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
 
-_kwtemplater = None
+_kwtemplater, _cmd, _cmdoptions = None, None, None
 
 # backwards compatibility hacks
 
 try:
+    # cmdutil.parse moves to dispatch._parse in 18a9fbb5cd78
+    from mercurial import dispatch
+    _dispatch_parse = dispatch._parse
+except ImportError:
+    try:
+        # commands.parse moves to cmdutil.parse in 0c61124ad877
+        _dispatch_parse = cmdutil.parse
+    except AttributeError:
+        _dispatch_parse = commands.parse
+
+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
+
+try:
+    setattr(dispatch, '_parse', _kwdispatch_parse)
+except (NameError, ImportError):
+    # 0.9.4 needs ImportError
+    if hasattr(cmdutil, 'parse'):
+        cmdutil.parse = _kwdispatch_parse
+    else:
+        commands.parse = _kwdispatch_parse
+
+try:
     # avoid spurious rejects if patchfile is available
     from mercurial.patch import patchfile
     _patchfile_init = patchfile.__init__
@@ -135,17 +162,6 @@
     template_filters = templater.common_filters
     template_firstline = templater.firstline
 
-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
@@ -245,11 +261,11 @@
         'Header': '{root}/{file},v {node|short} {date|utcdate} {author|user}',
     }
 
-    def __init__(self, ui, repo, inc, exc, restricted):
+    def __init__(self, ui, repo, inc, exc, restrict):
         self.ui = ui
         self.repo = repo
         self.matcher = util.matcher(repo.root, inc=inc, exc=exc)[1]
-        self.restricted = restricted
+        self.restrict = restrict
         self.commitnode = None
         self.path = ''
 
@@ -296,7 +312,7 @@
 
     def expand(self, node, data):
         '''Returns data with keywords expanded.'''
-        if self.restricted or util.binary(data):
+        if self.restrict or util.binary(data):
             return data
         return self.substitute(node, data, self.re_kw.sub)
 
@@ -536,23 +552,18 @@
     This is done for local repos only, and only if there are
     files configured at all for keyword substitution.'''
 
-    if not repo.local():
-        return
-
-    hgcmd, func, args, opts, cmdopts = _parse(ui, sys.argv[1:])
-    if hgcmd in nokwcommands.split():
-        return
+    global _kwtemplater
+    hgcmd, hgcmdopts = _cmd, _cmdoptions
 
-    if hgcmd == 'diff':
-        # only expand if comparing against working dir
-        node1, node2 = cmdutil.revpair(repo, cmdopts.get('rev'))
-        if node2 is not None:
+    try:
+        if (not repo.local() or hgcmd in nokwcommands.split() 
+            or '.hg' in repo.root.split('/')
+            or repo._url.startswith('bundle:')):
             return
-        # shrink if rev is not current node
-        if node1 is not None and node1 != repo.changectx().node():
-            hgcmd = 'diff1'
+    except AttributeError:
+        pass
 
-    inc, exc = [], ['.hgtags']
+    inc, exc = [], ['.hg*']
     for pat, opt in ui.configitems('keyword'):
         if opt != 'ignore':
             inc.append(pat)
@@ -561,9 +572,17 @@
     if not inc:
         return
 
-    global _kwtemplater
-    _restricted = hgcmd in restricted.split()
-    _kwtemplater = kwtemplater(ui, repo, inc, exc, _restricted)
+    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'
+
+    restrict = hgcmd in restricted.split()
+    _kwtemplater = kwtemplater(ui, repo, inc, exc, restrict)
 
     class kwrepo(repo.__class__):
         def file(self, f, kwmatch=False):
@@ -575,7 +594,7 @@
 
         def wread(self, filename):
             data = super(kwrepo, self).wread(filename)
-            if _restricted and _kwtemplater.matcher(filename):
+            if restrict and _kwtemplater.matcher(filename):
                 return _kwtemplater.shrink(data)
             return data