Patchwork [3,of,3] unbundle20: move header parsing into the 'getunbundler' function

login
register
mail settings
Submitter Pierre-Yves David
Date April 7, 2015, 6:30 a.m.
Message ID <90af15d249d4fb90d024.1428388254@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/8529/
State Accepted
Commit 54e5c239c2d9ad87fc2080fd9be35765cf0ebc9f
Headers show

Comments

Pierre-Yves David - April 7, 2015, 6:30 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1428361638 25200
#      Mon Apr 06 16:07:18 2015 -0700
# Node ID 90af15d249d4fb90d0248cc25a81f781bf92a133
# Parent  37b1e89e2ffcbd8a8851cb7c84404d47c6dc8e56
unbundle20: move header parsing into the 'getunbundler' function

The dispatching will be based on the header content, so we need to move this
logic in the factory function.

Patch

diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -521,30 +521,31 @@  class unpackermixin(object):
         if util.safehasattr(self._fp, 'close'):
             return self._fp.close()
 
 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
 
     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):
         """dictionary of stream level parameters"""
         self.ui.debug('reading bundle2 stream parameters\n')