changeset 32843:a470bbb4e3af

clonebundle: use context managers for lock and transaction
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 15 Jun 2017 17:00:32 -0700
parents 97a4d09f5140
children 76bb53f8d374
files mercurial/exchange.py
diffstat 1 files changed, 15 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/exchange.py	Tue Apr 11 21:38:24 2017 +0900
+++ b/mercurial/exchange.py	Thu Jun 15 17:00:32 2017 -0700
@@ -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