Patchwork clonebundle: use context managers for lock and transaction

login
register
mail settings
Submitter via Mercurial-devel
Date June 16, 2017, 4:11 a.m.
Message ID <329eaeee8809d229ce2c.1497586301@martinvonz.svl.corp.google.com>
Download mbox | patch
Permalink /patch/21422/
State Accepted
Headers show

Comments

via Mercurial-devel - June 16, 2017, 4:11 a.m.
# HG changeset patch
# User Martin von Zweigbergk <martinvonz@google.com>
# Date 1497571232 25200
#      Thu Jun 15 17:00:32 2017 -0700
# Node ID 329eaeee8809d229ce2c1c027acea7aa4a48cb73
# Parent  29558247b00eff8c95c7604032b59cfbab34010d
clonebundle: use context managers for lock and transaction
Sean Farley - June 16, 2017, 5:24 a.m.
Martin von Zweigbergk via Mercurial-devel
<mercurial-devel@mercurial-scm.org> writes:

> # HG changeset patch
> # User Martin von Zweigbergk <martinvonz@google.com>
> # Date 1497571232 25200
> #      Thu Jun 15 17:00:32 2017 -0700
> # Node ID 329eaeee8809d229ce2c1c027acea7aa4a48cb73
> # Parent  29558247b00eff8c95c7604032b59cfbab34010d
> clonebundle: use context managers for lock and transaction

Sure, sounds good. Queued, thanks!

Patch

diff --git a/mercurial/exchange.py b/mercurial/exchange.py
--- a/mercurial/exchange.py
+++ b/mercurial/exchange.py
@@ -1989,29 +1989,21 @@ 
 
 def trypullbundlefromurl(ui, repo, url):
     """Attempt to apply a bundle from a URL."""
-    lock = repo.lock()
-    try:
-        tr = repo.transaction('bundleurl')
+    with repo.lock(), repo.transaction('bundleurl') as tr:
         try:
-            try:
-                fh = urlmod.open(ui, url)
-                cg = readbundle(ui, fh, 'stream')
+            fh = urlmod.open(ui, url)
+            cg = readbundle(ui, fh, 'stream')
 
-                if isinstance(cg, bundle2.unbundle20):
-                    bundle2.processbundle(repo, cg, lambda: tr)
-                elif isinstance(cg, streamclone.streamcloneapplier):
-                    cg.apply(repo)
-                else:
-                    cg.apply(repo, 'clonebundles', url)
-                tr.close()
-                return True
-            except urlerr.httperror as e:
-                ui.warn(_('HTTP error fetching bundle: %s\n') % str(e))
-            except urlerr.urlerror as e:
-                ui.warn(_('error fetching bundle: %s\n') % e.reason)
+            if isinstance(cg, bundle2.unbundle20):
+                bundle2.processbundle(repo, cg, lambda: tr)
+            elif isinstance(cg, streamclone.streamcloneapplier):
+                cg.apply(repo)
+            else:
+                cg.apply(repo, 'clonebundles', url)
+            return True
+        except urlerr.httperror as e:
+            ui.warn(_('HTTP error fetching bundle: %s\n') % str(e))
+        except urlerr.urlerror as e:
+            ui.warn(_('error fetching bundle: %s\n') % e.reason)
 
-            return False
-        finally:
-            tr.release()
-    finally:
-        lock.release()
+        return False