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

Christian Ebert blacktrash at gmx.net
Wed Sep 29 19:19:53 CDT 2010

# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1285804619 -7200
# Node ID 8edf0e31e86c6027472241894c03045c91b02f2f
# Parent  814930cba3bd749a3de90e48535cdfa5ea2c364d
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