Skip to content

Commit 87aa78c

Browse files
committed
refs: added constructor flag to allow refs to be instatiated from any path, including simple test
1 parent b7ae99c commit 87aa78c

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

git/refs/reference.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ class Reference(SymbolicReference, LazyMixin, Iterable):
2020
_resolve_ref_on_create = True
2121
_common_path_default = "refs"
2222

23-
def __init__(self, repo, path):
23+
def __init__(self, repo, path, check_path = True):
2424
"""Initialize this instance
2525
:param repo: Our parent repository
2626
2727
:param path:
2828
Path relative to the .git/ directory pointing to the ref in question, i.e.
29-
refs/heads/master"""
30-
if not path.startswith(self._common_path_default+'/'):
31-
raise ValueError("Cannot instantiate %r from path %s, maybe use %s.to_full_path(name) to safely generate a valid full path from a name" % ( self.__class__.__name__, path, type(self).__name__))
29+
refs/heads/master
30+
:param check_path: if False, you can provide any path. Otherwise the path must start with the
31+
default path prefix of this type."""
32+
if check_path and not path.startswith(self._common_path_default+'/'):
33+
raise ValueError("Cannot instantiate %r from path %s" % (self.__class__.__name__, path))
3234
super(Reference, self).__init__(repo, path)
3335

3436

git/test/refs/test_refs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def test_from_path(self):
2929
assert isinstance(instance, ref_type)
3030
# END for each name
3131
# END for each type
32+
33+
# invalid path
34+
self.failUnlessRaises(ValueError, TagReference, self.rorepo, "refs/invalid/tag")
35+
# works without path check
36+
TagReference(self.rorepo, "refs/invalid/tag", check_path=False)
3237

3338
def test_tag_base(self):
3439
tag_object_refs = list()

0 commit comments

Comments
 (0)