typ.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # This module is part of GitPython and is released under the
  2. # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
  3. """Additional types used by the index."""
  4. __all__ = ["BlobFilter", "BaseIndexEntry", "IndexEntry", "StageType"]
  5. from binascii import b2a_hex
  6. from pathlib import Path
  7. from git.objects import Blob
  8. from .util import pack, unpack
  9. # typing ----------------------------------------------------------------------
  10. from typing import NamedTuple, Sequence, TYPE_CHECKING, Tuple, Union, cast
  11. from git.types import PathLike
  12. if TYPE_CHECKING:
  13. from git.repo import Repo
  14. StageType = int
  15. # ---------------------------------------------------------------------------------
  16. # { Invariants
  17. CE_NAMEMASK = 0x0FFF
  18. CE_STAGEMASK = 0x3000
  19. CE_EXTENDED = 0x4000
  20. CE_VALID = 0x8000
  21. CE_STAGESHIFT = 12
  22. CE_EXT_SKIP_WORKTREE = 0x4000
  23. CE_EXT_INTENT_TO_ADD = 0x2000
  24. # } END invariants
  25. class BlobFilter:
  26. """Predicate to be used by
  27. :meth:`IndexFile.iter_blobs <git.index.base.IndexFile.iter_blobs>` allowing to
  28. filter only return blobs which match the given list of directories or files.
  29. The given paths are given relative to the repository.
  30. """
  31. __slots__ = ("paths",)
  32. def __init__(self, paths: Sequence[PathLike]) -> None:
  33. """
  34. :param paths:
  35. Tuple or list of paths which are either pointing to directories or to files
  36. relative to the current repository.
  37. """
  38. self.paths = paths
  39. def __call__(self, stage_blob: Tuple[StageType, Blob]) -> bool:
  40. blob_pathlike: PathLike = stage_blob[1].path
  41. blob_path: Path = blob_pathlike if isinstance(blob_pathlike, Path) else Path(blob_pathlike)
  42. for pathlike in self.paths:
  43. path: Path = pathlike if isinstance(pathlike, Path) else Path(pathlike)
  44. # TODO: Change to use `PosixPath.is_relative_to` once Python 3.8 is no
  45. # longer supported.
  46. filter_parts = path.parts
  47. blob_parts = blob_path.parts
  48. if len(filter_parts) > len(blob_parts):
  49. continue
  50. if all(i == j for i, j in zip(filter_parts, blob_parts)):
  51. return True
  52. return False
  53. class BaseIndexEntryHelper(NamedTuple):
  54. """Typed named tuple to provide named attribute access for :class:`BaseIndexEntry`.
  55. This is needed to allow overriding ``__new__`` in child class to preserve backwards
  56. compatibility.
  57. """
  58. mode: int
  59. binsha: bytes
  60. flags: int
  61. path: PathLike
  62. ctime_bytes: bytes = pack(">LL", 0, 0)
  63. mtime_bytes: bytes = pack(">LL", 0, 0)
  64. dev: int = 0
  65. inode: int = 0
  66. uid: int = 0
  67. gid: int = 0
  68. size: int = 0
  69. # version 3 extended flags, only when (flags & CE_EXTENDED) is set
  70. extended_flags: int = 0
  71. class BaseIndexEntry(BaseIndexEntryHelper):
  72. R"""Small brother of an index entry which can be created to describe changes
  73. done to the index in which case plenty of additional information is not required.
  74. As the first 4 data members match exactly to the :class:`IndexEntry` type, methods
  75. expecting a :class:`BaseIndexEntry` can also handle full :class:`IndexEntry`\s even
  76. if they use numeric indices for performance reasons.
  77. """
  78. def __new__(
  79. cls,
  80. inp_tuple: Union[
  81. Tuple[int, bytes, int, PathLike],
  82. Tuple[int, bytes, int, PathLike, bytes, bytes, int, int, int, int, int, int],
  83. ],
  84. ) -> "BaseIndexEntry":
  85. """Override ``__new__`` to allow construction from a tuple for backwards
  86. compatibility."""
  87. return super().__new__(cls, *inp_tuple)
  88. def __str__(self) -> str:
  89. return "%o %s %i\t%s" % (self.mode, self.hexsha, self.stage, self.path)
  90. def __repr__(self) -> str:
  91. return "(%o, %s, %i, %s)" % (self.mode, self.hexsha, self.stage, self.path)
  92. @property
  93. def hexsha(self) -> str:
  94. """hex version of our sha"""
  95. return b2a_hex(self.binsha).decode("ascii")
  96. @property
  97. def stage(self) -> int:
  98. """Stage of the entry, either:
  99. * 0 = default stage
  100. * 1 = stage before a merge or common ancestor entry in case of a 3 way merge
  101. * 2 = stage of entries from the 'left' side of the merge
  102. * 3 = stage of entries from the 'right' side of the merge
  103. :note:
  104. For more information, see :manpage:`git-read-tree(1)`.
  105. """
  106. return (self.flags & CE_STAGEMASK) >> CE_STAGESHIFT
  107. @property
  108. def skip_worktree(self) -> bool:
  109. return (self.extended_flags & CE_EXT_SKIP_WORKTREE) > 0
  110. @property
  111. def intent_to_add(self) -> bool:
  112. return (self.extended_flags & CE_EXT_INTENT_TO_ADD) > 0
  113. @classmethod
  114. def from_blob(cls, blob: Blob, stage: int = 0) -> "BaseIndexEntry":
  115. """:return: Fully equipped BaseIndexEntry at the given stage"""
  116. return cls((blob.mode, blob.binsha, stage << CE_STAGESHIFT, blob.path))
  117. def to_blob(self, repo: "Repo") -> Blob:
  118. """:return: Blob using the information of this index entry"""
  119. return Blob(repo, self.binsha, self.mode, self.path)
  120. class IndexEntry(BaseIndexEntry):
  121. """Allows convenient access to index entry data as defined in
  122. :class:`BaseIndexEntry` without completely unpacking it.
  123. Attributes usually accessed often are cached in the tuple whereas others are
  124. unpacked on demand.
  125. See the properties for a mapping between names and tuple indices.
  126. """
  127. @property
  128. def ctime(self) -> Tuple[int, int]:
  129. """
  130. :return:
  131. Tuple(int_time_seconds_since_epoch, int_nano_seconds) of the
  132. file's creation time
  133. """
  134. return cast(Tuple[int, int], unpack(">LL", self.ctime_bytes))
  135. @property
  136. def mtime(self) -> Tuple[int, int]:
  137. """See :attr:`ctime` property, but returns modification time."""
  138. return cast(Tuple[int, int], unpack(">LL", self.mtime_bytes))
  139. @classmethod
  140. def from_base(cls, base: "BaseIndexEntry") -> "IndexEntry":
  141. """
  142. :return:
  143. Minimal entry as created from the given :class:`BaseIndexEntry` instance.
  144. Missing values will be set to null-like values.
  145. :param base:
  146. Instance of type :class:`BaseIndexEntry`.
  147. """
  148. time = pack(">LL", 0, 0)
  149. return IndexEntry((base.mode, base.binsha, base.flags, base.path, time, time, 0, 0, 0, 0, 0)) # type: ignore[arg-type]
  150. @classmethod
  151. def from_blob(cls, blob: Blob, stage: int = 0) -> "IndexEntry":
  152. """:return: Minimal entry resembling the given blob object"""
  153. time = pack(">LL", 0, 0)
  154. return IndexEntry(
  155. (
  156. blob.mode,
  157. blob.binsha,
  158. stage << CE_STAGESHIFT,
  159. blob.path,
  160. time,
  161. time,
  162. 0,
  163. 0,
  164. 0,
  165. 0,
  166. blob.size,
  167. ) # type: ignore[arg-type]
  168. )