# HG changeset patch # User Durham Goode # Date 2014-03-24 22:57:47 # Node ID 3c47677a8d04bc3bdc271a9ec36bbfc4f66781ed # Parent 925c2d6043890e64c26b2b802a2e681f2464c5b2 transaction: add onclose/onabort hook for pre-close logic Adds an optional onclose parameter to transactions that gets called just before the transaction is committed. This allows things that build up data over the course of the transaction (like the fncache) to commit their data. Also adds onabort. It's not used, but will allow extensions to hook into onclose and onabort to provide transaction support. diff --git a/mercurial/transaction.py b/mercurial/transaction.py --- a/mercurial/transaction.py +++ b/mercurial/transaction.py @@ -42,12 +42,27 @@ def _playback(journal, report, opener, e opener.unlink(journal) class transaction(object): - def __init__(self, report, opener, journal, after=None, createmode=None): + def __init__(self, report, opener, journal, after=None, createmode=None, + onclose=None, onabort=None): + """Begin a new transaction + + Begins a new transaction that allows rolling back writes in the event of + an exception. + + * `after`: called after the transaction has been committed + * `createmode`: the mode of the journal file that will be created + * `onclose`: called as the transaction is closing, but before it is + closed + * `onabort`: called as the transaction is aborting, but before any files + have been truncated + """ self.count = 1 self.usages = 1 self.report = report self.opener = opener self.after = after + self.onclose = onclose + self.onabort = onabort self.entries = [] self.map = {} self.journal = journal @@ -126,6 +141,9 @@ class transaction(object): @active def close(self): '''commit the transaction''' + if self.count == 1 and self.onclose is not None: + self.onclose() + self.count -= 1 if self.count != 0: return @@ -149,6 +167,9 @@ class transaction(object): self.usages = 0 self.file.close() + if self.onabort is not None: + self.onabort() + try: if not self.entries: if self.journal: