[PATCH 1 of 3 🚂] transaction: allow to register post close callback

Pierre-Yves David pierre-yves.david at ens-lyon.org
Fri Nov 7 16:05:28 UTC 2014


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1414502683 -3600
#      Tue Oct 28 14:24:43 2014 +0100
# Node ID 6c051a14a1aa57c05f2289ab5b82ff6a3098b304
# Parent  2d54aa5397cdb1c697673ba10b7618d5ac25c69e
transaction: allow to register post close callback

The addchangegroup code consider that the transaction is done after a `tr.close()` call
and schedule the hooks execution for after lock release. In the nested transaction
case, the transaction is not yet committed and we must delay this scheduling.
We add a `addpostclose` method (on the `addpending` and `addfinalize` ones) that
register code to be run if the transaction is successfully committed.

diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -105,10 +105,12 @@ class transaction(object):
         self._pendingcallback = {}
         # True is any pending data have been written ever
         self._anypending = False
         # holds callback to call when writing the transaction
         self._finalizecallback = {}
+        # hold callbalk for post transaction close
+        self._postclosecallback = {}
 
     def __del__(self):
         if self.journal:
             self._abort()
 
@@ -297,10 +299,19 @@ class transaction(object):
         newer callbacks.
         """
         self._finalizecallback[category] = callback
 
     @active
+    def addpostclose(self, category, callback):
+        """add a callback to be called after the transaction is closed
+
+        Category is a unique identifier to allow overwriting old callback with
+        newer callback.
+        """
+        self._postclosecallback[category] = callback
+
+    @active
     def close(self):
         '''commit the transaction'''
         if self.count == 1 and self.onclose is not None:
             self._generatefiles()
             categories = sorted(self._finalizecallback)
@@ -322,10 +333,14 @@ class transaction(object):
             self.opener.unlink(self.backupjournal)
             for _f, b, _ignore in self.backupentries:
                 self.opener.unlink(b)
         self.backupentries = []
         self.journal = None
+        # run post close action
+        categories = sorted(self._postclosecallback)
+        for cat in categories:
+            self._postclosecallback[cat]()
 
     @active
     def abort(self):
         '''abort the transaction (generally called on error, or when the
         transaction is not explicitly committed before going out of


More information about the Mercurial-devel mailing list