diff options
-rw-r--r-- | lib/git/refs.py | 13 | ||||
-rw-r--r-- | test/git/test_refs.py | 6 |
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/git/refs.py b/lib/git/refs.py index bedca7b2..4941e0a6 100644 --- a/lib/git/refs.py +++ b/lib/git/refs.py @@ -214,6 +214,19 @@ class SymbolicReference(object): # alias ref = reference + def is_valid(self): + """ + Returns + True if the reference is valid, hence it can be read and points to + a valid object or reference. + """ + try: + self.commit + except (OSError, ValueError): + return False + else: + return True + @property def is_detached(self): """ diff --git a/test/git/test_refs.py b/test/git/test_refs.py index 2cddf862..61c421fd 100644 --- a/test/git/test_refs.py +++ b/test/git/test_refs.py @@ -66,6 +66,12 @@ class TestRefs(TestBase): types_found.add(type(ref)) assert len(types_found) == 3 + def test_is_valid(self): + assert Reference(self.rorepo, 'refs/doesnt/exist').is_valid() == False + assert self.rorepo.head.is_valid() + assert self.rorepo.head.reference.is_valid() + assert SymbolicReference(self.rorepo, 'hellothere').is_valid() == False + @with_rw_repo('0.1.6') def test_head_reset(self, rw_repo): cur_head = rw_repo.head |