transaction: add a validation stage
authorPierre-Yves David <pierre-yves.david@fb.com>
Mon, 09 Mar 2015 22:43:36 -0700
changeset 24283 ef22cfff7052
parent 24282 db8679812f84
child 24284 ff14b26fe5f4
transaction: add a validation stage The 'transaction' object can now be fed a 'validator' function. This function will be run right before the transaction is closed to validate its content. The target usage is hooks. The validation function is expected to raise an exception when it wants to abort the transaction. This only introduce the idea with a default no-op validator. Actual usage is in the next changeset.
mercurial/transaction.py
--- a/mercurial/transaction.py	Mon Mar 09 22:36:56 2015 -0700
+++ b/mercurial/transaction.py	Mon Mar 09 22:43:36 2015 -0700
@@ -83,7 +83,7 @@
 
 class transaction(object):
     def __init__(self, report, opener, vfsmap, journalname, undoname=None,
-                 after=None, createmode=None):
+                 after=None, createmode=None, validator=None):
         """Begin a new transaction
 
         Begins a new transaction that allows rolling back writes in the event of
@@ -107,6 +107,12 @@
         self.journal = journalname
         self.undoname = undoname
         self._queue = []
+        # A callback to validate transaction content before closing it.
+        # should raise exception is anything is wrong.
+        # target user is repository hooks.
+        if validator is None:
+            validator = lambda tr: None
+        self.validator = validator
         # a dict of arguments to be passed to hooks
         self.hookargs = {}
         self.file = opener.open(self.journal, "w")
@@ -378,6 +384,7 @@
     def close(self):
         '''commit the transaction'''
         if self.count == 1:
+            self.validator(self)  # will raise exception if needed
             self._generatefiles()
             categories = sorted(self._finalizecallback)
             for cat in categories: