Skip to content

Commit 916c45d

Browse files
committed
refs: added constructor flag to allow refs to be instatiated from any path, including simple test
1 parent e94df6a commit 916c45d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

git/refs/reference.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ class Reference(SymbolicReference, LazyMixin, Iterable):
2222
_resolve_ref_on_create = True
2323
_common_path_default = "refs"
2424

25-
def __init__(self, repo, path):
25+
def __init__(self, repo, path, check_path = True):
2626
"""Initialize this instance
2727
:param repo: Our parent repository
2828
2929
:param path:
3030
Path relative to the .git/ directory pointing to the ref in question, i.e.
31-
refs/heads/master"""
32-
if not path.startswith(self._common_path_default+'/'):
33-
raise ValueError("Cannot instantiate %r from path %s" % ( self.__class__.__name__, path ))
31+
refs/heads/master
32+
:param check_path: if False, you can provide any path. Otherwise the path must start with the
33+
default path prefix of this type."""
34+
if check_path and not path.startswith(self._common_path_default+'/'):
35+
raise ValueError("Cannot instantiate %r from path %s" % (self.__class__.__name__, path))
3436
super(Reference, self).__init__(repo, path)
3537

3638

git/test/test_refs.py

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def test_from_path(self):
2424
assert isinstance(instance, ref_type)
2525
# END for each name
2626
# END for each type
27+
28+
# invalid path
29+
self.failUnlessRaises(ValueError, TagReference, self.rorepo, "refs/invalid/tag")
30+
# works without path check
31+
TagReference(self.rorepo, "refs/invalid/tag", check_path=False)
2732

2833
def test_tag_base(self):
2934
tag_object_refs = list()

0 commit comments

Comments
 (0)