Patchwork [11,of,14,FIX-bundle2] bundlerepo: move temp-bundle writing logic into a closure

login
register
mail settings
Submitter Pierre-Yves David
Date Oct. 20, 2015, 2:36 p.m.
Message ID <5a5bdcfb1d79add813b9.1445351787@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/11188/
State Accepted
Commit 7cac6ee41be7fd6ff97c072f88bdee72ad594bbd
Headers show

Comments

Pierre-Yves David - Oct. 20, 2015, 2:36 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1445270284 -7200
#      Mon Oct 19 17:58:04 2015 +0200
# Node ID 5a5bdcfb1d79add813b938e97c82d8a76cb59736
# Parent  e9701075984882bbdbb6ab5a4b8cf5e7fb27e80a
# EXP-Topic generaldelta
# Available At http://hg.netv6.net/marmoute-wip/mercurial/
#              hg pull http://hg.netv6.net/marmoute-wip/mercurial/ -r 5a5bdcfb1d79
bundlerepo: move temp-bundle writing logic into a closure

We will reuse this logic for bundle2

Patch

diff --git a/mercurial/bundlerepo.py b/mercurial/bundlerepo.py
--- a/mercurial/bundlerepo.py
+++ b/mercurial/bundlerepo.py
@@ -235,10 +235,31 @@  class bundlephasecache(phases.phasecache
         self.invalidate()
         self.dirty = True
 
 class bundlerepository(localrepo.localrepository):
     def __init__(self, ui, path, bundlename):
+        def _writetempbundle(read, suffix, header=''):
+            """Write a temporary file to disk
+
+            This is closure because we need to make sure this tracked by
+            self.tempfile for cleanup purposes."""
+            fdtemp, temp = self.vfs.mkstemp(prefix="hg-bundle-",
+                                            suffix=".hg10un")
+            self.tempfile = temp
+            fptemp = os.fdopen(fdtemp, 'wb')
+
+            try:
+                fptemp.write(header)
+                while True:
+                    chunk = read(2**18)
+                    if not chunk:
+                        break
+                    fptemp.write(chunk)
+            finally:
+                fptemp.close()
+
+            return self.vfs.open(self.tempfile, mode="rb")
         self._tempparent = None
         try:
             localrepo.localrepository.__init__(self, ui, path)
         except error.RepoError:
             self._tempparent = tempfile.mkdtemp()
@@ -253,26 +274,11 @@  class bundlerepository(localrepo.localre
 
         self.tempfile = None
         f = util.posixfile(bundlename, "rb")
         self.bundlefile = self.bundle = exchange.readbundle(ui, f, bundlename)
         if self.bundle.compressed():
-            fdtemp, temp = self.vfs.mkstemp(prefix="hg-bundle-",
-                                            suffix=".hg10un")
-            self.tempfile = temp
-            fptemp = os.fdopen(fdtemp, 'wb')
-
-            try:
-                fptemp.write("HG10UN")
-                while True:
-                    chunk = self.bundle.read(2**18)
-                    if not chunk:
-                        break
-                    fptemp.write(chunk)
-            finally:
-                fptemp.close()
-
-            f = self.vfs.open(self.tempfile, mode="rb")
+            f = _writetempbundle(self.bundle.read, '.hg10un', header='HG10UN')
             self.bundlefile = self.bundle = exchange.readbundle(ui, f,
                                                                 bundlename,
                                                                 self.vfs)
 
         if isinstance(self.bundle, bundle2.unbundle20):