import: use context manager for wlock
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 14 Jun 2018 15:12:28 -0700
changeset 38324 c924e7dbcd0f
parent 38323 48c52385e062
child 38325 5cb39a368c80
import: use context manager for wlock Differential Revision: https://phab.mercurial-scm.org/D3747
mercurial/commands.py
--- a/mercurial/commands.py	Thu Jun 14 15:08:32 2018 -0700
+++ b/mercurial/commands.py	Thu Jun 14 15:12:28 2018 -0700
@@ -3108,69 +3108,68 @@
             raise error.Abort(_('cannot use --exact with --prefix'))
 
     base = opts["base"]
-    wlock = dsguard = lock = tr = None
+    dsguard = lock = tr = None
     msgs = []
     ret = 0
 
-
-    try:
-        wlock = repo.wlock()
-
-        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')
-        parents = repo[None].parents()
-        for patchurl in patches:
-            if patchurl == '-':
-                ui.status(_('applying patch from stdin\n'))
-                patchfile = ui.fin
-                patchurl = 'stdin'      # for error message
+    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:
-                patchurl = os.path.join(base, patchurl)
-                ui.status(_('applying %s\n') % patchurl)
-                patchfile = hg.openpath(ui, patchurl)
-
-            haspatch = False
-            for hunk in patch.split(patchfile):
-                with patch.extract(ui, hunk) as patchdata:
-                    msg, node, rej = cmdutil.tryimportone(ui, repo, patchdata,
-                                                          parents, opts,
-                                                          msgs, hg.clean)
-                if msg:
-                    haspatch = True
-                    ui.note(msg + '\n')
-                if update or exact:
-                    parents = repo[None].parents()
+                dsguard = dirstateguard.dirstateguard(repo, 'import')
+            parents = repo[None].parents()
+            for patchurl in patches:
+                if patchurl == '-':
+                    ui.status(_('applying patch from stdin\n'))
+                    patchfile = ui.fin
+                    patchurl = 'stdin'      # for error message
                 else:
-                    parents = [repo[node]]
-                if rej:
-                    ui.write_err(_("patch applied partially\n"))
-                    ui.write_err(_("(fix the .rej files and run "
-                                   "`hg commit --amend`)\n"))
-                    ret = 1
-                    break
-
-            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, wlock)
+                    patchurl = os.path.join(base, patchurl)
+                    ui.status(_('applying %s\n') % patchurl)
+                    patchfile = hg.openpath(ui, patchurl)
+
+                haspatch = False
+                for hunk in patch.split(patchfile):
+                    with patch.extract(ui, hunk) as patchdata:
+                        msg, node, rej = cmdutil.tryimportone(ui, repo,
+                                                              patchdata,
+                                                              parents, opts,
+                                                              msgs, hg.clean)
+                    if msg:
+                        haspatch = True
+                        ui.note(msg + '\n')
+                    if update or exact:
+                        parents = repo[None].parents()
+                    else:
+                        parents = [repo[node]]
+                    if rej:
+                        ui.write_err(_("patch applied partially\n"))
+                        ui.write_err(_("(fix the .rej files and run "
+                                       "`hg commit --amend`)\n"))
+                        ret = 1
+                        break
+
+                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)
 
 @command('incoming|in',
     [('f', 'force', None,