浏览代码

Implement BaseRepo.__contains__.

Jelmer Vernooij 15 年之前
父节点
当前提交
b69ec0f8f1
共有 2 个文件被更改,包括 22 次插入1 次删除
  1. 10 1
      dulwich/repo.py
  2. 12 0
      dulwich/tests/test_repository.py

+ 10 - 1
dulwich/repo.py

@@ -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):

+ 12 - 0
dulwich/tests/test_repository.py

@@ -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)