瀏覽代碼

Merge fix for use of alternates in DiskObjectStore.__contains__ and DiskObjectStore.__iter__.

Jelmer Vernooij 12 年之前
父節點
當前提交
11e4b3d182
共有 3 個文件被更改,包括 33 次插入3 次删除
  1. 5 0
      NEWS
  2. 27 3
      dulwich/object_store.py
  3. 1 0
      dulwich/tests/test_object_store.py

+ 5 - 0
NEWS

@@ -1,5 +1,10 @@
 0.9.0	UNRELEASED
 
+ BUG FIXES
+
+  * Fix use of alternates in ``DiskObjectStore``.{__contains__,__iter__}.
+    (Dmitriy)
+
 0.8.6	2012-11-09
 
  API CHANGES

+ 27 - 3
dulwich/object_store.py

@@ -231,12 +231,27 @@ class PackBasedObjectStore(BaseObjectStore):
         return []
 
     def contains_packed(self, sha):
-        """Check if a particular object is present by SHA1 and is packed."""
+        """Check if a particular object is present by SHA1 and is packed.
+
+        This does not check alternates.
+        """
         for pack in self.packs:
             if sha in pack:
                 return True
         return False
 
+    def __contains__(self, sha):
+        """Check if a particular object is present by SHA1.
+
+        This method makes no distinction between loose and packed objects.
+        """
+        if self.contains_packed(sha) or self.contains_loose(sha):
+            return True
+        for alternate in self.alternates:
+            if sha in alternate:
+                return True
+        return False
+
     def _load_packs(self):
         raise NotImplementedError(self._load_packs)
 
@@ -258,6 +273,12 @@ class PackBasedObjectStore(BaseObjectStore):
             self._pack_cache = self._load_packs()
         return self._pack_cache
 
+    def _iter_alternate_objects(self):
+        """Iterate over the SHAs of all the objects in alternate stores."""
+        for alternate in self.alternates:
+            for alternate_object in alternate:
+                yield alternate_object
+
     def _iter_loose_objects(self):
         """Iterate over the SHAs of all loose objects."""
         raise NotImplementedError(self._iter_loose_objects)
@@ -283,11 +304,14 @@ class PackBasedObjectStore(BaseObjectStore):
 
     def __iter__(self):
         """Iterate over the SHAs that are present in this store."""
-        iterables = self.packs + [self._iter_loose_objects()]
+        iterables = self.packs + [self._iter_loose_objects()] + [self._iter_alternate_objects()]
         return itertools.chain(*iterables)
 
     def contains_loose(self, sha):
-        """Check if a particular object is present by SHA1 and is loose."""
+        """Check if a particular object is present by SHA1 and is loose.
+
+        This does not check alternates.
+        """
         return self._get_loose_object(sha) is not None
 
     def get_raw(self, name):

+ 1 - 0
dulwich/tests/test_object_store.py

@@ -237,6 +237,7 @@ class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
         store = DiskObjectStore(self.store_dir)
         self.assertRaises(KeyError, store.__getitem__, b2.id)
         store.add_alternate_path(alternate_dir)
+        self.assertIn(b2.id, store)
         self.assertEqual(b2, store[b2.id])
 
     def test_add_alternate_path(self):