Browse Source

Merge Pack.keep support from Marc Brinkmann.

Jelmer Vernooij 14 years ago
parent
commit
b951ca4fae
3 changed files with 31 additions and 1 deletions
  1. 2 0
      NEWS
  2. 16 1
      dulwich/pack.py
  3. 13 0
      dulwich/tests/test_pack.py

+ 2 - 0
NEWS

@@ -16,6 +16,8 @@
 
   * Sphinxified documentation. (Lukasz Balcerzak)
 
+  * Add Pack.keep.(Marc Brinkmann)
+
  API CHANGES
 
   * The order of the parameters to Tree.add(name, mode, sha) has changed, and

+ 16 - 1
dulwich/pack.py

@@ -1394,7 +1394,7 @@ class Pack(object):
 
     @classmethod
     def from_lazy_objects(self, data_fn, idx_fn):
-        """Create a new pack object from callables to load pack data and 
+        """Create a new pack object from callables to load pack data and
         index objects."""
         ret = Pack("")
         ret._data_load = data_fn
@@ -1498,6 +1498,21 @@ class Pack(object):
             yield ShaFile.from_raw_chunks(
               *self.data.resolve_object(offset, type, obj))
 
+    def keep(self, msg=None):
+        """Add a .keep file for the pack, preventing git from garbage collecting it.
+           :param msg: A message written inside the .keep file; can be used later to
+                       determine whether or not a .keep file is obsolete.
+           :return: The path of the .keep file, as a string."""
+        keepfile_name = '%s.keep' % self._basename
+        keepfile = GitFile(keepfile_name, 'wb')
+        try:
+            if msg:
+                keepfile.write(msg)
+                keepfile.write('\n')
+        finally:
+            keepfile.close()
+        return keepfile_name
+
 
 try:
     from dulwich._pack import apply_delta, bisect_find_sha

+ 13 - 0
dulwich/tests/test_pack.py

@@ -304,6 +304,19 @@ class TestPack(PackTests):
                           commit.author)
         self.assertEquals([], commit.parents)
 
+    def test_keep(self):
+        p = self.get_pack(pack1_sha)
+        msg = 'some message'
+        keepfile_name = p.keep(msg)
+
+        # file should exist
+        self.assertTrue(os.path.exists(keepfile_name))
+
+        # and contain the right message, with a linefeed
+        buf = file(keepfile_name).read()
+
+        self.assertEqual(msg + '\n', buf)
+
     def test_name(self):
         p = self.get_pack(pack1_sha)
         self.assertEquals(pack1_sha, p.name())