objectspec.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # objectspec.py -- Object specification
  2. # Copyright (C) 2014 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Object specification."""
  19. def to_bytes(text):
  20. if getattr(text, "encode", None) is not None:
  21. text = text.encode('ascii')
  22. return text
  23. def parse_object(repo, objectish):
  24. """Parse a string referring to an object.
  25. :param repo: A `Repo` object
  26. :param objectish: A string referring to an object
  27. :return: A git object
  28. :raise KeyError: If the object can not be found
  29. """
  30. objectish = to_bytes(objectish)
  31. return repo[objectish]
  32. def parse_ref(container, refspec):
  33. """Parse a string referring to a reference.
  34. :param container: A RefsContainer object
  35. :param refspec: A string referring to a ref
  36. :return: A ref
  37. :raise KeyError: If the ref can not be found
  38. """
  39. refspec = to_bytes(refspec)
  40. possible_refs = [
  41. refspec,
  42. b"refs/" + refspec,
  43. b"refs/tags/" + refspec,
  44. b"refs/heads/" + refspec,
  45. b"refs/remotes/" + refspec,
  46. b"refs/remotes/" + refspec + b"/HEAD"
  47. ]
  48. for ref in possible_refs:
  49. if ref in container:
  50. return ref
  51. else:
  52. raise KeyError(refspec)
  53. def parse_reftuple(lh_container, rh_container, refspec):
  54. """Parse a reftuple spec.
  55. :param lh_container: A RefsContainer object
  56. :param hh_container: A RefsContainer object
  57. :param refspec: A string
  58. :return: A tuple with left and right ref
  59. :raise KeyError: If one of the refs can not be found
  60. """
  61. if refspec.startswith(b"+"):
  62. force = True
  63. refspec = refspec[1:]
  64. else:
  65. force = False
  66. refspec = to_bytes(refspec)
  67. if b":" in refspec:
  68. (lh, rh) = refspec.split(b":")
  69. else:
  70. lh = rh = refspec
  71. if rh == b"":
  72. lh = None
  73. else:
  74. lh = parse_ref(lh_container, lh)
  75. if rh == b"":
  76. rh = None
  77. else:
  78. try:
  79. rh = parse_ref(rh_container, rh)
  80. except KeyError:
  81. # TODO: check force?
  82. if not b"/" in rh:
  83. rh = b"refs/heads/" + rh
  84. return (lh, rh, force)
  85. def parse_reftuples(lh_container, rh_container, refspecs):
  86. """Parse a list of reftuple specs to a list of reftuples.
  87. :param lh_container: A RefsContainer object
  88. :param hh_container: A RefsContainer object
  89. :param refspecs: A list of refspecs or a string
  90. :return: A list of refs
  91. :raise KeyError: If one of the refs can not be found
  92. """
  93. if not isinstance(refspecs, list):
  94. refspecs = [refspecs]
  95. ret = []
  96. # TODO: Support * in refspecs
  97. for refspec in refspecs:
  98. ret.append(parse_reftuple(lh_container, rh_container, refspec))
  99. return ret
  100. def parse_refs(container, refspecs):
  101. """Parse a list of refspecs to a list of refs.
  102. :param container: A RefsContainer object
  103. :param refspecs: A list of refspecs or a string
  104. :return: A list of refs
  105. :raise KeyError: If one of the refs can not be found
  106. """
  107. # TODO: Support * in refspecs
  108. if not isinstance(refspecs, list):
  109. refspecs = [refspecs]
  110. ret = []
  111. for refspec in refspecs:
  112. ret.append(parse_ref(container, refspec))
  113. return ret
  114. def parse_commit_range(repo, committishs):
  115. """Parse a string referring to a range of commits.
  116. :param repo: A `Repo` object
  117. :param committishs: A string referring to a range of commits.
  118. :return: An iterator over `Commit` objects
  119. :raise KeyError: When the reference commits can not be found
  120. :raise ValueError: If the range can not be parsed
  121. """
  122. committishs = to_bytes(committishs)
  123. # TODO(jelmer): Support more than a single commit..
  124. return iter([parse_commit(repo, committishs)])
  125. def parse_commit(repo, committish):
  126. """Parse a string referring to a single commit.
  127. :param repo: A` Repo` object
  128. :param commitish: A string referring to a single commit.
  129. :return: A Commit object
  130. :raise KeyError: When the reference commits can not be found
  131. :raise ValueError: If the range can not be parsed
  132. """
  133. committish = to_bytes(committish)
  134. return repo[committish] # For now..
  135. # TODO: parse_path_in_tree(), which handles e.g. v1.0:Documentation