[PATCH 1 of 6] transaction: add releasefn to notify the end of a transaction scope

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Thu Oct 8 18:59:00 UTC 2015


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1444330426 -32400
#      Fri Oct 09 03:53:46 2015 +0900
# Node ID fea46c744240b972f814f635492d5d97943a876b
# Parent  5b01218d3502b01d72ac41297f95e124cccaace9
transaction: add releasefn to notify the end of a transaction scope

'releasefn' is used by subsequent patch, to do appropriate action
according to the result of it at the end of a transaction scope.

To ensure that 'releasefn' is invoked only once, this patch invokes it
after assignment 'self.journal = None', because such assignment
prevents from invoked 'transaction._abort()' again via '__del__()'.

    def __del__(self):
        if self.journal:
            self._abort()

diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -89,7 +89,7 @@
 
 class transaction(object):
     def __init__(self, report, opener, vfsmap, journalname, undoname=None,
-                 after=None, createmode=None, validator=None):
+                 after=None, createmode=None, validator=None, releasefn=None):
         """Begin a new transaction
 
         Begins a new transaction that allows rolling back writes in the event of
@@ -97,6 +97,7 @@
 
         * `after`: called after the transaction has been committed
         * `createmode`: the mode of the journal file that will be created
+        * `releasefn`: called after releasing (with transaction and result)
         """
         self.count = 1
         self.usages = 1
@@ -119,6 +120,11 @@
         if validator is None:
             validator = lambda tr: None
         self.validator = validator
+        # A callback to do something just after releasing transaction.
+        if releasefn is None:
+            releasefn = lambda tr, success: None
+        self.releasefn = releasefn
+
         # a dict of arguments to be passed to hooks
         self.hookargs = {}
         self.file = opener.open(self.journal, "w")
@@ -442,6 +448,9 @@
                                     % (vfs.join(b), inst))
         self._backupentries = []
         self.journal = None
+
+        self.releasefn(self, True) # notify success of closing transaction
+
         # run post close action
         categories = sorted(self._postclosecallback)
         for cat in categories:
@@ -506,7 +515,7 @@
                 self.report(_("rollback failed - please run hg recover\n"))
         finally:
             self.journal = None
-
+            self.releasefn(self, False) # notify failure of transaction
 
 def rollback(opener, vfsmap, file, report):
     """Rolls back the transaction contained in the given file


More information about the Mercurial-devel mailing list