← Back to team overview

dulwich-users team mailing list archive

[PATCH 9/9] pack: Add ThinPackData.from_file.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: Ic136588e9fe6fff6cf54441fb111812ad719d409
---
 NEWS                       |    3 +++
 dulwich/pack.py            |    4 ++++
 dulwich/tests/test_pack.py |   20 ++++++++++++++++++++
 3 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/NEWS b/NEWS
index 0d35abd..dd233e4 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,9 @@
 
   * Don't error when creating GitFiles with the default mode. (Dave Borowitz)
 
+  * ThinPackData.from_file now works with resolve_ext_ref callback.
+    (Dave Borowitz)
+
  FEATURES
 
   * Use slots for core objects to save up on memory. (Jelmer Vernooij)
diff --git a/dulwich/pack.py b/dulwich/pack.py
index 59f27c9..5bcd06e 100644
--- a/dulwich/pack.py
+++ b/dulwich/pack.py
@@ -888,6 +888,10 @@ class ThinPackData(PackData):
         super(ThinPackData, self).__init__(*args, **kwargs)
         self.resolve_ext_ref = resolve_ext_ref
 
+    @classmethod
+    def from_file(cls, resolve_ext_ref, file, size):
+        return cls(resolve_ext_ref, str(file), file=file, size=size)
+
     def get_ref(self, sha):
         """Resolve a reference looking in both this pack and the store."""
         try:
diff --git a/dulwich/tests/test_pack.py b/dulwich/tests/test_pack.py
index b6aea48..5ea6207 100644
--- a/dulwich/tests/test_pack.py
+++ b/dulwich/tests/test_pack.py
@@ -40,6 +40,7 @@ from dulwich.objects import (
 from dulwich.pack import (
     Pack,
     PackData,
+    ThinPackData,
     apply_delta,
     create_delta,
     load_pack_index,
@@ -162,6 +163,25 @@ class TestPackData(PackTests):
     def test_create_pack(self):
         p = self.get_pack_data(pack1_sha)
 
+    def test_from_file(self):
+        path = os.path.join(self.datadir, 'pack-%s.pack' % pack1_sha)
+        PackData.from_file(open(path), os.path.getsize(path))
+
+    # TODO: more ThinPackData tests.
+    def test_thin_from_file(self):
+        test_sha = '1' * 40
+
+        def resolve(sha):
+            self.assertEqual(test_sha, sha)
+            return 3, 'data'
+
+        path = os.path.join(self.datadir, 'pack-%s.pack' % pack1_sha)
+        data = ThinPackData.from_file(resolve, open(path),
+                                      os.path.getsize(path))
+        idx = self.get_pack_index(pack1_sha)
+        Pack.from_objects(data, idx)
+        self.assertEqual((None, 3, 'data'), data.get_ref(test_sha))
+
     def test_pack_len(self):
         p = self.get_pack_data(pack1_sha)
         self.assertEquals(3, len(p))
-- 
1.7.1




References