D7430: bisect: replace try:/finally: by a "restore_state" context manager

dlax (Denis Laxalde) phabricator at mercurial-scm.org
Fri Nov 15 10:14:10 UTC 2019


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

REVISION SUMMARY
  This should help pytype to not consider "bgood" variable as NameError.
  See https://phab.mercurial-scm.org/D7384 for context.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/commands.py
  mercurial/hbisect.py

CHANGE DETAILS

diff --git a/mercurial/hbisect.py b/mercurial/hbisect.py
--- a/mercurial/hbisect.py
+++ b/mercurial/hbisect.py
@@ -11,6 +11,7 @@
 from __future__ import absolute_import
 
 import collections
+import contextlib
 
 from .i18n import _
 from .node import (
@@ -180,6 +181,15 @@
         raise error.Abort(_(b'cannot bisect (no known bad revisions)'))
 
 
+ at contextlib.contextmanager
+def restore_state(repo, state, node):
+    try:
+        yield
+    finally:
+        state[b'current'] = [node]
+        save_state(repo, state)
+
+
 def get(repo, status):
     """
     Return a list of revision(s) that match the given status:
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1073,7 +1073,7 @@
                 raise error.Abort(_(b'current bisect revision is a merge'))
         if rev:
             node = repo[scmutil.revsingle(repo, rev, node)].node()
-        try:
+        with hbisect.restore_state(repo, state, node):
             while changesets:
                 # update state
                 state[b'current'] = [node]
@@ -1105,9 +1105,6 @@
                 # update to next check
                 node = nodes[0]
                 mayupdate(repo, node, show_stats=False)
-        finally:
-            state[b'current'] = [node]
-            hbisect.save_state(repo, state)
         hbisect.printresult(ui, repo, state, displayer, nodes, bgood)
         return
 



To: dlax, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list