[PATCH 2 of 3] keyword: support (q)record

Christian Ebert blacktrash at gmx.net
Fri Apr 23 05:24:15 CDT 2010


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1272017882 -7200
# Node ID 00931730c61c0188772cfd709834525bbf559c52
# Parent  a93ca895ae5120d7f15d24bfe2eeaaf879233cb9
keyword: support (q)record

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.
@@ -94,6 +89,8 @@
 # not when reading filelog, and unexpand when reading from working dir
 restricted = 'merge record qrecord resolve transplant'
 
+recordcommands = 'record qrecord'
+
 # provide cvs-like UTC date filter
 utcdate = lambda x: util.datestr((x[0], 0), '%Y/%m/%d %H:%M:%S')
 
@@ -124,6 +121,7 @@
         self.match = match.match(repo.root, '', [],
                                  kwtools['inc'], kwtools['exc'])
         self.restrict = kwtools['hgcmd'] in restricted.split()
+        self.record = kwtools['hgcmd'] in recordcommands.split()
 
         kwmaps = self.ui.configitems('keywordmaps')
         if kwmaps: # override default templates
@@ -161,11 +159,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 +174,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 +427,29 @@
 
         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']
+        try:
+            wlock = repo.wlock()
+            # 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
+        finally:
+            wlock.release()
+
+    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 +491,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 not kwt.record:
+                kwt.overwrite(n, True, None)
             return n
 
     # monkeypatches


More information about the Mercurial-devel mailing list