diff mercurial/util.py @ 33446:fad6852cf879

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
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 12 Jul 2017 13:57:03 -0700
parents 0e114b992e02
children 9a2ee9591acc
line wrap: on
line diff
--- a/mercurial/util.py	Wed Jul 12 13:17:49 2017 -0700
+++ b/mercurial/util.py	Wed Jul 12 13:57:03 2017 -0700
@@ -19,6 +19,7 @@
 import calendar
 import codecs
 import collections
+import contextlib
 import datetime
 import errno
 import gc
@@ -589,6 +590,24 @@
             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.