Skip to content

Commit b1a2271

Browse files
committed
refs: Added is_valid method to SymbolicReference base class
1 parent bc66da0 commit b1a2271

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/git/refs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ def _set_reference(self, ref):
214214
# alias
215215
ref = reference
216216

217+
def is_valid(self):
218+
"""
219+
Returns
220+
True if the reference is valid, hence it can be read and points to
221+
a valid object or reference.
222+
"""
223+
try:
224+
self.commit
225+
except (OSError, ValueError):
226+
return False
227+
else:
228+
return True
229+
217230
@property
218231
def is_detached(self):
219232
"""

test/git/test_refs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ def test_refs(self):
6666
types_found.add(type(ref))
6767
assert len(types_found) == 3
6868

69+
def test_is_valid(self):
70+
assert Reference(self.rorepo, 'refs/doesnt/exist').is_valid() == False
71+
assert self.rorepo.head.is_valid()
72+
assert self.rorepo.head.reference.is_valid()
73+
assert SymbolicReference(self.rorepo, 'hellothere').is_valid() == False
74+
6975
@with_rw_repo('0.1.6')
7076
def test_head_reset(self, rw_repo):
7177
cur_head = rw_repo.head

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy