typ.py 6.1 KB

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