mercurial/transaction.py
changeset 44543 36f08ae87ef6
parent 44407 f6798c1a80fa
child 45738 5df1655edf42
equal deleted inserted replaced
44542:a2b49606a837 44543:36f08ae87ef6
   163         self._entries = []
   163         self._entries = []
   164         self._map = {}
   164         self._map = {}
   165         self._journal = journalname
   165         self._journal = journalname
   166         self._undoname = undoname
   166         self._undoname = undoname
   167         self._queue = []
   167         self._queue = []
   168         # A callback to validate transaction content before closing it.
       
   169         # should raise exception is anything is wrong.
       
   170         # target user is repository hooks.
       
   171         if validator is None:
       
   172             validator = lambda tr: None
       
   173         self._validator = validator
       
   174         # A callback to do something just after releasing transaction.
   168         # A callback to do something just after releasing transaction.
   175         if releasefn is None:
   169         if releasefn is None:
   176             releasefn = lambda tr, success: None
   170             releasefn = lambda tr, success: None
   177         self._releasefn = releasefn
   171         self._releasefn = releasefn
   178 
   172 
   212         self._pendingcallback = {}
   206         self._pendingcallback = {}
   213         # True is any pending data have been written ever
   207         # True is any pending data have been written ever
   214         self._anypending = False
   208         self._anypending = False
   215         # holds callback to call when writing the transaction
   209         # holds callback to call when writing the transaction
   216         self._finalizecallback = {}
   210         self._finalizecallback = {}
       
   211         # holds callback to call when validating the transaction
       
   212         # should raise exception if anything is wrong
       
   213         self._validatecallback = {}
       
   214         if validator is not None:
       
   215             self._validatecallback[b'001-userhooks'] = validator
   217         # hold callback for post transaction close
   216         # hold callback for post transaction close
   218         self._postclosecallback = {}
   217         self._postclosecallback = {}
   219         # holds callbacks to call during abort
   218         # holds callbacks to call during abort
   220         self._abortcallback = {}
   219         self._abortcallback = {}
   221 
   220 
   504         with a newer callback.
   503         with a newer callback.
   505         """
   504         """
   506         self._abortcallback[category] = callback
   505         self._abortcallback[category] = callback
   507 
   506 
   508     @active
   507     @active
       
   508     def addvalidator(self, category, callback):
       
   509         """ adds a callback to be called when validating the transaction.
       
   510 
       
   511         The transaction will be given as the first argument to the callback.
       
   512 
       
   513         callback should raise exception if to abort transaction """
       
   514         self._validatecallback[category] = callback
       
   515 
       
   516     @active
   509     def close(self):
   517     def close(self):
   510         '''commit the transaction'''
   518         '''commit the transaction'''
   511         if self._count == 1:
   519         if self._count == 1:
   512             self._validator(self)  # will raise exception if needed
   520             for category in sorted(self._validatecallback):
   513             self._validator = None  # Help prevent cycles.
   521                 self._validatecallback[category](self)
       
   522             self._validatecallback = None  # Help prevent cycles.
   514             self._generatefiles(group=GEN_GROUP_PRE_FINALIZE)
   523             self._generatefiles(group=GEN_GROUP_PRE_FINALIZE)
   515             while self._finalizecallback:
   524             while self._finalizecallback:
   516                 callbacks = self._finalizecallback
   525                 callbacks = self._finalizecallback
   517                 self._finalizecallback = {}
   526                 self._finalizecallback = {}
   518                 categories = sorted(callbacks)
   527                 categories = sorted(callbacks)