D3748: import: use context manager for lock, dirstateguard, transaction

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Fri Jun 15 13:04:53 EDT 2018


martinvonz updated this revision to Diff 9095.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3748?vs=9077&id=9095

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

AFFECTED FILES
  mercurial/commands.py

CHANGE DETAILS

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -40,7 +40,6 @@
     hbisect,
     help,
     hg,
-    lock as lockmod,
     logcmdutil,
     merge as mergemod,
     obsolete,
@@ -67,8 +66,6 @@
     stringutil,
 )
 
-release = lockmod.release
-
 table = {}
 table.update(debugcommandsmod.command._table)
 
@@ -3108,22 +3105,27 @@
             raise error.Abort(_('cannot use --exact with --prefix'))
 
     base = opts["base"]
-    dsguard = lock = tr = None
     msgs = []
     ret = 0
 
     with repo.wlock():
-        try:
-            if update:
-                cmdutil.checkunfinished(repo)
-                if (exact or not opts.get('force')):
-                    cmdutil.bailifchanged(repo)
-
-            if not opts.get('no_commit'):
-                lock = repo.lock()
+        if update:
+            cmdutil.checkunfinished(repo)
+            if (exact or not opts.get('force')):
+                cmdutil.bailifchanged(repo)
+
+        if not opts.get('no_commit'):
+            lock = repo.lock()
+            try:
                 tr = repo.transaction('import')
-            else:
-                dsguard = dirstateguard.dirstateguard(repo, 'import')
+            except:
+                lock.release()
+            dsguard = util.nullcontextmanager()
+        else:
+            lock = util.nullcontextmanager()
+            tr = util.nullcontextmanager()
+            dsguard = dirstateguard.dirstateguard(repo, 'import')
+        with lock, tr, dsguard:
             parents = repo[None].parents()
             for patchurl in patches:
                 if patchurl == '-':
@@ -3159,17 +3161,9 @@
                 if not haspatch:
                     raise error.Abort(_('%s: no diffs found') % patchurl)
 
-            if tr:
-                tr.close()
             if msgs:
                 repo.savecommitmessage('\n* * *\n'.join(msgs))
-            if dsguard:
-                dsguard.close()
-            return ret
-        finally:
-            if tr:
-                tr.release()
-            release(lock, dsguard)
+        return ret
 
 @command('incoming|in',
     [('f', 'force', None,



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


More information about the Mercurial-devel mailing list