Bladeren bron

pack: Allow reading of empty packs.

Change-Id: Iece4767a55e6c0763eb1108944fed613608ea2f3
Dave Borowitz 13 jaren geleden
bovenliggende
commit
a88869bf56
3 gewijzigde bestanden met toevoegingen van 13 en 1 verwijderingen
  1. 2 0
      NEWS
  2. 7 1
      dulwich/pack.py
  3. 4 0
      dulwich/tests/test_pack.py

+ 2 - 0
NEWS

@@ -43,6 +43,8 @@
     performance when wide revision graphs are involved.
     (Jelmer Vernooij, #818168)
 
+  * Teach ReceivePackHandler how to read empty packs. (Dave Borowitz)
+
  API CHANGES
 
   * write_pack no longer takes the num_objects argument and requires an object

+ 7 - 1
dulwich/pack.py

@@ -631,9 +631,12 @@ def read_pack_header(read):
     """Read the header of a pack file.
 
     :param read: Read function
-    :return: Tuple with pack version and number of objects
+    :return: Tuple of (pack version, number of objects). If no data is available
+        to read, returns (None, None).
     """
     header = read(12)
+    if not header:
+        return None, None
     assert header[:4] == 'PACK'
     (version,) = unpack_from('>L', header, 4)
     assert version in (2, 3), 'Version was %d' % version
@@ -817,6 +820,9 @@ class PackStreamReader(object):
         :raise IOError: if an error occurred writing to the output file.
         """
         pack_version, self._num_objects = read_pack_header(self.read)
+        if pack_version is None:
+            return
+
         for i in xrange(self._num_objects):
             offset = self.offset
             unpacked, unused = unpack_object(

+ 4 - 0
dulwich/tests/test_pack.py

@@ -708,6 +708,10 @@ class TestPackStreamReader(TestCase):
         reader = PackStreamReader(f.read, zlib_bufsize=4)
         self.assertEqual(2, len(list(reader.read_objects())))
 
+    def test_read_objects_empty(self):
+        reader = PackStreamReader(StringIO().read)
+        self.assertEqual([], list(reader.read_objects()))
+
 
 class TestPackIterator(DeltaChainIterator):