[Differential] [Request, 32 lines] D66: histedit: extract InterventionRequired transaction handling to utils

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Wed Jul 12 21:39:04 UTC 2017


martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D66

AFFECTED FILES
  hgext/histedit.py
  mercurial/util.py

CHANGE DETAILS

Index: mercurial/util.py
===================================================================
--- mercurial/util.py
+++ mercurial/util.py
@@ -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)
 
+ at 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.
 
Index: hgext/histedit.py
===================================================================
--- hgext/histedit.py
+++ hgext/histedit.py
@@ -1123,7 +1123,7 @@
     # Force an initial state file write, so the user can run --abort/continue
     # even if there's an exception before the first transaction serialize.
     state.write()
-    try:
+    with util.acceptintervention(tr):
         while state.actions:
             state.write(tr=tr)
             actobj = state.actions[0]
@@ -1137,17 +1137,6 @@
             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)
 


EMAIL PREFERENCES
  https://phab.mercurial-scm.org/settings/panel/emailpreferences/

To: martinvonz, #hg-reviewers
Cc: mercurial-devel, indygreg


More information about the Mercurial-devel mailing list