objectspec.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # objectspec.py -- Object specification
  2. # Copyright (C) 2014 Jelmer Vernooij <jelmer@samba.org>
  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. def to_bytes(text):
  22. if getattr(text, "encode", None) is not None:
  23. text = text.encode('ascii')
  24. return text
  25. def parse_object(repo, objectish):
  26. """Parse a string referring to an object.
  27. :param repo: A `Repo` object
  28. :param objectish: A string referring to an object
  29. :return: A git object
  30. :raise KeyError: If the object can not be found
  31. """
  32. objectish = to_bytes(objectish)
  33. return repo[objectish]
  34. def parse_ref(container, refspec):
  35. """Parse a string referring to a reference.
  36. :param container: A RefsContainer object
  37. :param refspec: A string referring to a ref
  38. :return: A ref
  39. :raise KeyError: If the ref can not be found
  40. """
  41. refspec = to_bytes(refspec)
  42. possible_refs = [
  43. refspec,
  44. b"refs/" + refspec,
  45. b"refs/tags/" + refspec,
  46. b"refs/heads/" + refspec,
  47. b"refs/remotes/" + refspec,
  48. b"refs/remotes/" + refspec + b"/HEAD"
  49. ]
  50. for ref in possible_refs:
  51. if ref in container:
  52. return ref
  53. else:
  54. raise KeyError(refspec)
  55. def parse_reftuple(lh_container, rh_container, refspec):
  56. """Parse a reftuple spec.
  57. :param lh_container: A RefsContainer object
  58. :param hh_container: A RefsContainer object
  59. :param refspec: A string
  60. :return: A tuple with left and right ref
  61. :raise KeyError: If one of the refs can not be found
  62. """
  63. if refspec.startswith(b"+"):
  64. force = True
  65. refspec = refspec[1:]
  66. else:
  67. force = False
  68. refspec = to_bytes(refspec)
  69. if b":" in refspec:
  70. (lh, rh) = refspec.split(b":")
  71. else:
  72. lh = rh = refspec
  73. if lh == b"":
  74. lh = None
  75. else:
  76. lh = parse_ref(lh_container, lh)
  77. if rh == b"":
  78. rh = None
  79. else:
  80. try:
  81. rh = parse_ref(rh_container, rh)
  82. except KeyError:
  83. # TODO: check force?
  84. if not b"/" in rh:
  85. rh = b"refs/heads/" + rh
  86. return (lh, rh, force)
  87. def parse_reftuples(lh_container, rh_container, refspecs):
  88. """Parse a list of reftuple specs to a list of reftuples.
  89. :param lh_container: A RefsContainer object
  90. :param hh_container: A RefsContainer object
  91. :param refspecs: A list of refspecs or a string
  92. :return: A list of refs
  93. :raise KeyError: If one of the refs can not be found
  94. """
  95. if not isinstance(refspecs, list):
  96. refspecs = [refspecs]
  97. ret = []
  98. # TODO: Support * in refspecs
  99. for refspec in refspecs:
  100. ret.append(parse_reftuple(lh_container, rh_container, refspec))
  101. return ret
  102. def parse_refs(container, refspecs):
  103. """Parse a list of refspecs to a list of refs.
  104. :param container: A RefsContainer object
  105. :param refspecs: A list of refspecs or a string
  106. :return: A list of refs
  107. :raise KeyError: If one of the refs can not be found
  108. """
  109. # TODO: Support * in refspecs
  110. if not isinstance(refspecs, list):
  111. refspecs = [refspecs]
  112. ret = []
  113. for refspec in refspecs:
  114. ret.append(parse_ref(container, refspec))
  115. return ret
  116. def parse_commit_range(repo, committishs):
  117. """Parse a string referring to a range of commits.
  118. :param repo: A `Repo` object
  119. :param committishs: A string referring to a range of commits.
  120. :return: An iterator over `Commit` objects
  121. :raise KeyError: When the reference commits can not be found
  122. :raise ValueError: If the range can not be parsed
  123. """
  124. committishs = to_bytes(committishs)
  125. # TODO(jelmer): Support more than a single commit..
  126. return iter([parse_commit(repo, committishs)])
  127. def parse_commit(repo, committish):
  128. """Parse a string referring to a single commit.
  129. :param repo: A` Repo` object
  130. :param commitish: A string referring to a single commit.
  131. :return: A Commit object
  132. :raise KeyError: When the reference commits can not be found
  133. :raise ValueError: If the range can not be parsed
  134. """
  135. committish = to_bytes(committish)
  136. return repo[committish] # For now..
  137. # TODO: parse_path_in_tree(), which handles e.g. v1.0:Documentation