reflog.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # reflog.py -- Parsing and writing reflog files
  2. # Copyright (C) 2015 Jelmer Vernooij and others.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Utilities for reading and generating reflogs."""
  22. import collections
  23. from .objects import ZERO_SHA, format_timezone, parse_timezone
  24. Entry = collections.namedtuple(
  25. "Entry",
  26. ["old_sha", "new_sha", "committer", "timestamp", "timezone", "message"],
  27. )
  28. def format_reflog_line(old_sha, new_sha, committer, timestamp, timezone, message):
  29. """Generate a single reflog line.
  30. Args:
  31. old_sha: Old Commit SHA
  32. new_sha: New Commit SHA
  33. committer: Committer name and e-mail
  34. timestamp: Timestamp
  35. timezone: Timezone
  36. message: Message
  37. """
  38. if old_sha is None:
  39. old_sha = ZERO_SHA
  40. return (
  41. old_sha
  42. + b" "
  43. + new_sha
  44. + b" "
  45. + committer
  46. + b" "
  47. + str(int(timestamp)).encode("ascii")
  48. + b" "
  49. + format_timezone(timezone)
  50. + b"\t"
  51. + message
  52. )
  53. def parse_reflog_line(line):
  54. """Parse a reflog line.
  55. Args:
  56. line: Line to parse
  57. Returns: Tuple of (old_sha, new_sha, committer, timestamp, timezone,
  58. message)
  59. """
  60. (begin, message) = line.split(b"\t", 1)
  61. (old_sha, new_sha, rest) = begin.split(b" ", 2)
  62. (committer, timestamp_str, timezone_str) = rest.rsplit(b" ", 2)
  63. return Entry(
  64. old_sha,
  65. new_sha,
  66. committer,
  67. int(timestamp_str),
  68. parse_timezone(timezone_str)[0],
  69. message,
  70. )
  71. def read_reflog(f):
  72. """Read reflog.
  73. Args:
  74. f: File-like object
  75. Returns: Iterator over Entry objects
  76. """
  77. for line in f:
  78. yield parse_reflog_line(line)
  79. def drop_reflog_entry(f, index, rewrite=False) -> None:
  80. """Drop the specified reflog entry.
  81. Args:
  82. f: File-like object
  83. index: Reflog entry index (in Git reflog reverse 0-indexed order)
  84. rewrite: If a reflog entry's predecessor is removed, set its
  85. old SHA to the new SHA of the entry that now precedes it
  86. """
  87. if index < 0:
  88. raise ValueError(f"Invalid reflog index {index}")
  89. log = []
  90. offset = f.tell()
  91. for line in f:
  92. log.append((offset, parse_reflog_line(line)))
  93. offset = f.tell()
  94. inverse_index = len(log) - index - 1
  95. write_offset = log[inverse_index][0]
  96. f.seek(write_offset)
  97. if index == 0:
  98. f.truncate()
  99. return
  100. del log[inverse_index]
  101. if rewrite and index > 0 and log:
  102. if inverse_index == 0:
  103. previous_new = ZERO_SHA
  104. else:
  105. previous_new = log[inverse_index - 1][1].new_sha
  106. offset, entry = log[inverse_index]
  107. log[inverse_index] = (
  108. offset,
  109. Entry(
  110. previous_new,
  111. entry.new_sha,
  112. entry.committer,
  113. entry.timestamp,
  114. entry.timezone,
  115. entry.message,
  116. ),
  117. )
  118. for _, entry in log[inverse_index:]:
  119. f.write(
  120. format_reflog_line(
  121. entry.old_sha,
  122. entry.new_sha,
  123. entry.committer,
  124. entry.timestamp,
  125. entry.timezone,
  126. entry.message,
  127. )
  128. )
  129. f.truncate()