(stable) merge with default branch stable
authorChristian Ebert <blacktrash@gmx.net>
Sat, 09 Feb 2008 09:42:00 +0100
branchstable
changeset 389 d18a329bf222
parent 375 ed160b097bad (current diff)
parent 386 c27209bf8385 (diff)
child 390 ee07603fab12
(stable) merge with default branch
--- a/hgkw/keyword.py	Tue Feb 05 16:09:21 2008 -0600
+++ b/hgkw/keyword.py	Sat Feb 09 09:42:00 2008 +0100
@@ -84,13 +84,13 @@
 from mercurial import patch, localrepo, templater, templatefilters, util
 from mercurial.node import *
 from mercurial.i18n import _
-import re, shutil, sys, tempfile, time
+import 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
@@ -101,7 +101,30 @@
     return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0]))
 
 
-_kwtemplater = None
+_kwtemplater, _cmd, _cmdoptions = None, None, None
+ 
+# store originals of monkeypatches
+_patchfile_init = patch.patchfile.__init__
+_dispatch_parse = dispatch._parse
+
+def _kwpatchfile_init(self, ui, fname, missing=False):
+    '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
+    rejects or conflicts due to expanded keywords in working dir.'''
+    _patchfile_init(self, ui, fname, missing=missing)
+    if _kwtemplater.matcher(self.fname):
+        # shrink keywords read from working dir
+        kwshrunk = _kwtemplater.shrink(''.join(self.lines))
+        self.lines = kwshrunk.splitlines(True)
+
+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
+
+dispatch._parse = _kwdispatch_parse
+
 
 class kwtemplater(object):
     '''
@@ -118,11 +141,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 = ''
 
@@ -161,7 +184,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)
 
@@ -213,21 +236,6 @@
             return t2 != text
         return revlog.revlog.cmp(self, node, text)
 
-
-# store original patch.patchfile.__init__
-_patchfile_init = patch.patchfile.__init__
-
-def _kwpatchfile_init(self, ui, fname, missing=False):
-    '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
-    rejects or conflicts due to expanded keywords in working dir.'''
-    _patchfile_init(self, ui, fname, missing=missing)
-
-    if _kwtemplater.matcher(self.fname):
-        # shrink keywords read from working dir
-        kwshrunk = _kwtemplater.shrink(''.join(self.lines))
-        self.lines = kwshrunk.splitlines(True)
-
-
 def _iskwfile(f, link):
     return not link(f) and _kwtemplater.matcher(f)
 
@@ -412,23 +420,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 = dispatch._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)
@@ -437,9 +440,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):
@@ -451,7 +462,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
 
--- a/tests/test-keyword	Tue Feb 05 16:09:21 2008 -0600
+++ b/tests/test-keyword	Sat Feb 09 09:42:00 2008 +0100
@@ -24,8 +24,8 @@
 
 hg --quiet kwdemo "Branch = {branches}"
 
-hg init Test
-cd Test
+hg init Test-bndl
+cd Test-bndl
 
 echo % kwshrink should exit silently in empty/invalid repo
 hg kwshrink
@@ -69,6 +69,16 @@
 mv $HGRCPATH.nohook $HGRCPATH
 rm hooktest
 
+echo % bundle
+hg bundle --base null ../kw.hg
+
+cd ..
+hg init Test
+cd Test
+
+echo % pull from bundle
+hg pull -u ../kw.hg
+
 echo % touch
 touch a b
 echo % status
@@ -90,8 +100,12 @@
 echo % compare changenodes in a c
 cat a c
 
+echo % qinit -c
+hg qinit -c
 echo % qimport
 hg qimport -r tip -n mqtest.diff
+echo % qcommit
+hg qcommit -mqtest
 echo % keywords should not be expanded in patch
 cat .hg/patches/mqtest.diff
 echo % qpop
--- a/tests/test-keyword.out	Tue Feb 05 16:09:21 2008 -0600
+++ b/tests/test-keyword.out	Sat Feb 09 09:42:00 2008 +0100
@@ -138,6 +138,16 @@
 a
 % diff a hooktest
 % removing commit hook from config
+% bundle
+1 changesets found
+% pull from bundle
+pulling from ../kw.hg
+requesting all changes
+adding changesets
+adding manifests
+adding file changes
+added 1 changesets with 3 changes to 3 files
+3 files updated, 0 files merged, 0 files removed, 0 files unresolved
 % touch
 % status
 % update
@@ -162,7 +172,9 @@
 xxx $
 $Id: c,v ba4426d1938e 1970/01/01 00:00:01 user $
 tests for different changenodes
+% qinit -c
 % qimport
+% qcommit
 % keywords should not be expanded in patch
 # HG changeset patch
 # User User Name <user@example.com>