reflog.py 4.0 KB

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