[PATCH 4 of 7 main-line-of-work] transaction: pass the transaction to 'pending' callback

Pierre-Yves David pierre-yves.david at ens-lyon.org
Tue Nov 11 11:35:11 CST 2014


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1415464070 0
#      Sat Nov 08 16:27:50 2014 +0000
# Node ID 0655518980f77b00f74ee2a13fecd0ce5273dfd4
# Parent  f82d02b7ff612b2c00e531832d776e60f2392714
transaction: pass the transaction to 'pending' callback

The callback will likely needs to perform some operation related to the
transaction (eg: backing files up). So we better pass the current transaction as
the callback argument. Otherwise callback that needs it has to rely on horrible
weak reference trick.

The first foreseen user of this is changelog._writepending. We would like it to
register the temporary file it create for cleanup purpose.

diff --git a/mercurial/changelog.py b/mercurial/changelog.py
--- a/mercurial/changelog.py
+++ b/mercurial/changelog.py
@@ -268,11 +268,11 @@ class changelog(revlog.revlog):
         self.index = r.index
         self.nodemap = r.nodemap
         self._nodecache = r._nodecache
         self._chunkcache = r._chunkcache
 
-    def _writepending(self):
+    def _writepending(self, tr):
         "create a file containing the unfinalized state for pretxnchangegroup"
         if self._delaybuf:
             # make a temporary copy of the index
             fp1 = self._realopener(self.indexfile)
             fp2 = self._realopener(self.indexfile + ".a", "w")
diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -279,10 +279,12 @@ class transaction(object):
         return self.count > 0
 
     def addpending(self, category, callback):
         """add a callback to be called when the transaction is pending
 
+        The transaction will be given as callback first argument.
+
         Category is a unique identifier to allow overwriting an old callback
         with a newer callback.
         """
         self._pendingcallback[category] = callback
 
@@ -292,11 +294,11 @@ class transaction(object):
 
         This is used to allow hooks to view a transaction before commit'''
         categories = sorted(self._pendingcallback)
         for cat in categories:
             # remove callback since the data will have been flushed
-            any = self._pendingcallback.pop(cat)()
+            any = self._pendingcallback.pop(cat)(self)
             self._anypending = self._anypending or any
         return self._anypending
 
     @active
     def addfinalize(self, category, callback):


More information about the Mercurial-devel mailing list