[PATCH 1 of 2] keyword: support recording

Christian Ebert blacktrash at gmx.net
Mon Apr 19 06:47:54 CDT 2010


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1271676541 -7200
# Node ID 447f507d109dfacc0465718d36b5f41c36df9c7d
# Parent  e532813d5b747e8b9040b651498cadf603617b65
keyword: support recording

Monkeypatch hgext.dorecord to trigger keyword expansion.
Read data from working directory, not from filelog.
Prevent keyword expansion from within record's commitfunc,
thereby fixing a bug/inconsistency where files which are clean
after recording were overwritten twice.

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -66,11 +66,6 @@
 To force expansion after enabling it, or a configuration change, run
 "hg kwexpand".
 
-Also, when committing with the record extension or using mq's qrecord,
-be aware that keywords cannot be updated. Again, run "hg kwexpand" on
-the files in question to update keyword expansions after all changes
-have been checked in.
-
 Expansions spanning more than one line and incremental expansions,
 like CVS' $Log$, are not supported. A keyword template map "Log =
 {desc}" expands to the first line of the changeset description.
@@ -161,11 +156,14 @@
         Caveat: localrepository._link fails on Windows.'''
         return self.match(path) and not 'l' in flagfunc(path)
 
-    def overwrite(self, node, expand, candidates):
+    def overwrite(self, node, expand, candidates, recctx=None):
         '''Overwrites selected files expanding/shrinking keywords.'''
-        ctx = self.repo[node]
+        if recctx is None:
+            ctx = self.repo[node]
+        else:
+            ctx = recctx
         mf = ctx.manifest()
-        if node is not None:     # commit
+        if node is not None:     # commit, record
             candidates = [f for f in ctx.files() if f in mf]
         candidates = [f for f in candidates if self.iskwfile(f, ctx.flags)]
         if candidates:
@@ -173,8 +171,10 @@
             msg = (expand and _('overwriting %s expanding keywords\n')
                    or _('overwriting %s shrinking keywords\n'))
             for f in candidates:
-                fp = self.repo.file(f)
-                data = fp.read(mf[f])
+                if recctx is None:
+                    data = self.repo.file(f).read(mf[f])
+                else:
+                    data = self.repo.wread(f)
                 if util.binary(data):
                     continue
                 if expand:
@@ -424,6 +424,26 @@
 
         extensions.wrapfunction(dispatch, '_parse', kwdispatch_parse)
 
+def extsetup(ui):
+    try:
+        record = extensions.find('record')
+    except KeyError:
+        return
+
+    def kw_dorecord(orig, ui, repo, commitfunc, *pats, **opts):
+        kwt = kwtools['templater']
+        # record returns 0 even when nothing has changed
+        # therefore compare nodes before and after
+        ctx = repo['.']
+        ret = orig(ui, repo, commitfunc, *pats, **opts)
+        recctx = repo['.']
+        if kwt and ctx != recctx:
+            kwt.overwrite('.',  True, None, recctx)
+        return ret
+
+    extensions.wrapfunction(record, 'dorecord', kw_dorecord)
+
+
 def reposetup(ui, repo):
     '''Sets up repo as kwrepo for keyword substitution.
     Overrides file method to return kwfilelog instead of filelog
@@ -465,7 +485,9 @@
         def kwcommitctx(self, ctx, error=False):
             n = super(kwrepo, self).commitctx(ctx, error)
             # no lock needed, only called from repo.commit() which already locks
-            kwt.overwrite(n, True, None)
+            # prevent keyword expansion within record's internal commitfunc
+            if kwtools['hgcmd'] != 'record':
+                kwt.overwrite(n, True, None)
             return n
 
     # monkeypatches


More information about the Mercurial-devel mailing list