transaction: add functionality to have multiple validators
authorPulkit Goyal <7895pulkit@gmail.com>
Sat, 29 Feb 2020 12:56:37 +0530
changeset 44543 36f08ae87ef6
parent 44542 a2b49606a837
child 44544 13da36d77a3f
transaction: add functionality to have multiple validators This will help us in adding more validators which can aggregate data from transaction and prompt user whether to commit the transaction or not. The current target is to use this to implement `pull --confirm`. Differential Revision: https://phab.mercurial-scm.org/D8198
mercurial/transaction.py
--- a/mercurial/transaction.py	Wed Mar 04 22:13:15 2020 +0530
+++ b/mercurial/transaction.py	Sat Feb 29 12:56:37 2020 +0530
@@ -165,12 +165,6 @@
         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 callback to do something just after releasing transaction.
         if releasefn is None:
             releasefn = lambda tr, success: None
@@ -214,6 +208,11 @@
         self._anypending = False
         # holds callback to call when writing the transaction
         self._finalizecallback = {}
+        # holds callback to call when validating the transaction
+        # should raise exception if anything is wrong
+        self._validatecallback = {}
+        if validator is not None:
+            self._validatecallback[b'001-userhooks'] = validator
         # hold callback for post transaction close
         self._postclosecallback = {}
         # holds callbacks to call during abort
@@ -506,11 +505,21 @@
         self._abortcallback[category] = callback
 
     @active
+    def addvalidator(self, category, callback):
+        """ adds a callback to be called when validating the transaction.
+
+        The transaction will be given as the first argument to the callback.
+
+        callback should raise exception if to abort transaction """
+        self._validatecallback[category] = callback
+
+    @active
     def close(self):
         '''commit the transaction'''
         if self._count == 1:
-            self._validator(self)  # will raise exception if needed
-            self._validator = None  # Help prevent cycles.
+            for category in sorted(self._validatecallback):
+                self._validatecallback[category](self)
+            self._validatecallback = None  # Help prevent cycles.
             self._generatefiles(group=GEN_GROUP_PRE_FINALIZE)
             while self._finalizecallback:
                 callbacks = self._finalizecallback