瀏覽代碼

Cope with locking.

Jelmer Vernooij 13 年之前
父節點
當前提交
62b640c908
共有 2 個文件被更改,包括 22 次插入3 次删除
  1. 12 3
      dulwich/object_store.py
  2. 10 0
      dulwich/tests/test_object_store.py

+ 12 - 3
dulwich/object_store.py

@@ -387,10 +387,19 @@ class DiskObjectStore(PackBasedObjectStore):
         except OSError, e:
             if e.errno != errno.EEXIST:
                 raise
-        # FIXME: Locking
-        f = GitFile(os.path.join(self.path, "info/alternates"), 'wb')
+        alternates_path = os.path.join(self.path, "info/alternates")
+        f = GitFile(alternates_path, 'wb')
         try:
-            f.seek(0, os.SEEK_END)
+            try:
+                orig_f = open(alternates_path, 'rb')
+            except (OSError, IOError), e:
+                if e.errno != errno.ENOENT:
+                    raise
+            else:
+                try:
+                    f.write(orig_f.read())
+                finally:
+                    orig_f.close()
             f.write("%s\n" % path)
         finally:
             f.close()

+ 10 - 0
dulwich/tests/test_object_store.py

@@ -239,6 +239,16 @@ class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
         store.add_alternate_path(alternate_dir)
         self.assertEquals(b2, store[b2.id])
 
+    def test_add_alternate_path(self):
+        store = DiskObjectStore(self.store_dir)
+        self.assertEquals([], store._read_alternate_paths())
+        store.add_alternate_path("/foo/path")
+        self.assertEquals(["/foo/path"], store._read_alternate_paths())
+        store.add_alternate_path("/bar/path")
+        self.assertEquals(
+            ["/foo/path", "/bar/path"],
+            store._read_alternate_paths())
+
     def test_pack_dir(self):
         o = DiskObjectStore(self.store_dir)
         self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)