[PATCH 3 of 5 V2] bundle2: make sure the unbundler refuse non bundle2 stream

pierre-yves.david at ens-lyon.org pierre-yves.david at ens-lyon.org
Thu Mar 20 13:39:44 CDT 2014


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1395185734 25200
#      Tue Mar 18 16:35:34 2014 -0700
# Node ID 9793c682d01acbaace3604a2d517b0b12d64d58e
# Parent  82f930ee78d4cdb25789e0b78a61b3dec99990d3
bundle2: make sure the unbundler refuse non bundle2 stream

We now make use of the magic string at the beginning of the file.

diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py
--- a/mercurial/bundle2.py
+++ b/mercurial/bundle2.py
@@ -60,11 +60,11 @@ Binary format is as follow
   Currently forced to 0 in the current state of the implementation
 """
 
 import util
 import changegroup
-
+from i18n import _
 
 _magicstring = 'HG20'
 
 class bundler(object):
     """represent an outgoing bundle2 container
@@ -91,14 +91,17 @@ class unbundler(object):
     """interpret a bundle2 stream
 
     (this will eventually yield parts)"""
 
     def __init__(self, fp):
-        # assume the magic string is ok and drop it
-        # to be obviously fixed soon.
         self._fp = fp
-        self._readexact(4)
+        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)
 
     def _unpack(self, format):
         """unpack this struct format from the stream"""
         data = self._readexact(struct.calcsize(format))
         return _unpack(format, data)
diff --git a/tests/test-bundle2.t b/tests/test-bundle2.t
--- a/tests/test-bundle2.t
+++ b/tests/test-bundle2.t
@@ -36,10 +36,13 @@ Create an extension to test bundle2 API
 
 The extension requires a repo (currently unused)
 
   $ hg init main
   $ cd main
+  $ touch a
+  $ hg add a
+  $ hg commit -m 'a'
 
 Test simple generation of empty bundle
 
   $ hg bundle2
   HG20\x00\x00\x00\x00 (no-eol) (esc)
@@ -47,5 +50,13 @@ Test simple generation of empty bundle
 Test parsing of an empty bundle
 
   $ hg bundle2 | hg unbundle2
   options count: 0
   parts count:   0
+
+Test old style bundle are detected and refused
+
+  $ hg bundle --all ../bundle.hg
+  1 changesets found
+  $ hg unbundle2 < ../bundle.hg
+  abort: unknown bundle version 10
+  [255]


More information about the Mercurial-devel mailing list