2
0

objectspec.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # objectspec.py -- Object specification
  2. # Copyright (C) 2014 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Object specification."""
  21. from collections.abc import Iterator
  22. from typing import TYPE_CHECKING, Optional, Union
  23. from .objects import Commit, ShaFile, Tree
  24. if TYPE_CHECKING:
  25. from .refs import Ref, RefsContainer
  26. from .repo import Repo
  27. def to_bytes(text: Union[str, bytes]) -> bytes:
  28. if getattr(text, "encode", None) is not None:
  29. text = text.encode("ascii") # type: ignore
  30. return text # type: ignore
  31. def parse_object(repo: "Repo", objectish: Union[bytes, str]) -> "ShaFile":
  32. """Parse a string referring to an object.
  33. Args:
  34. repo: A `Repo` object
  35. objectish: A string referring to an object
  36. Returns: A git object
  37. Raises:
  38. KeyError: If the object can not be found
  39. """
  40. objectish = to_bytes(objectish)
  41. return repo[objectish]
  42. def parse_tree(repo: "Repo", treeish: Union[bytes, str]) -> "Tree":
  43. """Parse a string referring to a tree.
  44. Args:
  45. repo: A `Repo` object
  46. treeish: A string referring to a tree
  47. Returns: A git object
  48. Raises:
  49. KeyError: If the object can not be found
  50. """
  51. treeish = to_bytes(treeish)
  52. try:
  53. treeish = parse_ref(repo, treeish)
  54. except KeyError: # treeish is commit sha
  55. pass
  56. o = repo[treeish]
  57. if o.type_name == b"commit":
  58. return repo[o.tree]
  59. return o
  60. def parse_ref(
  61. container: Union["Repo", "RefsContainer"], refspec: Union[str, bytes]
  62. ) -> "Ref":
  63. """Parse a string referring to a reference.
  64. Args:
  65. container: A RefsContainer object
  66. refspec: A string referring to a ref
  67. Returns: A ref
  68. Raises:
  69. KeyError: If the ref can not be found
  70. """
  71. refspec = to_bytes(refspec)
  72. possible_refs = [
  73. refspec,
  74. b"refs/" + refspec,
  75. b"refs/tags/" + refspec,
  76. b"refs/heads/" + refspec,
  77. b"refs/remotes/" + refspec,
  78. b"refs/remotes/" + refspec + b"/HEAD",
  79. ]
  80. for ref in possible_refs:
  81. if ref in container:
  82. return ref
  83. raise KeyError(refspec)
  84. def parse_reftuple(
  85. lh_container: Union["Repo", "RefsContainer"],
  86. rh_container: Union["Repo", "RefsContainer"],
  87. refspec: Union[str, bytes],
  88. force: bool = False,
  89. ) -> tuple[Optional["Ref"], Optional["Ref"], bool]:
  90. """Parse a reftuple spec.
  91. Args:
  92. lh_container: A RefsContainer object
  93. rh_container: A RefsContainer object
  94. refspec: A string
  95. Returns: A tuple with left and right ref
  96. Raises:
  97. KeyError: If one of the refs can not be found
  98. """
  99. refspec = to_bytes(refspec)
  100. if refspec.startswith(b"+"):
  101. force = True
  102. refspec = refspec[1:]
  103. lh: Optional[bytes]
  104. rh: Optional[bytes]
  105. if b":" in refspec:
  106. (lh, rh) = refspec.split(b":")
  107. else:
  108. lh = rh = refspec
  109. if lh == b"":
  110. lh = None
  111. else:
  112. lh = parse_ref(lh_container, lh)
  113. if rh == b"":
  114. rh = None
  115. else:
  116. try:
  117. rh = parse_ref(rh_container, rh)
  118. except KeyError:
  119. # TODO: check force?
  120. if b"/" not in rh:
  121. rh = b"refs/heads/" + rh
  122. return (lh, rh, force)
  123. def parse_reftuples(
  124. lh_container: Union["Repo", "RefsContainer"],
  125. rh_container: Union["Repo", "RefsContainer"],
  126. refspecs: Union[bytes, list[bytes]],
  127. force: bool = False,
  128. ):
  129. """Parse a list of reftuple specs to a list of reftuples.
  130. Args:
  131. lh_container: A RefsContainer object
  132. rh_container: A RefsContainer object
  133. refspecs: A list of refspecs or a string
  134. force: Force overwriting for all reftuples
  135. Returns: A list of refs
  136. Raises:
  137. KeyError: If one of the refs can not be found
  138. """
  139. if not isinstance(refspecs, list):
  140. refspecs = [refspecs]
  141. ret = []
  142. # TODO: Support * in refspecs
  143. for refspec in refspecs:
  144. ret.append(parse_reftuple(lh_container, rh_container, refspec, force=force))
  145. return ret
  146. def parse_refs(container, refspecs):
  147. """Parse a list of refspecs to a list of refs.
  148. Args:
  149. container: A RefsContainer object
  150. refspecs: A list of refspecs or a string
  151. Returns: A list of refs
  152. Raises:
  153. KeyError: If one of the refs can not be found
  154. """
  155. # TODO: Support * in refspecs
  156. if not isinstance(refspecs, list):
  157. refspecs = [refspecs]
  158. ret = []
  159. for refspec in refspecs:
  160. ret.append(parse_ref(container, refspec))
  161. return ret
  162. def parse_commit_range(
  163. repo: "Repo", committishs: Union[str, bytes]
  164. ) -> Iterator["Commit"]:
  165. """Parse a string referring to a range of commits.
  166. Args:
  167. repo: A `Repo` object
  168. committishs: A string referring to a range of commits.
  169. Returns: An iterator over `Commit` objects
  170. Raises:
  171. KeyError: When the reference commits can not be found
  172. ValueError: If the range can not be parsed
  173. """
  174. committishs = to_bytes(committishs)
  175. # TODO(jelmer): Support more than a single commit..
  176. return iter([parse_commit(repo, committishs)])
  177. class AmbiguousShortId(Exception):
  178. """The short id is ambiguous."""
  179. def __init__(self, prefix, options) -> None:
  180. self.prefix = prefix
  181. self.options = options
  182. def scan_for_short_id(object_store, prefix, tp):
  183. """Scan an object store for a short id."""
  184. ret = []
  185. for object_id in object_store.iter_prefix(prefix):
  186. o = object_store[object_id]
  187. if isinstance(o, tp):
  188. ret.append(o)
  189. if not ret:
  190. raise KeyError(prefix)
  191. if len(ret) == 1:
  192. return ret[0]
  193. raise AmbiguousShortId(prefix, ret)
  194. def parse_commit(repo: "Repo", committish: Union[str, bytes]) -> "Commit":
  195. """Parse a string referring to a single commit.
  196. Args:
  197. repo: A` Repo` object
  198. committish: A string referring to a single commit.
  199. Returns: A Commit object
  200. Raises:
  201. KeyError: When the reference commits can not be found
  202. ValueError: If the range can not be parsed
  203. """
  204. committish = to_bytes(committish)
  205. try:
  206. return repo[committish]
  207. except KeyError:
  208. pass
  209. try:
  210. return repo[parse_ref(repo, committish)]
  211. except KeyError:
  212. pass
  213. if len(committish) >= 4 and len(committish) < 40:
  214. try:
  215. int(committish, 16)
  216. except ValueError:
  217. pass
  218. else:
  219. try:
  220. return scan_for_short_id(repo.object_store, committish, Commit)
  221. except KeyError:
  222. pass
  223. raise KeyError(committish)
  224. # TODO: parse_path_in_tree(), which handles e.g. v1.0:Documentation