|
@@ -19,6 +19,12 @@
|
|
|
"""Object specification."""
|
|
|
|
|
|
|
|
|
+def to_bytes(text):
|
|
|
+ if getattr(text, "encode", None) is not None:
|
|
|
+ text = text.encode('ascii')
|
|
|
+ return text
|
|
|
+
|
|
|
+
|
|
|
def parse_object(repo, objectish):
|
|
|
"""Parse a string referring to an object.
|
|
|
|
|
@@ -27,21 +33,19 @@ def parse_object(repo, objectish):
|
|
|
:return: A git object
|
|
|
:raise KeyError: If the object can not be found
|
|
|
"""
|
|
|
- if getattr(objectish, "encode", None) is not None:
|
|
|
- objectish = objectish.encode('ascii')
|
|
|
+ objectish = to_bytes(objectish)
|
|
|
return repo[objectish]
|
|
|
|
|
|
|
|
|
-def parse_refspec(container, refspec):
|
|
|
- """Parse a string referring to an reference.
|
|
|
+def parse_ref(container, refspec):
|
|
|
+ """Parse a string referring to a reference.
|
|
|
|
|
|
:param container: A RefsContainer object
|
|
|
:param refspec: A string referring to a ref
|
|
|
:return: A ref
|
|
|
:raise KeyError: If the ref can not be found
|
|
|
"""
|
|
|
- if getattr(refspec, "encode", None) is not None:
|
|
|
- refspec = refspec.encode('ascii')
|
|
|
+ refspec = to_bytes(refspec)
|
|
|
for ref in [refspec, b"refs/heads/" + refspec]:
|
|
|
if ref in container:
|
|
|
return ref
|
|
@@ -49,6 +53,76 @@ def parse_refspec(container, refspec):
|
|
|
raise KeyError(refspec)
|
|
|
|
|
|
|
|
|
+def parse_reftuple(lh_container, rh_container, refspec):
|
|
|
+ """Parse a reftuple spec.
|
|
|
+
|
|
|
+ :param lh_container: A RefsContainer object
|
|
|
+ :param hh_container: A RefsContainer object
|
|
|
+ :param refspec: A string
|
|
|
+ :return: A tuple with left and right ref
|
|
|
+ :raise KeyError: If one of the refs can not be found
|
|
|
+ """
|
|
|
+ if refspec.startswith("+"):
|
|
|
+ force = True
|
|
|
+ refspec = refspec[1:]
|
|
|
+ else:
|
|
|
+ force = False
|
|
|
+ refspec = to_bytes(refspec)
|
|
|
+ if b":" in refspec:
|
|
|
+ (lh, rh) = refspec.split(b":")
|
|
|
+ else:
|
|
|
+ lh = rh = refspec
|
|
|
+ if rh == "":
|
|
|
+ lh = None
|
|
|
+ else:
|
|
|
+ lh = parse_ref(lh_container, lh)
|
|
|
+ if rh == "":
|
|
|
+ rh = None
|
|
|
+ else:
|
|
|
+ try:
|
|
|
+ rh = parse_ref(rh_container, rh)
|
|
|
+ except KeyError:
|
|
|
+ # TODO: check force?
|
|
|
+ if not b"/" in rh:
|
|
|
+ rh = b"refs/heads/" + rh
|
|
|
+ return (lh, rh, force)
|
|
|
+
|
|
|
+
|
|
|
+def parse_reftuples(lh_container, rh_container, refspecs):
|
|
|
+ """Parse a list of reftuple specs to a list of reftuples.
|
|
|
+
|
|
|
+ :param lh_container: A RefsContainer object
|
|
|
+ :param hh_container: A RefsContainer object
|
|
|
+ :param refspecs: A list of refspecs or a string
|
|
|
+ :return: A list of refs
|
|
|
+ :raise KeyError: If one of the refs can not be found
|
|
|
+ """
|
|
|
+ if not isinstance(refspecs, list):
|
|
|
+ refspecs = [refspecs]
|
|
|
+ ret = []
|
|
|
+ # TODO: Support * in refspecs
|
|
|
+ for refspec in refspecs:
|
|
|
+ ret.append(parse_reftuple(lh_container, rh_container, refspec))
|
|
|
+ return ret
|
|
|
+
|
|
|
+
|
|
|
+def parse_refs(container, refspecs):
|
|
|
+ """Parse a list of refspecs to a list of refs.
|
|
|
+
|
|
|
+ :param container: A RefsContainer object
|
|
|
+ :param refspecs: A list of refspecs or a string
|
|
|
+ :return: A list of refs
|
|
|
+ :raise KeyError: If one of the refs can not be found
|
|
|
+ """
|
|
|
+ # TODO: Support * in refspecs
|
|
|
+ if not isinstance(refspecs, list):
|
|
|
+ refspecs = [refspecs]
|
|
|
+ ret = []
|
|
|
+ for refspec in refspecs:
|
|
|
+ ret.append(parse_ref(container, refspec))
|
|
|
+ return ret
|
|
|
+
|
|
|
+
|
|
|
def parse_commit_range(repo, committishs):
|
|
|
"""Parse a string referring to a range of commits.
|
|
|
|
|
@@ -58,6 +132,5 @@ def parse_commit_range(repo, committishs):
|
|
|
:raise KeyError: When the reference commits can not be found
|
|
|
:raise ValueError: If the range can not be parsed
|
|
|
"""
|
|
|
- if getattr(committishs, "encode", None) is not None:
|
|
|
- committishs = committishs.encode('ascii')
|
|
|
+ committishs = to_bytes(committishs)
|
|
|
return iter([repo[committishs]])
|