objectspec.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. for ref in [refspec, b"refs/heads/" + refspec]:
  41. if ref in container:
  42. return ref
  43. else:
  44. raise KeyError(refspec)
  45. def parse_reftuple(lh_container, rh_container, refspec):
  46. """Parse a reftuple spec.
  47. :param lh_container: A RefsContainer object
  48. :param hh_container: A RefsContainer object
  49. :param refspec: A string
  50. :return: A tuple with left and right ref
  51. :raise KeyError: If one of the refs can not be found
  52. """
  53. if refspec.startswith(b"+"):
  54. force = True
  55. refspec = refspec[1:]
  56. else:
  57. force = False
  58. refspec = to_bytes(refspec)
  59. if b":" in refspec:
  60. (lh, rh) = refspec.split(b":")
  61. else:
  62. lh = rh = refspec
  63. if rh == b"":
  64. lh = None
  65. else:
  66. lh = parse_ref(lh_container, lh)
  67. if rh == b"":
  68. rh = None
  69. else:
  70. try:
  71. rh = parse_ref(rh_container, rh)
  72. except KeyError:
  73. # TODO: check force?
  74. if not b"/" in rh:
  75. rh = b"refs/heads/" + rh
  76. return (lh, rh, force)
  77. def parse_reftuples(lh_container, rh_container, refspecs):
  78. """Parse a list of reftuple specs to a list of reftuples.
  79. :param lh_container: A RefsContainer object
  80. :param hh_container: A RefsContainer object
  81. :param refspecs: A list of refspecs or a string
  82. :return: A list of refs
  83. :raise KeyError: If one of the refs can not be found
  84. """
  85. if not isinstance(refspecs, list):
  86. refspecs = [refspecs]
  87. ret = []
  88. # TODO: Support * in refspecs
  89. for refspec in refspecs:
  90. ret.append(parse_reftuple(lh_container, rh_container, refspec))
  91. return ret
  92. def parse_refs(container, refspecs):
  93. """Parse a list of refspecs to a list of refs.
  94. :param container: A RefsContainer object
  95. :param refspecs: A list of refspecs or a string
  96. :return: A list of refs
  97. :raise KeyError: If one of the refs can not be found
  98. """
  99. # TODO: Support * in refspecs
  100. if not isinstance(refspecs, list):
  101. refspecs = [refspecs]
  102. ret = []
  103. for refspec in refspecs:
  104. ret.append(parse_ref(container, refspec))
  105. return ret
  106. def parse_commit_range(repo, committishs):
  107. """Parse a string referring to a range of commits.
  108. :param repo: A `Repo` object
  109. :param committishs: A string referring to a range of commits.
  110. :return: An iterator over `Commit` objects
  111. :raise KeyError: When the reference commits can not be found
  112. :raise ValueError: If the range can not be parsed
  113. """
  114. committishs = to_bytes(committishs)
  115. return iter([repo[committishs]])