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 5f62331..4700c77 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 1ad0af7..c7d4916 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 98849b7..1e1f91c 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