You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.3KB

  1. from git.util import (
  2. LazyMixin,
  3. Iterable,
  4. )
  5. from .symbolic import SymbolicReference
  6. __all__ = ["Reference"]
  7. #{ Utilities
  8. def require_remote_ref_path(func):
  9. """A decorator raising a TypeError if we are not a valid remote, based on the path"""
  10. def wrapper(self, *args):
  11. if not self.is_remote():
  12. raise ValueError("ref path does not point to a remote reference: %s" % self.path)
  13. return func(self, *args)
  14. # END wrapper
  15. wrapper.__name__ = func.__name__
  16. return wrapper
  17. #}END utilities
  18. class Reference(SymbolicReference, LazyMixin, Iterable):
  19. """Represents a named reference to any object. Subclasses may apply restrictions though,
  20. i.e. Heads can only point to commits."""
  21. __slots__ = ()
  22. _points_to_commits_only = False
  23. _resolve_ref_on_create = True
  24. _common_path_default = "refs"
  25. def __init__(self, repo, path, check_path=True):
  26. """Initialize this instance
  27. :param repo: Our parent repository
  28. :param path:
  29. Path relative to the .git/ directory pointing to the ref in question, i.e.
  30. refs/heads/master
  31. :param check_path: if False, you can provide any path. Otherwise the path must start with the
  32. default path prefix of this type."""
  33. if check_path and not path.startswith(self._common_path_default + '/'):
  34. raise ValueError("Cannot instantiate %r from path %s" % (self.__class__.__name__, path))
  35. super(Reference, self).__init__(repo, path)
  36. def __str__(self):
  37. return self.name
  38. #{ Interface
  39. def set_object(self, object, logmsg=None): # @ReservedAssignment
  40. """Special version which checks if the head-log needs an update as well
  41. :return: self"""
  42. oldbinsha = None
  43. if logmsg is not None:
  44. head = self.repo.head
  45. if not head.is_detached and head.ref == self:
  46. oldbinsha = self.commit.binsha
  47. # END handle commit retrieval
  48. # END handle message is set
  49. super(Reference, self).set_object(object, logmsg)
  50. if oldbinsha is not None:
  51. # /* from refs.c in git-source
  52. # * Special hack: If a branch is updated directly and HEAD
  53. # * points to it (may happen on the remote side of a push
  54. # * for example) then logically the HEAD reflog should be
  55. # * updated too.
  56. # * A generic solution implies reverse symref information,
  57. # * but finding all symrefs pointing to the given branch
  58. # * would be rather costly for this rare event (the direct
  59. # * update of a branch) to be worth it. So let's cheat and
  60. # * check with HEAD only which should cover 99% of all usage
  61. # * scenarios (even 100% of the default ones).
  62. # */
  63. self.repo.head.log_append(oldbinsha, logmsg)
  64. # END check if the head
  65. return self
  66. # NOTE: Don't have to overwrite properties as the will only work without a the log
  67. @property
  68. def name(self):
  69. """:return: (shortest) Name of this reference - it may contain path components"""
  70. # first two path tokens are can be removed as they are
  71. # refs/heads or refs/tags or refs/remotes
  72. tokens = self.path.split('/')
  73. if len(tokens) < 3:
  74. return self.path # could be refs/HEAD
  75. return '/'.join(tokens[2:])
  76. @classmethod
  77. def iter_items(cls, repo, common_path=None):
  78. """Equivalent to SymbolicReference.iter_items, but will return non-detached
  79. references as well."""
  80. return cls._iter_items(repo, common_path)
  81. #}END interface
  82. #{ Remote Interface
  83. @property
  84. @require_remote_ref_path
  85. def remote_name(self):
  86. """
  87. :return:
  88. Name of the remote we are a reference of, such as 'origin' for a reference
  89. named 'origin/master'"""
  90. tokens = self.path.split('/')
  91. # /refs/remotes/<remote name>/<branch_name>
  92. return tokens[2]
  93. @property
  94. @require_remote_ref_path
  95. def remote_head(self):
  96. """:return: Name of the remote head itself, i.e. master.
  97. :note: The returned name is usually not qualified enough to uniquely identify
  98. a branch"""
  99. tokens = self.path.split('/')
  100. return '/'.join(tokens[3:])
  101. #} END remote interface