# HG changeset patch # User Martin von Zweigbergk # Date 2017-07-12 20:57:03 # Node ID fad6852cf87944f7405bb2cf18fc47424465f61d # Parent 0491004e2233c2c33c300698b1e1855a79be9874 histedit: extract InterventionRequired transaction handling to utils rebase will have similar logic, so let's extract it. Besides, it makes the histedit code more readable. We may want to parametrize acceptintervention() by the exception(s) that should result in transaction close. Differential Revision: https://phab.mercurial-scm.org/D66 diff --git a/hgext/histedit.py b/hgext/histedit.py --- a/hgext/histedit.py +++ b/hgext/histedit.py @@ -1122,7 +1122,7 @@ def _continuehistedit(ui, repo, state): # and reopen a transaction. For example, if the action executes an # external process it may choose to commit the transaction first. tr = repo.transaction('histedit') - try: + with util.acceptintervention(tr): while state.actions: state.write(tr=tr) actobj = state.actions[0] @@ -1136,17 +1136,6 @@ def _continuehistedit(ui, repo, state): state.replacements.extend(replacement_) state.actions.pop(0) - if tr is not None: - tr.close() - except error.InterventionRequired: - if tr is not None: - tr.close() - raise - except Exception: - if tr is not None: - tr.abort() - raise - state.write() ui.progress(_("editing"), None) diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -19,6 +19,7 @@ import bz2 import calendar import codecs import collections +import contextlib import datetime import errno import gc @@ -589,6 +590,24 @@ class sortdict(collections.OrderedDict): del self[key] super(sortdict, self).__setitem__(key, value) +@contextlib.contextmanager +def acceptintervention(tr=None): + """A context manager that closes the transaction on InterventionRequired + + If no transaction was provided, this simply runs the body and returns + """ + if not tr: + yield + return + try: + yield + tr.close() + except error.InterventionRequired: + tr.close() + raise + finally: + tr.release() + class _lrucachenode(object): """A node in a doubly linked list.