changeset 21063:7ca4f2049d3b

bundle2: move `readbundle` into the `exchange` module The `readbundle` function is going to understand the bundle2 header. We move the function to a more suitable place before making any other changes.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Mon, 14 Apr 2014 15:33:50 -0400
parents e7c0a65a5c9c
children 4d9d490d7bbe
files hgext/shelve.py mercurial/bundlerepo.py mercurial/changegroup.py mercurial/commands.py mercurial/exchange.py mercurial/repair.py mercurial/wireproto.py
diffstat 7 files changed, 30 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/shelve.py	Mon Apr 14 14:46:32 2014 -0400
+++ b/hgext/shelve.py	Mon Apr 14 15:33:50 2014 -0400
@@ -25,7 +25,7 @@
 from mercurial.node import nullid, nullrev, bin, hex
 from mercurial import changegroup, cmdutil, scmutil, phases, commands
 from mercurial import error, hg, mdiff, merge, patch, repair, util
-from mercurial import templatefilters, changegroup
+from mercurial import templatefilters, changegroup, exchange
 from mercurial import lock as lockmod
 from hgext import rebase
 import errno
@@ -71,7 +71,7 @@
     def applybundle(self):
         fp = self.opener()
         try:
-            gen = changegroup.readbundle(fp, self.fname, self.vfs)
+            gen = exchange.readbundle(fp, self.fname, self.vfs)
             changegroup.addchangegroup(self.repo, gen, 'unshelve',
                                        'bundle:' + self.vfs.join(self.fname))
         finally:
--- a/mercurial/bundlerepo.py	Mon Apr 14 14:46:32 2014 -0400
+++ b/mercurial/bundlerepo.py	Mon Apr 14 15:33:50 2014 -0400
@@ -14,7 +14,7 @@
 from node import nullid
 from i18n import _
 import os, tempfile, shutil
-import changegroup, util, mdiff, discovery, cmdutil, scmutil
+import changegroup, util, mdiff, discovery, cmdutil, scmutil, exchange
 import localrepo, changelog, manifest, filelog, revlog, error
 
 class bundlerevlog(revlog.revlog):
@@ -202,7 +202,7 @@
 
         self.tempfile = None
         f = util.posixfile(bundlename, "rb")
-        self.bundle = changegroup.readbundle(f, bundlename)
+        self.bundle = exchange.readbundle(f, bundlename)
         if self.bundle.compressed():
             fdtemp, temp = self.vfs.mkstemp(prefix="hg-bundle-",
                                             suffix=".hg10un")
@@ -220,7 +220,7 @@
                 fptemp.close()
 
             f = self.vfs.open(self.tempfile, mode="rb")
-            self.bundle = changegroup.readbundle(f, bundlename, self.vfs)
+            self.bundle = exchange.readbundle(f, bundlename, self.vfs)
 
         # dict with the mapping 'filename' -> position in the bundle
         self.bundlefilespos = {}
--- a/mercurial/changegroup.py	Mon Apr 14 14:46:32 2014 -0400
+++ b/mercurial/changegroup.py	Mon Apr 14 15:33:50 2014 -0400
@@ -227,25 +227,6 @@
             return d
         return readexactly(self._fh, n)
 
-def readbundle(fh, fname, vfs=None):
-    header = readexactly(fh, 6)
-
-    if not fname:
-        fname = "stream"
-        if not header.startswith('HG') and header.startswith('\0'):
-            fh = headerlessfixup(fh, header)
-            header = "HG10UN"
-    elif vfs:
-        fname = vfs.join(fname)
-
-    magic, version, alg = header[0:2], header[2:4], header[4:6]
-
-    if magic != 'HG':
-        raise util.Abort(_('%s: not a Mercurial bundle') % fname)
-    if version != '10':
-        raise util.Abort(_('%s: unknown bundle version %s') % (fname, version))
-    return unbundle10(fh, alg)
-
 class bundle10(object):
     deltaheader = _BUNDLE10_DELTA_HEADER
     def __init__(self, repo, bundlecaps=None):
--- a/mercurial/commands.py	Mon Apr 14 14:46:32 2014 -0400
+++ b/mercurial/commands.py	Mon Apr 14 15:33:50 2014 -0400
@@ -20,7 +20,7 @@
 import dagparser, context, simplemerge, graphmod
 import random
 import setdiscovery, treediscovery, dagutil, pvec, localrepo
-import phases, obsolete
+import phases, obsolete, exchange
 
 table = {}
 
@@ -1736,7 +1736,7 @@
     """lists the contents of a bundle"""
     f = hg.openpath(ui, bundlepath)
     try:
-        gen = changegroup.readbundle(f, bundlepath)
+        gen = exchange.readbundle(f, bundlepath)
         if all:
             ui.write(("format: id, p1, p2, cset, delta base, len(delta)\n"))
 
@@ -5807,7 +5807,7 @@
     try:
         for fname in fnames:
             f = hg.openpath(ui, fname)
-            gen = changegroup.readbundle(f, fname)
+            gen = exchange.readbundle(f, fname)
             modheads = changegroup.addchangegroup(repo, gen, 'unbundle',
                                                   'bundle:' + fname)
     finally:
--- a/mercurial/exchange.py	Mon Apr 14 14:46:32 2014 -0400
+++ b/mercurial/exchange.py	Mon Apr 14 15:33:50 2014 -0400
@@ -11,6 +11,25 @@
 import util, scmutil, changegroup, base85
 import discovery, phases, obsolete, bookmarks, bundle2
 
+def readbundle(fh, fname, vfs=None):
+    header = changegroup.readexactly(fh, 6)
+
+    if not fname:
+        fname = "stream"
+        if not header.startswith('HG') and header.startswith('\0'):
+            fh = changegroup.headerlessfixup(fh, header)
+            header = "HG10UN"
+    elif vfs:
+        fname = vfs.join(fname)
+
+    magic, version, alg = header[0:2], header[2:4], header[4:6]
+
+    if magic != 'HG':
+        raise util.Abort(_('%s: not a Mercurial bundle') % fname)
+    if version != '10':
+        raise util.Abort(_('%s: unknown bundle version %s') % (fname, version))
+    return changegroup.unbundle10(fh, alg)
+
 
 class pushoperation(object):
     """A object that represent a single push operation
--- a/mercurial/repair.py	Mon Apr 14 14:46:32 2014 -0400
+++ b/mercurial/repair.py	Mon Apr 14 15:33:50 2014 -0400
@@ -6,7 +6,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-from mercurial import changegroup
+from mercurial import changegroup, exchange
 from mercurial.node import short
 from mercurial.i18n import _
 import errno
@@ -147,7 +147,7 @@
         if saveheads or savebases:
             ui.note(_("adding branch\n"))
             f = vfs.open(chgrpfile, "rb")
-            gen = changegroup.readbundle(f, chgrpfile, vfs)
+            gen = exchange.readbundle(f, chgrpfile, vfs)
             if not repo.ui.verbose:
                 # silence internal shuffling chatter
                 repo.ui.pushbuffer()
--- a/mercurial/wireproto.py	Mon Apr 14 14:46:32 2014 -0400
+++ b/mercurial/wireproto.py	Mon Apr 14 15:33:50 2014 -0400
@@ -766,7 +766,7 @@
         try:
             proto.getfile(fp)
             fp.seek(0)
-            gen = changegroupmod.readbundle(fp, None)
+            gen = exchange.readbundle(fp, None)
             r = exchange.unbundle(repo, gen, their_heads, 'serve',
                                   proto._client())
             return pushres(r)