[PATCH 3 of 4] keyword: avoid duplicate substition/search in kwtemplater.overwrite()

Christian Ebert blacktrash at gmx.net
Tue Sep 28 11:53:45 CDT 2010

# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1285690795 -7200
# Node ID 40d58ce59bc668a1cbe3851f935b0ddb189cf4c6
# Parent  575cdde8c14f0e5396e4e0701428a196d77f6b6d
keyword: avoid duplicate substition/search in kwtemplater.overwrite()

Only go into restricted read mode when reading from filelog.
rollback and record read from the working directory, where
restricted mode would already shrink keywords before overwrite()
either expands or shrinks them again.

This ensures that the usual automatic operations on keywords
are turned off during overwrite() and only overwrite() itself
acts on them.

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -198,13 +198,13 @@
         candidates = [f for f in candidates if self.iskwfile(f, ctx.flags)]
         if candidates:
             restrict = self.restrict
-            self.restrict = True        # do not expand when reading
+            self.restrict = changed is None # no expansion on filelog read
             rollback = kwtools['hgcmd'] == 'rollback'
             mf = ctx.manifest()
             msg = (expand and _('overwriting %s expanding keywords\n')
                    or _('overwriting %s shrinking keywords\n'))
             for f in candidates:
-                if not self.record and not rollback:
+                if self.restrict:           # commit, kwexpand, kwshrink
                     data = self.repo.file(f).read(mf[f])
                     data = self.repo.wread(f)
@@ -215,8 +215,10 @@
                         ctx = self.repo.filectx(f, fileid=mf[f]).changectx()
                     data, found = self.substitute(data, f, ctx,
+                elif self.restrict:
+                    found = self.re_kw.search(data)
-                    found = self.re_kw.search(data)
+                    data, found = self.re_kw.subn(r'$\1$', data)
                 if found:
                     self.ui.note(msg % f)
                     self.repo.wwrite(f, data, mf.flags(f))

More information about the Mercurial-devel mailing list