Przeglądaj źródła

Add Pack.pack_tuples().

Jelmer Vernooij 13 lat temu
rodzic
commit
cc46acdf24

+ 1 - 1
dulwich/object_store.py

@@ -407,7 +407,7 @@ class DiskObjectStore(PackBasedObjectStore):
             # Write a full pack version
             temppath = os.path.join(self.pack_dir,
                 sha_to_hex(urllib2.randombytes(20))+".temppack")
-            write_pack(temppath, [(o, None) for o in p.iterobjects()])
+            write_pack(temppath, p.pack_tuples())
         finally:
             p.close()
 

+ 19 - 0
dulwich/pack.py

@@ -1510,6 +1510,25 @@ class Pack(object):
             yield ShaFile.from_raw_chunks(
               *self.data.resolve_object(offset, type, obj))
 
+    def pack_tuples(self):
+        """Provide an iterable for use with write_pack_data.
+
+        :return: Object that can iterate over (object, path) tuples
+            and provides __len__
+        """
+        class PackTupleIterable(object):
+
+            def __init__(self, pack):
+                self.pack = pack
+
+            def __len__(self):
+                return len(self.pack)
+
+            def __iter__(self):
+                return ((o, None) for o in self.pack.iterobjects())
+
+        return PackTupleIterable(self)
+
     def keep(self, msg=None):
         """Add a .keep file for the pack, preventing git from garbage collecting it.
 

+ 1 - 1
dulwich/tests/compat/test_pack.py

@@ -54,7 +54,7 @@ class TestPack(PackTests):
         origpack = self.get_pack(pack1_sha)
         self.assertSucceeds(origpack.index.check)
         pack_path = os.path.join(self._tempdir, "Elch")
-        write_pack(pack_path, [(x, "") for x in origpack.iterobjects()])
+        write_pack(pack_path, origpack.pack_tuples())
         output = run_git_or_fail(['verify-pack', '-v', pack_path])
 
         pack_shas = set()

+ 15 - 2
dulwich/tests/test_pack.py

@@ -257,6 +257,19 @@ class TestPack(PackTests):
         p = self.get_pack(pack1_sha)
         self.assertEquals(set([tree_sha, commit_sha, a_sha]), set(p))
 
+    def test_iterobjects(self):
+        p = self.get_pack(pack1_sha)
+        expected = set([p[s] for s in [commit_sha, tree_sha, a_sha]])
+        self.assertEquals(expected, set(list(p.iterobjects())))
+
+    def test_pack_tuples(self):
+        p = self.get_pack(pack1_sha)
+        tuples = p.pack_tuples()
+        expected = set([(p[s], None) for s in [commit_sha, tree_sha, a_sha]])
+        self.assertEquals(expected, set(list(tuples)))
+        self.assertEquals(expected, set(list(tuples)))
+        self.assertEquals(3, len(tuples))
+
     def test_get_object_at(self):
         """Tests random access for non-delta objects"""
         p = self.get_pack(pack1_sha)
@@ -276,7 +289,7 @@ class TestPack(PackTests):
         try:
             self.assertSucceeds(origpack.index.check)
             basename = os.path.join(self.tempdir, 'Elch')
-            write_pack(basename, [(x, '') for x in origpack.iterobjects()])
+            write_pack(basename, origpack.pack_tuples())
             newpack = Pack(basename)
 
             try:
@@ -305,7 +318,7 @@ class TestPack(PackTests):
 
     def _copy_pack(self, origpack):
         basename = os.path.join(self.tempdir, 'somepack')
-        write_pack(basename, [(x, '') for x in origpack.iterobjects()])
+        write_pack(basename, origpack.pack_tuples())
         return Pack(basename)
 
     def test_keep_no_message(self):