D406: commit: use context manager with dirstateguard

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Tue Aug 15 07:13:39 UTC 2017


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

REVISION SUMMARY
  When I wrote https://phab.mercurial-scm.org/rHG5ac845ca059a9665bc2777d8bf5c8855060757cf (commit: don't let failed commit with
  --addremove update dirstate (issue5645), 2017-07-31), Durham's
  https://phab.mercurial-scm.org/rHG609606d217659e0a6c1cf6f907b6512be5340e57 (rebase: use one dirstateguard for when using
  rebase.singletransaction, 2017-07-20) had not yet landed, so I had to
  write it in the old-fashioned way. Now that Durham's patch is in, we
  can simplify by using a context manager.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/cmdutil.py

CHANGE DETAILS

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3000,19 +3000,13 @@
     # that doesn't support addremove
     if opts.get('addremove'):
         dsguard = dirstateguard.dirstateguard(repo, 'commit')
-    try:
+    with dsguard or util.nullcontextmanager():
         if dsguard:
             if scmutil.addremove(repo, matcher, "", opts) != 0:
                 raise error.Abort(
                     _("failed to mark all new/missing files as added/removed"))
 
-        r = commitfunc(ui, repo, message, matcher, opts)
-        if dsguard:
-            dsguard.close()
-        return r
-    finally:
-        if dsguard:
-            dsguard.release()
+        return commitfunc(ui, repo, message, matcher, opts)
 
 def samefile(f, ctx1, ctx2):
     if f in ctx1.manifest():



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


More information about the Mercurial-devel mailing list