Explorar o código

Add abort return value to ObjectStore.add_pack.

Jelmer Vernooij %!s(int64=11) %!d(string=hai) anos
pai
achega
441caf1feb
Modificáronse 4 ficheiros con 60 adicións e 25 borrados
  1. 4 0
      NEWS
  2. 9 4
      dulwich/client.py
  3. 29 13
      dulwich/object_store.py
  4. 18 8
      dulwich/tests/test_object_store.py

+ 4 - 0
NEWS

@@ -39,6 +39,10 @@
   * SSHVendor.connect_ssh has been renamed to SSHVendor.run_command.
     (Jelmer Vernooij)
 
+  * ObjectStore.add_pack() now returns a 3-tuple. The last element will be an
+    abort() method that can be used to cancel the pack operation.
+    (Jelmer Vernooij)
+
 0.9.0	2013-05-31
 
  BUG FIXES

+ 9 - 4
dulwich/client.py

@@ -202,10 +202,15 @@ class GitClient(object):
         """
         if determine_wants is None:
             determine_wants = target.object_store.determine_wants_all
-        f, commit = target.object_store.add_pack()
-        result = self.fetch_pack(path, determine_wants,
-                target.get_graph_walker(), f.write, progress)
-        commit()
+        f, commit, abort = target.object_store.add_pack()
+        try:
+            result = self.fetch_pack(path, determine_wants,
+                    target.get_graph_walker(), f.write, progress)
+        except:
+            abort()
+            raise
+        else:
+            commit()
         return result
 
     def fetch_pack(self, path, determine_wants, graph_walker, pack_data,

+ 29 - 13
dulwich/object_store.py

@@ -381,9 +381,14 @@ class PackBasedObjectStore(BaseObjectStore):
         if len(objects) == 0:
             # Don't bother writing an empty pack file
             return
-        f, commit = self.add_pack()
-        write_pack_objects(f, objects)
-        return commit()
+        f, commit, abort = self.add_pack()
+        try:
+            write_pack_objects(f, objects)
+        except:
+            abort()
+            raise
+        else:
+            return commit()
 
 
 class DiskObjectStore(PackBasedObjectStore):
@@ -618,8 +623,9 @@ class DiskObjectStore(PackBasedObjectStore):
     def add_pack(self):
         """Add a new pack to this object store.
 
-        :return: Fileobject to write to and a commit function to
-            call when the pack is finished.
+        :return: Fileobject to write to, a commit function to
+            call when the pack is finished and an abort
+            function.
         """
         fd, path = tempfile.mkstemp(dir=self.pack_dir, suffix=".pack")
         f = os.fdopen(fd, 'wb')
@@ -631,7 +637,10 @@ class DiskObjectStore(PackBasedObjectStore):
             else:
                 os.remove(path)
                 return None
-        return f, commit
+        def abort():
+            f.close()
+            os.remove(path)
+        return f, commit, abort
 
     def add_object(self, obj):
         """Add a single object to this object store.
@@ -742,7 +751,9 @@ class MemoryObjectStore(BaseObjectStore):
             f.close()
             for obj in PackInflater.for_pack_data(p):
                 self._data[obj.id] = obj
-        return f, commit
+        def abort():
+            pass
+        return f, commit, abort
 
     def _complete_thin_pack(self, f, indexer):
         """Complete a thin pack by adding external references.
@@ -779,12 +790,17 @@ class MemoryObjectStore(BaseObjectStore):
         :param read_some: Read function that returns at least one byte, but may
             not return the number of bytes requested.
         """
-        f, commit = self.add_pack()
-        indexer = PackIndexer(f, resolve_ext_ref=self.get_raw)
-        copier = PackStreamCopier(read_all, read_some, f, delta_iter=indexer)
-        copier.verify()
-        self._complete_thin_pack(f, indexer)
-        commit()
+        f, commit, abort = self.add_pack()
+        try:
+            indexer = PackIndexer(f, resolve_ext_ref=self.get_raw)
+            copier = PackStreamCopier(read_all, read_some, f, delta_iter=indexer)
+            copier.verify()
+            self._complete_thin_pack(f, indexer)
+        except:
+            abort()
+            raise
+        else:
+            commit()
 
 
 class ObjectImporter(object):

+ 18 - 8
dulwich/tests/test_object_store.py

@@ -197,10 +197,15 @@ class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
 
     def test_add_pack(self):
         o = MemoryObjectStore()
-        f, commit = o.add_pack()
-        b = make_object(Blob, data="more yummy data")
-        write_pack_objects(f, [(b, None)])
-        commit()
+        f, commit, abort = o.add_pack()
+        try:
+            b = make_object(Blob, data="more yummy data")
+            write_pack_objects(f, [(b, None)])
+        except:
+            abort()
+            raise
+        else:
+            commit()
 
     def test_add_thin_pack(self):
         o = MemoryObjectStore()
@@ -290,10 +295,15 @@ class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
 
     def test_add_pack(self):
         o = DiskObjectStore(self.store_dir)
-        f, commit = o.add_pack()
-        b = make_object(Blob, data="more yummy data")
-        write_pack_objects(f, [(b, None)])
-        commit()
+        f, commit, abort = o.add_pack()
+        try:
+            b = make_object(Blob, data="more yummy data")
+            write_pack_objects(f, [(b, None)])
+        except:
+            abort()
+            raise
+        else:
+            commit()
 
     def test_add_thin_pack(self):
         o = DiskObjectStore(self.store_dir)