[PATCH] keyword: make repo.commit use a custom commitctx wrapper

Christian Ebert blacktrash at gmx.net
Tue Jun 30 05:13:28 CDT 2009


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1246354203 -7200
# Node ID 02f1d60fe9bf5274d5b4cf409963815694c24338
# Parent  bacb06d1e8b60b6083e3139fc5fb23355612eef8
keyword: make repo.commit use a custom commitctx wrapper

This avoids forcing the dirstate of overwritten files to normal
during a commit.

Thanks to Dan Villiom Podlaski Christiansen for the idea of a
"double wrapper", so other extensions can still wrap
repo.commitctx safely.

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -189,7 +189,8 @@
                 if found:
                     notify(msg % f)
                     self.repo.wwrite(f, data, mf.flags(f))
-                    self.repo.dirstate.normal(f)
+                    if node is None:
+                        self.repo.dirstate.normal(f)
             self.restrict = False
 
     def shrinktext(self, text):
@@ -460,8 +461,14 @@
 
         def commit(self, text='', user=None, date=None, match=None,
                    force=False, editor=None, extra={}):
+            # use custom commitctx for user commands
+            # other extensions can still wrap repo.commitctx directly
+            repo.commitctx = self.kwcommitctx
+            return super(kwrepo, self).commit(text, user, date, match, force,
+                         editor, extra)
+
+        def kwcommitctx(self, ctx, error=False):
             wlock = lock = None
-            _p1 = _p2 = None
             try:
                 wlock = self.wlock()
                 lock = self.lock()
@@ -472,23 +479,17 @@
                         commithooks[name] = cmd
                         ui.setconfig('hooks', name, None)
                 if commithooks:
-                    # store parents for commit hook environment
-                    _p1, _p2 = repo.dirstate.parents()
-                    _p1 = hex(_p1)
-                    if _p2 == nullid:
-                        _p2 = ''
-                    else:
-                        _p2 = hex(_p2)
+                    # store parents for commit hooks
+                    p1, p2 = ctx.p1(), ctx.p2()
+                    xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
 
-                n = super(kwrepo, self).commit(text, user, date, match, force,
-                                               editor, extra)
+                n = super(kwrepo, self).commitctx(ctx, error)
 
-                # restore commit hooks
-                for name, cmd in commithooks.iteritems():
-                    ui.setconfig('hooks', name, cmd)
-                if n is not None:
-                    kwt.overwrite(n, True, None)
-                    repo.hook('commit', node=n, parent1=_p1, parent2=_p2)
+                kwt.overwrite(n, True, None)
+                if commithooks:
+                    for name, cmd in commithooks.iteritems():
+                        ui.setconfig('hooks', name, cmd)
+                    repo.hook('commit', node=n, parent1=xp1, parent2=xp2)
                 return n
             finally:
                 release(lock, wlock)


More information about the Mercurial-devel mailing list