diff options
-rw-r--r-- | git/refs/reference.py | 10 | ||||
-rw-r--r-- | git/test/refs/test_refs.py | 5 |
2 files changed, 11 insertions, 4 deletions
diff --git a/git/refs/reference.py b/git/refs/reference.py index 5cff74bb..bd9c47a6 100644 --- a/git/refs/reference.py +++ b/git/refs/reference.py @@ -20,15 +20,17 @@ class Reference(SymbolicReference, LazyMixin, Iterable): _resolve_ref_on_create = True _common_path_default = "refs" - def __init__(self, repo, path): + def __init__(self, repo, path, check_path = True): """Initialize this instance :param repo: Our parent repository :param path: Path relative to the .git/ directory pointing to the ref in question, i.e. - refs/heads/master""" - if not path.startswith(self._common_path_default+'/'): - 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__)) + refs/heads/master + :param check_path: if False, you can provide any path. Otherwise the path must start with the + default path prefix of this type.""" + if check_path and not path.startswith(self._common_path_default+'/'): + raise ValueError("Cannot instantiate %r from path %s" % (self.__class__.__name__, path)) super(Reference, self).__init__(repo, path) diff --git a/git/test/refs/test_refs.py b/git/test/refs/test_refs.py index 668dd94c..81be173c 100644 --- a/git/test/refs/test_refs.py +++ b/git/test/refs/test_refs.py @@ -29,6 +29,11 @@ class TestRefs(TestBase): assert isinstance(instance, ref_type) # END for each name # END for each type + + # invalid path + self.failUnlessRaises(ValueError, TagReference, self.rorepo, "refs/invalid/tag") + # works without path check + TagReference(self.rorepo, "refs/invalid/tag", check_path=False) def test_tag_base(self): tag_object_refs = list() |