unbundle20: move header parsing into the 'getunbundler' function
authorPierre-Yves David <pierre-yves.david@fb.com>
Mon, 06 Apr 2015 16:07:18 -0700
changeset 24642 54e5c239c2d9
parent 24641 60fecc5b14a4
child 24643 a8e6897dffbe
unbundle20: move header parsing into the 'getunbundler' function The dispatching will be based on the header content, so we need to move this logic into the factory function.
mercurial/bundle2.py
--- a/mercurial/bundle2.py	Mon Apr 06 16:04:33 2015 -0700
+++ b/mercurial/bundle2.py	Mon Apr 06 16:07:18 2015 -0700
@@ -523,7 +523,16 @@
 
 def getunbundler(ui, fp, header=None):
     """return a valid unbundler object for a given header"""
-    return unbundle20(ui, fp, header)
+    if header is None:
+        header = changegroup.readexactly(fp, 4)
+        magic, version = header[0:2], header[2:4]
+        if magic != 'HG':
+            raise util.Abort(_('not a Mercurial bundle'))
+        if version != '2Y':
+            raise util.Abort(_('unknown bundle version %s') % version)
+    unbundler = unbundle20(ui, fp)
+    ui.debug('start processing of %s stream\n' % header)
+    return unbundler
 
 class unbundle20(unpackermixin):
     """interpret a bundle2 stream
@@ -531,18 +540,10 @@
     This class is fed with a binary stream and yields parts through its
     `iterparts` methods."""
 
-    def __init__(self, ui, fp, header=None):
+    def __init__(self, ui, fp):
         """If header is specified, we do not read it out of the stream."""
         self.ui = ui
         super(unbundle20, self).__init__(fp)
-        if header is None:
-            header = self._readexact(4)
-            magic, version = header[0:2], header[2:4]
-            if magic != 'HG':
-                raise util.Abort(_('not a Mercurial bundle'))
-            if version != '2Y':
-                raise util.Abort(_('unknown bundle version %s') % version)
-        self.ui.debug('start processing of %s stream\n' % header)
 
     @util.propertycache
     def params(self):