changeset 21013:a813caca89b3

bundle2: extract stream/unpack logic in an unpackermixin The coming `unbundlepart` will need the same kind of method than `unbundle20` for unpacking data from the stream. We extract them into a mixin class before the creation of `unbundlepart`.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Fri, 11 Apr 2014 15:19:54 -0400
parents c827a0028e6f
children a6246bba7b9e
files mercurial/bundle2.py
diffstat 1 files changed, 20 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/bundle2.py	Sun Apr 13 12:21:09 2014 -0400
+++ b/mercurial/bundle2.py	Fri Apr 11 15:19:54 2014 -0400
@@ -373,21 +373,11 @@
             blocks.append(par)
         return ' '.join(blocks)
 
-class unbundle20(object):
-    """interpret a bundle2 stream
+class unpackermixin(object):
+    """A mixin to extract bytes and struct data from a stream"""
 
-    (this will eventually yield parts)"""
-
-    def __init__(self, ui, fp):
-        self.ui = ui
+    def __init__(self, fp):
         self._fp = fp
-        header = self._readexact(4)
-        magic, version = header[0:2], header[2:4]
-        if magic != 'HG':
-            raise util.Abort(_('not a Mercurial bundle'))
-        if version != '20':
-            raise util.Abort(_('unknown bundle version %s') % version)
-        self.ui.debug('start processing of %s stream\n' % header)
 
     def _unpack(self, format):
         """unpack this struct format from the stream"""
@@ -398,6 +388,23 @@
         """read exactly <size> bytes from the stream"""
         return changegroup.readexactly(self._fp, size)
 
+
+class unbundle20(unpackermixin):
+    """interpret a bundle2 stream
+
+    (this will eventually yield parts)"""
+
+    def __init__(self, ui, fp):
+        self.ui = ui
+        super(unbundle20, self).__init__(fp)
+        header = self._readexact(4)
+        magic, version = header[0:2], header[2:4]
+        if magic != 'HG':
+            raise util.Abort(_('not a Mercurial bundle'))
+        if version != '20':
+            raise util.Abort(_('unknown bundle version %s') % version)
+        self.ui.debug('start processing of %s stream\n' % header)
+
     @util.propertycache
     def params(self):
         """dictionnary of stream level parameters"""