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

Christian Ebert blacktrash at gmx.net
Thu Sep 30 19:24:42 CDT 2010


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1285892011 -7200
# Node ID a9421b5cd93a99ba6e509acd4767d00c9a9f40cc
# Parent  8029dc8ff32b369184a6689d843a14fb5d7119ca
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])
                 else:
                     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,
                                                   self.re_kw.subn)
+                elif self.restrict:
+                    found = self.re_kw.search(data)
                 else:
-                    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