Implement BaseRepo.__contains__.
authorJelmer Vernooij <jelmer@samba.org>
Sat, 1 May 2010 11:21:15 +0000 (13:21 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Sat, 1 May 2010 11:21:15 +0000 (13:21 +0200)
dulwich/repo.py
dulwich/tests/test_repository.py

index 983afc9f26c0946fd60abeec4b74a53ecefe4c8e..3148b43712c7cd7db0034190aa15f6568bdbc7cc 100644 (file)
@@ -844,9 +844,18 @@ class BaseRepo(object):
 
     def __getitem__(self, name):
         if len(name) in (20, 40):
-            return self.object_store[name]
+            try:
+                return self.object_store[name]
+            except KeyError:
+                pass
         return self.object_store[self.refs[name]]
 
+    def __contains__(self, name):
+        if len(name) in (20, 40):
+            return name in self.object_store or name in self.refs
+        else:
+            return name in self.refs
+
     def __setitem__(self, name, value):
         if name.startswith("refs/") or name == "HEAD":
             if isinstance(value, ShaFile):
index 3048ca9effbb999870da304f9782ca706ff51ff4..e1ea7ede772bb7cccd09cbebe512cd2d054a3f01 100644 (file)
@@ -102,6 +102,18 @@ class RepositoryTests(unittest.TestCase):
         r = self._repo = open_repo('a.git')
         self.assertRaises(KeyError, r.get_object, missing_sha)
 
+    def test_contains_object(self):
+        r = self._repo = open_repo('a.git')
+        self.assertTrue(r.head() in r)
+
+    def test_contains_ref(self):
+        r = self._repo = open_repo('a.git')
+        self.assertTrue("HEAD" in r)
+
+    def test_contains_missing(self):
+        r = self._repo = open_repo('a.git')
+        self.assertFalse("bar" in r)
+
     def test_commit(self):
         r = self._repo = open_repo('a.git')
         warnings.simplefilter("ignore", DeprecationWarning)