base.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # base.py
  2. # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
  3. #
  4. # This module is part of GitPython and is released under
  5. # the BSD License: http://www.opensource.org/licenses/bsd-license.php
  6. from git.exc import WorkTreeRepositoryUnsupported
  7. from git.util import LazyMixin, join_path_native, stream_copy, bin_to_hex
  8. import gitdb.typ as dbtyp
  9. import os.path as osp
  10. from .util import get_object_type_by_name
  11. # typing ------------------------------------------------------------------
  12. from typing import Any, TYPE_CHECKING, Union
  13. from git.types import PathLike, Commit_ish, Lit_commit_ish
  14. if TYPE_CHECKING:
  15. from git.repo import Repo
  16. from gitdb.base import OStream
  17. from .tree import Tree
  18. from .blob import Blob
  19. from .submodule.base import Submodule
  20. from git.refs.reference import Reference
  21. IndexObjUnion = Union["Tree", "Blob", "Submodule"]
  22. # --------------------------------------------------------------------------
  23. _assertion_msg_format = "Created object %r whose python type %r disagrees with the actual git object type %r"
  24. __all__ = ("Object", "IndexObject")
  25. class Object(LazyMixin):
  26. """Implements an Object which may be Blobs, Trees, Commits and Tags"""
  27. NULL_HEX_SHA = "0" * 40
  28. NULL_BIN_SHA = b"\0" * 20
  29. TYPES = (
  30. dbtyp.str_blob_type,
  31. dbtyp.str_tree_type,
  32. dbtyp.str_commit_type,
  33. dbtyp.str_tag_type,
  34. )
  35. __slots__ = ("repo", "binsha", "size")
  36. type: Union[Lit_commit_ish, None] = None
  37. def __init__(self, repo: "Repo", binsha: bytes):
  38. """Initialize an object by identifying it by its binary sha.
  39. All keyword arguments will be set on demand if None.
  40. :param repo: repository this object is located in
  41. :param binsha: 20 byte SHA1"""
  42. super(Object, self).__init__()
  43. self.repo = repo
  44. self.binsha = binsha
  45. assert len(binsha) == 20, "Require 20 byte binary sha, got %r, len = %i" % (
  46. binsha,
  47. len(binsha),
  48. )
  49. @classmethod
  50. def new(cls, repo: "Repo", id: Union[str, "Reference"]) -> Commit_ish:
  51. """
  52. :return: New Object instance of a type appropriate to the object type behind
  53. id. The id of the newly created object will be a binsha even though
  54. the input id may have been a Reference or Rev-Spec
  55. :param id: reference, rev-spec, or hexsha
  56. :note: This cannot be a __new__ method as it would always call __init__
  57. with the input id which is not necessarily a binsha."""
  58. return repo.rev_parse(str(id))
  59. @classmethod
  60. def new_from_sha(cls, repo: "Repo", sha1: bytes) -> Commit_ish:
  61. """
  62. :return: new object instance of a type appropriate to represent the given
  63. binary sha1
  64. :param sha1: 20 byte binary sha1"""
  65. if sha1 == cls.NULL_BIN_SHA:
  66. # the NULL binsha is always the root commit
  67. return get_object_type_by_name(b"commit")(repo, sha1)
  68. # END handle special case
  69. oinfo = repo.odb.info(sha1)
  70. inst = get_object_type_by_name(oinfo.type)(repo, oinfo.binsha)
  71. inst.size = oinfo.size
  72. return inst
  73. def _set_cache_(self, attr: str) -> None:
  74. """Retrieve object information"""
  75. if attr == "size":
  76. oinfo = self.repo.odb.info(self.binsha)
  77. self.size = oinfo.size # type: int
  78. # assert oinfo.type == self.type, _assertion_msg_format % (self.binsha, oinfo.type, self.type)
  79. else:
  80. super(Object, self)._set_cache_(attr)
  81. def __eq__(self, other: Any) -> bool:
  82. """:return: True if the objects have the same SHA1"""
  83. if not hasattr(other, "binsha"):
  84. return False
  85. return self.binsha == other.binsha
  86. def __ne__(self, other: Any) -> bool:
  87. """:return: True if the objects do not have the same SHA1"""
  88. if not hasattr(other, "binsha"):
  89. return True
  90. return self.binsha != other.binsha
  91. def __hash__(self) -> int:
  92. """:return: Hash of our id allowing objects to be used in dicts and sets"""
  93. return hash(self.binsha)
  94. def __str__(self) -> str:
  95. """:return: string of our SHA1 as understood by all git commands"""
  96. return self.hexsha
  97. def __repr__(self) -> str:
  98. """:return: string with pythonic representation of our object"""
  99. return '<git.%s "%s">' % (self.__class__.__name__, self.hexsha)
  100. @property
  101. def hexsha(self) -> str:
  102. """:return: 40 byte hex version of our 20 byte binary sha"""
  103. # b2a_hex produces bytes
  104. return bin_to_hex(self.binsha).decode("ascii")
  105. @property
  106. def data_stream(self) -> "OStream":
  107. """:return: File Object compatible stream to the uncompressed raw data of the object
  108. :note: returned streams must be read in order"""
  109. return self.repo.odb.stream(self.binsha)
  110. def stream_data(self, ostream: "OStream") -> "Object":
  111. """Writes our data directly to the given output stream
  112. :param ostream: File object compatible stream object.
  113. :return: self"""
  114. istream = self.repo.odb.stream(self.binsha)
  115. stream_copy(istream, ostream)
  116. return self
  117. class IndexObject(Object):
  118. """Base for all objects that can be part of the index file , namely Tree, Blob and
  119. SubModule objects"""
  120. __slots__ = ("path", "mode")
  121. # for compatibility with iterable lists
  122. _id_attribute_ = "path"
  123. def __init__(
  124. self,
  125. repo: "Repo",
  126. binsha: bytes,
  127. mode: Union[None, int] = None,
  128. path: Union[None, PathLike] = None,
  129. ) -> None:
  130. """Initialize a newly instanced IndexObject
  131. :param repo: is the Repo we are located in
  132. :param binsha: 20 byte sha1
  133. :param mode:
  134. is the stat compatible file mode as int, use the stat module
  135. to evaluate the information
  136. :param path:
  137. is the path to the file in the file system, relative to the git repository root, i.e.
  138. file.ext or folder/other.ext
  139. :note:
  140. Path may not be set of the index object has been created directly as it cannot
  141. be retrieved without knowing the parent tree."""
  142. super(IndexObject, self).__init__(repo, binsha)
  143. if mode is not None:
  144. self.mode = mode
  145. if path is not None:
  146. self.path = path
  147. def __hash__(self) -> int:
  148. """
  149. :return:
  150. Hash of our path as index items are uniquely identifiable by path, not
  151. by their data !"""
  152. return hash(self.path)
  153. def _set_cache_(self, attr: str) -> None:
  154. if attr in IndexObject.__slots__:
  155. # they cannot be retrieved lateron ( not without searching for them )
  156. raise AttributeError(
  157. "Attribute '%s' unset: path and mode attributes must have been set during %s object creation"
  158. % (attr, type(self).__name__)
  159. )
  160. else:
  161. super(IndexObject, self)._set_cache_(attr)
  162. # END handle slot attribute
  163. @property
  164. def name(self) -> str:
  165. """:return: Name portion of the path, effectively being the basename"""
  166. return osp.basename(self.path)
  167. @property
  168. def abspath(self) -> PathLike:
  169. """
  170. :return:
  171. Absolute path to this index object in the file system ( as opposed to the
  172. .path field which is a path relative to the git repository ).
  173. The returned path will be native to the system and contains '\' on windows."""
  174. if self.repo.working_tree_dir is not None:
  175. return join_path_native(self.repo.working_tree_dir, self.path)
  176. else:
  177. raise WorkTreeRepositoryUnsupported("Working_tree_dir was None or empty")