reflog.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # reflog.py -- Parsing and writing reflog files
  2. # Copyright (C) 2015 Jelmer Vernooij and others.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Utilities for reading and generating reflogs.
  19. """
  20. import collections
  21. from dulwich.objects import (
  22. format_timezone,
  23. parse_timezone,
  24. ZERO_SHA,
  25. )
  26. Entry = collections.namedtuple('Entry', ['old_sha', 'new_sha', 'committer',
  27. 'timestamp', 'timezone', 'message'])
  28. def format_reflog_line(old_sha, new_sha, committer, timestamp, timezone, message):
  29. """Generate a single reflog line.
  30. :param old_sha: Old Commit SHA
  31. :param new_sha: New Commit SHA
  32. :param committer: Committer name and e-mail
  33. :param timestamp: Timestamp
  34. :param timezone: Timezone
  35. :param message: Message
  36. """
  37. if old_sha is None:
  38. old_sha = ZERO_SHA
  39. return (old_sha + b' ' + new_sha + b' ' + committer + b' ' +
  40. str(timestamp).encode('ascii') + b' ' +
  41. format_timezone(timezone) + b'\t' + message)
  42. def parse_reflog_line(line):
  43. """Parse a reflog line.
  44. :param line: Line to parse
  45. :return: Tuple of (old_sha, new_sha, committer, timestamp, timezone,
  46. message)
  47. """
  48. (begin, message) = line.split(b'\t', 1)
  49. (old_sha, new_sha, rest) = begin.split(b' ', 2)
  50. (committer, timestamp_str, timezone_str) = rest.rsplit(b' ', 2)
  51. return Entry(old_sha, new_sha, committer, int(timestamp_str),
  52. parse_timezone(timezone_str)[0], message)
  53. def read_reflog(f):
  54. """Read reflog.
  55. :param f: File-like object
  56. :returns: Iterator over Entry objects
  57. """
  58. for l in f:
  59. yield parse_reflog_line(l)