pack: Add ThinPackData.from_file.
authorDave Borowitz <dborowitz@google.com>
Mon, 26 Jul 2010 17:52:37 +0000 (19:52 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Mon, 26 Jul 2010 17:52:37 +0000 (19:52 +0200)
NEWS
dulwich/pack.py
dulwich/tests/test_pack.py

diff --git a/NEWS b/NEWS
index 189b57423d2af5b8367df02294b40c35fb34a871..abe297baa8edc885f2893de51d746c67626c3976 100644 (file)
--- 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)
index 59f27c913b48ec508ce0e5770e9ef808d46e27e9..5bcd06e4e95bbd831af709c2c774271f51ebb9af 100644 (file)
@@ -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:
index 2fd5fbb96952bb2fd99187550964b8664e3424dc..129836248088d50b0d5da881ec4b6f6c41c9983c 100644 (file)
@@ -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))