bundle2: make sure the unbundler refuse non bundle2 stream
authorPierre-Yves David <pierre-yves.david@fb.com>
Tue, 18 Mar 2014 16:35:34 -0700
changeset 20803 88db3e615319
parent 20802 520df53ad26a
child 20804 db9d3991d2c6
bundle2: make sure the unbundler refuse non bundle2 stream We now make use of the magic string at the beginning of the file.
mercurial/bundle2.py
tests/test-bundle2.t
--- a/mercurial/bundle2.py	Tue Mar 18 14:28:42 2014 -0700
+++ b/mercurial/bundle2.py	Tue Mar 18 16:35:34 2014 -0700
@@ -62,7 +62,7 @@
 
 import util
 import changegroup
-
+from i18n import _
 
 _magicstring = 'HG20'
 
@@ -93,10 +93,13 @@
     (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"""
--- a/tests/test-bundle2.t	Tue Mar 18 14:28:42 2014 -0700
+++ b/tests/test-bundle2.t	Tue Mar 18 16:35:34 2014 -0700
@@ -38,6 +38,9 @@
 
   $ hg init main
   $ cd main
+  $ touch a
+  $ hg add a
+  $ hg commit -m 'a'
 
 Test simple generation of empty bundle
 
@@ -49,3 +52,11 @@
   $ 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]