log.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. __all__ = ["RefLog", "RefLogEntry"]
  4. from mmap import mmap
  5. import os.path as osp
  6. import re
  7. import time as _time
  8. from git.compat import defenc
  9. from git.objects.util import (
  10. Serializable,
  11. altz_to_utctz_str,
  12. parse_date,
  13. )
  14. from git.util import (
  15. Actor,
  16. LockedFD,
  17. LockFile,
  18. assure_directory_exists,
  19. bin_to_hex,
  20. file_contents_ro_filepath,
  21. to_native_path,
  22. )
  23. # typing ------------------------------------------------------------------
  24. from typing import Iterator, List, Tuple, TYPE_CHECKING, Union
  25. from git.types import PathLike
  26. if TYPE_CHECKING:
  27. from io import BytesIO
  28. from git.config import GitConfigParser, SectionConstraint
  29. from git.refs import SymbolicReference
  30. # ------------------------------------------------------------------------------
  31. class RefLogEntry(Tuple[str, str, Actor, Tuple[int, int], str]):
  32. """Named tuple allowing easy access to the revlog data fields."""
  33. _re_hexsha_only = re.compile(r"^[0-9A-Fa-f]{40}$")
  34. __slots__ = ()
  35. def __repr__(self) -> str:
  36. """Representation of ourselves in git reflog format."""
  37. return self.format()
  38. def format(self) -> str:
  39. """:return: A string suitable to be placed in a reflog file."""
  40. act = self.actor
  41. time = self.time
  42. return "{} {} {} <{}> {!s} {}\t{}\n".format(
  43. self.oldhexsha,
  44. self.newhexsha,
  45. act.name,
  46. act.email,
  47. time[0],
  48. altz_to_utctz_str(time[1]),
  49. self.message,
  50. )
  51. @property
  52. def oldhexsha(self) -> str:
  53. """The hexsha to the commit the ref pointed to before the change."""
  54. return self[0]
  55. @property
  56. def newhexsha(self) -> str:
  57. """The hexsha to the commit the ref now points to, after the change."""
  58. return self[1]
  59. @property
  60. def actor(self) -> Actor:
  61. """Actor instance, providing access."""
  62. return self[2]
  63. @property
  64. def time(self) -> Tuple[int, int]:
  65. """Time as tuple:
  66. * [0] = ``int(time)``
  67. * [1] = ``int(timezone_offset)`` in :attr:`time.altzone` format
  68. """
  69. return self[3]
  70. @property
  71. def message(self) -> str:
  72. """Message describing the operation that acted on the reference."""
  73. return self[4]
  74. @classmethod
  75. def new(
  76. cls,
  77. oldhexsha: str,
  78. newhexsha: str,
  79. actor: Actor,
  80. time: int,
  81. tz_offset: int,
  82. message: str,
  83. ) -> "RefLogEntry": # skipcq: PYL-W0621
  84. """:return: New instance of a :class:`RefLogEntry`"""
  85. if not isinstance(actor, Actor):
  86. raise ValueError("Need actor instance, got %s" % actor)
  87. # END check types
  88. return RefLogEntry((oldhexsha, newhexsha, actor, (time, tz_offset), message))
  89. @classmethod
  90. def from_line(cls, line: bytes) -> "RefLogEntry":
  91. """:return: New :class:`RefLogEntry` instance from the given revlog line.
  92. :param line:
  93. Line bytes without trailing newline
  94. :raise ValueError:
  95. If `line` could not be parsed.
  96. """
  97. line_str = line.decode(defenc)
  98. fields = line_str.split("\t", 1)
  99. if len(fields) == 1:
  100. info, msg = fields[0], None
  101. elif len(fields) == 2:
  102. info, msg = fields
  103. else:
  104. raise ValueError("Line must have up to two TAB-separated fields. Got %s" % repr(line_str))
  105. # END handle first split
  106. oldhexsha = info[:40]
  107. newhexsha = info[41:81]
  108. for hexsha in (oldhexsha, newhexsha):
  109. if not cls._re_hexsha_only.match(hexsha):
  110. raise ValueError("Invalid hexsha: %r" % (hexsha,))
  111. # END if hexsha re doesn't match
  112. # END for each hexsha
  113. email_end = info.find(">", 82)
  114. if email_end == -1:
  115. raise ValueError("Missing token: >")
  116. # END handle missing end brace
  117. actor = Actor._from_string(info[82 : email_end + 1])
  118. time, tz_offset = parse_date(info[email_end + 2 :]) # skipcq: PYL-W0621
  119. return RefLogEntry((oldhexsha, newhexsha, actor, (time, tz_offset), msg)) # type: ignore [arg-type]
  120. class RefLog(List[RefLogEntry], Serializable):
  121. R"""A reflog contains :class:`RefLogEntry`\s, each of which defines a certain state
  122. of the head in question. Custom query methods allow to retrieve log entries by date
  123. or by other criteria.
  124. Reflog entries are ordered. The first added entry is first in the list. The last
  125. entry, i.e. the last change of the head or reference, is last in the list.
  126. """
  127. __slots__ = ("_path",)
  128. def __new__(cls, filepath: Union[PathLike, None] = None) -> "RefLog":
  129. inst = super().__new__(cls)
  130. return inst
  131. def __init__(self, filepath: Union[PathLike, None] = None) -> None:
  132. """Initialize this instance with an optional filepath, from which we will
  133. initialize our data. The path is also used to write changes back using the
  134. :meth:`write` method."""
  135. self._path = filepath
  136. if filepath is not None:
  137. self._read_from_file()
  138. # END handle filepath
  139. def _read_from_file(self) -> None:
  140. try:
  141. fmap = file_contents_ro_filepath(self._path, stream=True, allow_mmap=True)
  142. except OSError:
  143. # It is possible and allowed that the file doesn't exist!
  144. return
  145. # END handle invalid log
  146. try:
  147. self._deserialize(fmap)
  148. finally:
  149. fmap.close()
  150. # END handle closing of handle
  151. # { Interface
  152. @classmethod
  153. def from_file(cls, filepath: PathLike) -> "RefLog":
  154. """
  155. :return:
  156. A new :class:`RefLog` instance containing all entries from the reflog at the
  157. given `filepath`.
  158. :param filepath:
  159. Path to reflog.
  160. :raise ValueError:
  161. If the file could not be read or was corrupted in some way.
  162. """
  163. return cls(filepath)
  164. @classmethod
  165. def path(cls, ref: "SymbolicReference") -> str:
  166. """
  167. :return:
  168. String to absolute path at which the reflog of the given ref instance would
  169. be found. The path is not guaranteed to point to a valid file though.
  170. :param ref:
  171. :class:`~git.refs.symbolic.SymbolicReference` instance
  172. """
  173. return osp.join(ref.repo.git_dir, "logs", to_native_path(ref.path))
  174. @classmethod
  175. def iter_entries(cls, stream: Union[str, "BytesIO", mmap]) -> Iterator[RefLogEntry]:
  176. """
  177. :return:
  178. Iterator yielding :class:`RefLogEntry` instances, one for each line read
  179. from the given stream.
  180. :param stream:
  181. File-like object containing the revlog in its native format or string
  182. instance pointing to a file to read.
  183. """
  184. new_entry = RefLogEntry.from_line
  185. if isinstance(stream, str):
  186. # Default args return mmap since Python 3.
  187. _stream = file_contents_ro_filepath(stream)
  188. assert isinstance(_stream, mmap)
  189. else:
  190. _stream = stream
  191. # END handle stream type
  192. while True:
  193. line = _stream.readline()
  194. if not line:
  195. return
  196. yield new_entry(line.strip())
  197. # END endless loop
  198. @classmethod
  199. def entry_at(cls, filepath: PathLike, index: int) -> "RefLogEntry":
  200. """
  201. :return:
  202. :class:`RefLogEntry` at the given index.
  203. :param filepath:
  204. Full path to the index file from which to read the entry.
  205. :param index:
  206. Python list compatible index, i.e. it may be negative to specify an entry
  207. counted from the end of the list.
  208. :raise IndexError:
  209. If the entry didn't exist.
  210. :note:
  211. This method is faster as it only parses the entry at index, skipping all
  212. other lines. Nonetheless, the whole file has to be read if the index is
  213. negative.
  214. """
  215. with open(filepath, "rb") as fp:
  216. if index < 0:
  217. return RefLogEntry.from_line(fp.readlines()[index].strip())
  218. # Read until index is reached.
  219. for i in range(index + 1):
  220. line = fp.readline()
  221. if not line:
  222. raise IndexError(f"Index file ended at line {i + 1}, before given index was reached")
  223. # END abort on eof
  224. # END handle runup
  225. return RefLogEntry.from_line(line.strip())
  226. # END handle index
  227. def to_file(self, filepath: PathLike) -> None:
  228. """Write the contents of the reflog instance to a file at the given filepath.
  229. :param filepath:
  230. Path to file. Parent directories are assumed to exist.
  231. """
  232. lfd = LockedFD(filepath)
  233. assure_directory_exists(filepath, is_file=True)
  234. fp = lfd.open(write=True, stream=True)
  235. try:
  236. self._serialize(fp)
  237. lfd.commit()
  238. except BaseException:
  239. lfd.rollback()
  240. raise
  241. # END handle change
  242. @classmethod
  243. def append_entry(
  244. cls,
  245. config_reader: Union[Actor, "GitConfigParser", "SectionConstraint", None],
  246. filepath: PathLike,
  247. oldbinsha: bytes,
  248. newbinsha: bytes,
  249. message: str,
  250. write: bool = True,
  251. ) -> "RefLogEntry":
  252. """Append a new log entry to the revlog at filepath.
  253. :param config_reader:
  254. Configuration reader of the repository - used to obtain user information.
  255. May also be an :class:`~git.util.Actor` instance identifying the committer
  256. directly or ``None``.
  257. :param filepath:
  258. Full path to the log file.
  259. :param oldbinsha:
  260. Binary sha of the previous commit.
  261. :param newbinsha:
  262. Binary sha of the current commit.
  263. :param message:
  264. Message describing the change to the reference.
  265. :param write:
  266. If ``True``, the changes will be written right away.
  267. Otherwise the change will not be written.
  268. :return:
  269. :class:`RefLogEntry` objects which was appended to the log.
  270. :note:
  271. As we are append-only, concurrent access is not a problem as we do not
  272. interfere with readers.
  273. """
  274. if len(oldbinsha) != 20 or len(newbinsha) != 20:
  275. raise ValueError("Shas need to be given in binary format")
  276. # END handle sha type
  277. assure_directory_exists(filepath, is_file=True)
  278. first_line = message.split("\n")[0]
  279. if isinstance(config_reader, Actor):
  280. committer = config_reader # mypy thinks this is Actor | Gitconfigparser, but why?
  281. else:
  282. committer = Actor.committer(config_reader)
  283. entry = RefLogEntry(
  284. (
  285. bin_to_hex(oldbinsha).decode("ascii"),
  286. bin_to_hex(newbinsha).decode("ascii"),
  287. committer,
  288. (int(_time.time()), _time.altzone),
  289. first_line,
  290. )
  291. )
  292. if write:
  293. lf = LockFile(filepath)
  294. lf._obtain_lock_or_raise()
  295. fd = open(filepath, "ab")
  296. try:
  297. fd.write(entry.format().encode(defenc))
  298. finally:
  299. fd.close()
  300. lf._release_lock()
  301. # END handle write operation
  302. return entry
  303. def write(self) -> "RefLog":
  304. """Write this instance's data to the file we are originating from.
  305. :return:
  306. self
  307. """
  308. if self._path is None:
  309. raise ValueError("Instance was not initialized with a path, use to_file(...) instead")
  310. # END assert path
  311. self.to_file(self._path)
  312. return self
  313. # } END interface
  314. # { Serializable Interface
  315. def _serialize(self, stream: "BytesIO") -> "RefLog":
  316. write = stream.write
  317. # Write all entries.
  318. for e in self:
  319. write(e.format().encode(defenc))
  320. # END for each entry
  321. return self
  322. def _deserialize(self, stream: "BytesIO") -> "RefLog":
  323. self.extend(self.iter_entries(stream))
  324. return self
  325. # } END serializable interface