Browse Source

pack: Add ThinPackData.from_file.

Dave Borowitz 14 years ago
parent
commit
8e2a79ae9d
3 changed files with 27 additions and 0 deletions
  1. 3 0
      NEWS
  2. 4 0
      dulwich/pack.py
  3. 20 0
      dulwich/tests/test_pack.py

+ 3 - 0
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)

+ 4 - 0
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:

+ 20 - 0
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,
@@ -163,6 +164,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))