Patchwork D3747: import: use context manager for wlock

login
register
mail settings
Submitter phabricator
Date June 15, 2018, 12:49 p.m.
Message ID <afb72f3f69b502c1d9e601de6f35fd55@localhost.localdomain>
Download mbox | patch
Permalink /patch/32175/
State Not Applicable
Headers show

Comments

phabricator - June 15, 2018, 12:49 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGc924e7dbcd0f: import: use context manager for wlock (authored by martinvonz, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3747?vs=9076&id=9091

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

AFFECTED FILES
  mercurial/commands.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -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,