[PATCH 2 of 7] keyword: support copy and rename

Christian Ebert blacktrash at gmx.net
Fri Oct 8 12:40:15 CDT 2010


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1286559586 -3600
# Node ID 9a248300cbb0570ba183edf36479c21fcb5b64a8
# Parent  3a4b3955af3ee3a0084cad68dc4db66c444221e8
keyword: support copy and rename

copy/rename destinations being unversioned and possibly ignored by
the extension should not contain expanded keywords.

Files copied/renamed from an ignored source are not touched.

Add tests covering both of the above cases, plus the corner case of
cp symlink foo; hg cp -A symlink foo (where foo becomes a regular file).

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -91,8 +91,8 @@
 commands.optionalrepo += ' kwdemo'
 
 # hg commands that do not act on keywords
-nokwcommands = ('add addremove annotate bundle copy export grep incoming init'
-                ' log outgoing push rename tip verify convert email glog')
+nokwcommands = ('add addremove annotate bundle export grep incoming init log'
+                ' outgoing push tip verify convert email glog')
 
 # hg commands that trigger expansion only when writing to working dir,
 # not when reading filelog, and unexpand when reading from working dir
@@ -199,7 +199,8 @@
 
     def overwrite(self, ctx, candidates, lookup, expand):
         '''Overwrites selected files expanding/shrinking keywords.'''
-        candidates = [f for f in candidates if self.iskwfile(f, ctx.flags)]
+        if self.restrict or lookup: # exclude kw_copy
+            candidates = [f for f in candidates if self.iskwfile(f, ctx.flags)]
         if not candidates:
             return
         commit = self.restrict and not lookup
@@ -547,6 +548,22 @@
         kwt.match = util.never
         return orig(web, req, tmpl)
 
+    def kw_copy(orig, ui, repo, pats, opts, rename=False):
+        '''Wraps cmdutil.copy so that copy/rename destinations do not
+        contain expanded keywords.
+        Note that the source may also be a symlink as:
+        hg cp sym x                -> x is symlink
+        cp sym x; hg cp -A sym x   -> x is file (maybe expanded keywords)
+        '''
+        orig(ui, repo, pats, opts, rename)
+        if opts.get('dry_run'):
+            return
+        wctx = repo[None]
+        candidates = [f for f in repo.dirstate.copies() if
+                      kwt.match(repo.dirstate.copied(f)) and
+                      not 'l' in wctx.flags(f)]
+        kwt.overwrite(wctx, candidates, False, False)
+
     def kw_dorecord(orig, ui, repo, commitfunc, *pats, **opts):
         '''Wraps record.dorecord expanding keywords after recording.'''
         wlock = repo.wlock()
@@ -569,6 +586,7 @@
 
     extensions.wrapfunction(patch.patchfile, '__init__', kwpatchfile_init)
     extensions.wrapfunction(patch, 'diff', kw_diff)
+    extensions.wrapfunction(cmdutil, 'copy', kw_copy)
     for c in 'annotate changeset rev filediff diff'.split():
         extensions.wrapfunction(webcommands, c, kwweb_skip)
     for name in recordextensions.split():
diff --git a/tests/test-keyword.t b/tests/test-keyword.t
--- a/tests/test-keyword.t
+++ b/tests/test-keyword.t
@@ -48,6 +48,7 @@
   > [keyword]
   > ** =
   > b = ignore
+  > i = ignore
   > [hooks]
   > commit=
   > commit.test=cp a hooktest
@@ -498,6 +499,38 @@
   $ touch c
   $ hg status
 
+Copy kwfile to keyword ignored file unexpanding keywords
+
+  $ hg --verbose copy a i
+  copying a to i
+  overwriting i shrinking keywords
+  $ head -n 1 i
+  expand $Id$
+  $ hg forget i
+  $ rm i
+
+Copy ignored file to ignored file: no overwriting
+
+  $ hg --verbose copy b i
+  copying b to i
+  $ hg forget i
+  $ rm i
+
+cp symlink (becomes regular file), and hg copy after
+
+  $ cp sym i
+  $ ls -l i
+  -rw-r--r--  * (glob)
+  $ head -1 i
+  expand $Id: a,v ef63ca68695b 1970/01/01 00:00:00 user $
+  $ hg copy --after --verbose sym i
+  copying sym to i
+  overwriting i shrinking keywords
+  $ head -1 i
+  expand $Id$
+  $ hg forget i
+  $ rm i
+
 Test different options of hg kwfiles
 
   $ hg kwfiles
@@ -541,6 +574,7 @@
   ** = 
   b = ignore
   demo.txt = 
+  i = ignore
   [keywordmaps]
   Xinfo = {author}: {desc}
   $Xinfo: test: hg keyword configuration and expansion example $


More information about the Mercurial-devel mailing list