# HG changeset patch # User Durham Goode # Date 2017-08-23 19:34:56 # Node ID 702a26fec3e2e85f05af3ebeaf0fc037027ad2b6 # Parent 13dc7f29531e3cd11f5f98e7d5cee836d1e62ac5 bundlerepo: move temp bundle creation to a separate function A future patch will refactor certain parts of bundlerepo initiatlization such that we need to create temp bundles from another function. Let's move this to another function to support that. Differential Revision: https://phab.mercurial-scm.org/D288 diff --git a/mercurial/bundlerepo.py b/mercurial/bundlerepo.py --- a/mercurial/bundlerepo.py +++ b/mercurial/bundlerepo.py @@ -264,24 +264,6 @@ def _getfilestarts(bundle): 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 - - with os.fdopen(fdtemp, pycompat.sysstr('wb')) as fptemp: - fptemp.write(header) - while True: - chunk = read(2**18) - if not chunk: - break - fptemp.write(chunk) - - return self.vfs.open(self.tempfile, mode="rb") self._tempparent = None try: localrepo.localrepository.__init__(self, ui, path) @@ -314,8 +296,8 @@ class bundlerepository(localrepo.localre msg = _('Unsupported changegroup version: %s') raise error.Abort(msg % version) if self.bundle.compressed(): - cgstream = _writetempbundle(part.read, - ".cg%sun" % version) + cgstream = self._writetempbundle(part.read, + ".cg%sun" % version) if cgstream is None: raise error.Abort(_('No changegroups found')) @@ -324,7 +306,8 @@ class bundlerepository(localrepo.localre self.bundle = changegroup.getunbundler(version, cgstream, 'UN') elif self.bundle.compressed(): - f = _writetempbundle(self.bundle.read, '.hg10un', header='HG10UN') + f = self._writetempbundle(self.bundle.read, '.hg10un', + header='HG10UN') self.bundlefile = self.bundle = exchange.readbundle(ui, f, bundlename, self.vfs) @@ -336,6 +319,23 @@ class bundlerepository(localrepo.localre phases.retractboundary(self, None, phases.draft, [ctx.node() for ctx in self[self.firstnewrev:]]) + def _writetempbundle(self, readfn, suffix, header=''): + """Write a temporary file to disk + """ + fdtemp, temp = self.vfs.mkstemp(prefix="hg-bundle-", + suffix=".hg10un") + self.tempfile = temp + + with os.fdopen(fdtemp, pycompat.sysstr('wb')) as fptemp: + fptemp.write(header) + while True: + chunk = readfn(2**18) + if not chunk: + break + fptemp.write(chunk) + + return self.vfs.open(self.tempfile, mode="rb") + @localrepo.unfilteredpropertycache def _phasecache(self): return bundlephasecache(self, self._phasedefaults)