← Back to team overview

dulwich-users team mailing list archive

[PATCH 04/13] pack: Allow reading of empty packs.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: Iece4767a55e6c0763eb1108944fed613608ea2f3
---
 NEWS                       |    2 ++
 dulwich/pack.py            |    8 +++++++-
 dulwich/tests/test_pack.py |    4 ++++
 3 files changed, 13 insertions(+), 1 deletions(-)

diff --git a/NEWS b/NEWS
index 5d953fb..d5441c4 100644
--- a/NEWS
+++ b/NEWS
@@ -45,6 +45,8 @@
 
   * Teach the server how to serve a clone of an empty repo. (Dave Borowitz)
 
+  * Teach ReceivePackHandler how to read empty packs. (Dave Borowitz)
+
  API CHANGES
 
   * write_pack no longer takes the num_objects argument and requires an object
diff --git a/dulwich/pack.py b/dulwich/pack.py
index cd74045..52e79e0 100644
--- a/dulwich/pack.py
+++ b/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(
diff --git a/dulwich/tests/test_pack.py b/dulwich/tests/test_pack.py
index eb56711..1592aad 100644
--- a/dulwich/tests/test_pack.py
+++ b/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):
 
-- 
1.7.3.1



References