# HG changeset patch # User Pierre-Yves David # Date 1425966216 25200 # Node ID ef22cfff705254834c454b347385f1cbdae010c7 # Parent db8679812f84b0974fed7c37f49549035d0330ce 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. diff -r db8679812f84 -r ef22cfff7052 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: