Add PackBasedObjectStore.pack_loose_objects().
authorJelmer Vernooij <jelmer@samba.org>
Mon, 28 Jun 2010 20:19:22 +0000 (22:19 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Mon, 28 Jun 2010 20:19:22 +0000 (22:19 +0200)
NEWS
dulwich/object_store.py
dulwich/tests/test_object_store.py

diff --git a/NEWS b/NEWS
index 5f62331adb30c22d7d13c9baf8a779d0dfdd57b7..4700c776a69beb1bdac69eacfed1eb7eac1aa1df 100644 (file)
--- a/NEWS
+++ b/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)
index 1ad0af73c672d25541c7c92ea8fa1a43d98fef0b..c7d4916c33e70305c461660340f397d824afbbbf 100644 (file)
@@ -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.
 
index 98849b7e60852784b77e50d6b49d698ab71d1814..1e1f91c9b3d240a1cd74a746d24c0118e43f90e3 100644 (file)
@@ -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