浏览代码

Add read_reflog.

Jelmer Vernooij 9 年之前
父节点
当前提交
9713fdb08d
共有 2 个文件被更改,包括 20 次插入2 次删除
  1. 3 0
      NEWS
  2. 17 2
      dulwich/reflog.py

+ 3 - 0
NEWS

@@ -5,6 +5,9 @@
   * Add a `dulwich.archive` module that can create tarballs.
     Based on code from Jonas Haag in klaus.
 
+  * Add a `dulwich.reflog` module for reading and writing reflogs.
+    (Jelmer Vernooij)
+
   * Fix handling of ambiguous refs in `parse_ref` to make
     it match the behaviour described in https://git-scm.com/docs/gitrevisions.
     (Chris Bunney)

+ 17 - 2
dulwich/reflog.py

@@ -19,12 +19,17 @@
 """Utilities for reading and generating reflogs.
 """
 
+import collections
+
 from dulwich.objects import (
     format_timezone,
     parse_timezone,
     ZERO_SHA,
     )
 
+Entry = collections.namedtuple('Entry', ['old_sha', 'new_sha', 'committer',
+    'timestamp', 'timezone', 'message'])
+
 
 def format_reflog_line(old_sha, new_sha, committer, timestamp, timezone, message):
     """Generate a single reflog line.
@@ -53,5 +58,15 @@ def parse_reflog_line(line):
     (begin, message) = line.split('\t', 1)
     (old_sha, new_sha, rest) = begin.split(' ', 2)
     (committer, timestamp_str, timezone_str) = rest.rsplit(' ', 2)
-    return (old_sha, new_sha, committer, int(timestamp_str),
-            parse_timezone(timezone_str)[0], message)
+    return Entry(old_sha, new_sha, committer, int(timestamp_str),
+                 parse_timezone(timezone_str)[0], message)
+
+
+def read_reflog(f):
+    """Read reflog.
+
+    :param f: File-like object
+    :returns: Iterator over Entry objects
+    """
+    for l in f:
+        yield parse_reflog_line(l)