changeset 38367:2ceea1554d1e

import: use context manager for lock, dirstateguard, transaction A tiny side-effect is that the transaction is now closed after saving the commit message. Differential Revision: https://phab.mercurial-scm.org/D3748
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 14 Jun 2018 15:17:47 -0700
parents c1fca51c26f3
children e53879421ecd
files mercurial/commands.py
diffstat 1 files changed, 15 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Sun Jun 17 23:03:23 2018 -0400
+++ b/mercurial/commands.py	Thu Jun 14 15:17:47 2018 -0700
@@ -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)
 
@@ -3111,22 +3108,24 @@
             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()
-                tr = repo.transaction('import')
-            else:
-                dsguard = dirstateguard.dirstateguard(repo, 'import')
+        if update:
+            cmdutil.checkunfinished(repo)
+            if (exact or not opts.get('force')):
+                cmdutil.bailifchanged(repo)
+
+        if not opts.get('no_commit'):
+            lock = repo.lock
+            tr = lambda: repo.transaction('import')
+            dsguard = util.nullcontextmanager
+        else:
+            lock = util.nullcontextmanager
+            tr = util.nullcontextmanager
+            dsguard = lambda: dirstateguard.dirstateguard(repo, 'import')
+        with lock(), tr(), dsguard():
             parents = repo[None].parents()
             for patchurl in patches:
                 if patchurl == '-':
@@ -3162,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,