Browse Source

Add PackBasedObjectStore.pack_loose_objects().

Jelmer Vernooij 14 năm trước cách đây
mục cha
commit
73a2f131a9
3 tập tin đã thay đổi với 41 bổ sung6 xóa
  1. 2 0
      NEWS
  2. 20 0
      dulwich/object_store.py
  3. 19 6
      dulwich/tests/test_object_store.py

+ 2 - 0
NEWS

@@ -21,6 +21,8 @@
   * Allow overriding paths to executables in GitSSHClient. 
     (Ross Light, Jelmer Vernooij, #585204)
 
+  * Add PackBasedObjectStore.pack_loose_objects(). (Jelmer Vernooij)
+
  TESTS
 
   * Add tests for sorted_tree_items and C implementation. (Dave Borowitz)

+ 20 - 0
dulwich/object_store.py

@@ -272,11 +272,28 @@ class PackBasedObjectStore(BaseObjectStore):
         return self._pack_cache
 
     def _iter_loose_objects(self):
+        """Iterate over the SHAs of all loose objects."""
         raise NotImplementedError(self._iter_loose_objects)
 
     def _get_loose_object(self, sha):
         raise NotImplementedError(self._get_loose_object)
 
+    def _remove_loose_object(self, sha):
+        raise NotImplementedError(self._remove_loose_object)
+
+    def pack_loose_objects(self):
+        """Pack loose objects.
+        
+        :return: Number of objects packed
+        """
+        objects = set()
+        for sha in self._iter_loose_objects():
+            objects.add((self._get_loose_object(sha), None))
+        self.add_objects(objects)
+        for obj, path in objects:
+            self._remove_loose_object(obj.id)
+        return len(objects)
+
     def __iter__(self):
         """Iterate over the SHAs that are present in this store."""
         iterables = self.packs + [self._iter_loose_objects()]
@@ -385,6 +402,9 @@ class DiskObjectStore(PackBasedObjectStore):
                 return None
             raise
 
+    def _remove_loose_object(self, sha):
+        os.remove(self._get_shafile_path(sha))
+
     def move_in_thin_pack(self, path):
         """Move a specific file containing a pack into the pack directory.
 

+ 19 - 6
dulwich/tests/test_object_store.py

@@ -83,7 +83,25 @@ class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
         self.store = MemoryObjectStore()
 
 
-class DiskObjectStoreTests(ObjectStoreTests, TestCase):
+class PackBasedObjectStoreTests(ObjectStoreTests):
+
+    def test_empty_packs(self):
+        o = DiskObjectStore(self.store_dir)
+        self.assertEquals([], o.packs)
+
+    def test_pack_loose_objects(self):
+        o = DiskObjectStore(self.store_dir)
+        b1 = make_object(Blob, data="yummy data")
+        o.add_object(b1)
+        b2 = make_object(Blob, data="more yummy data")
+        o.add_object(b2)
+        self.assertEquals([], o.packs)
+        self.assertEquals(2, o.pack_loose_objects())
+        self.assertNotEquals([], o.packs)
+        self.assertEquals(0, o.pack_loose_objects())
+
+
+class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
 
     def setUp(self):
         TestCase.setUp(self)
@@ -98,9 +116,4 @@ class DiskObjectStoreTests(ObjectStoreTests, TestCase):
         o = DiskObjectStore(self.store_dir)
         self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)
 
-    def test_empty_packs(self):
-        o = DiskObjectStore(self.store_dir)
-        self.assertEquals([], o.packs)
-
-
 # TODO: MissingObjectFinderTests