Patchwork D8198: transaction: add functionality to have multiple validators

login
register
mail settings
Submitter phabricator
Date Feb. 29, 2020, 7:31 a.m.
Message ID <differential-rev-PHID-DREV-g3ctro77agvlm3qlmv6u-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/45410/
State Superseded
Headers show

Comments

phabricator - Feb. 29, 2020, 7:31 a.m.
pulkit created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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`.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8198

AFFECTED FILES
  mercurial/transaction.py

CHANGE DETAILS




To: pulkit, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -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['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