Patchwork [08,of,10] transaction: allow to register post close callback

login
register
mail settings
Submitter Pierre-Yves David
Date Oct. 28, 2014, 3:41 p.m.
Message ID <39eeb239ab3d900bad9a.1414510882@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/6488/
State Accepted
Headers show

Comments

Pierre-Yves David - Oct. 28, 2014, 3:41 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1414502683 -3600
#      Tue Oct 28 14:24:43 2014 +0100
# Branch stable
# Node ID 39eeb239ab3d900bad9adad309cfaa09ac95d810
# Parent  bdb9c60529bcda403e081e07d8d4c835b3750299
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.

Patch

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
         # hold callbalk to write 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 callback.
         """
         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