Browse Source

Add log command to porcelain.

Jelmer Vernooij 11 years ago
parent
commit
633270989b
3 changed files with 41 additions and 20 deletions
  1. 1 20
      bin/dulwich
  2. 29 0
      dulwich/porcelain.py
  3. 11 0
      dulwich/tests/test_porcelain.py

+ 1 - 20
bin/dulwich

@@ -91,26 +91,7 @@ def cmd_log(args):
         path = args.pop(0)
     else:
         path = "."
-    r = Repo(path)
-    todo = [r.head()]
-    done = set()
-    while todo:
-        sha = todo.pop()
-        assert isinstance(sha, str)
-        if sha in done:
-            continue
-        done.add(sha)
-        commit = r[sha]
-        print "-" * 50
-        print "commit: %s" % sha
-        if len(commit.parents) > 1:
-            print "merge: %s" % "...".join(commit.parents[1:])
-        print "author: %s" % commit.author
-        print "committer: %s" % commit.committer
-        print ""
-        print commit.message
-        print ""
-        todo.extend([p for p in commit.parents if p not in done])
+    porcelain.log(repo=path, outstream=sys.stdout)
 
 
 def cmd_diff(args):

+ 29 - 0
dulwich/porcelain.py

@@ -147,3 +147,32 @@ def rm(repo=".", paths=None):
     for p in paths:
         del index[p]
     index.write()
+
+
+def print_commit(commit, outstream):
+    """Write a human-readable commit log entry.
+
+    :param commit: A `Commit` object
+    :param outstream: A stream file to write to
+    """
+    outstream.write("-" * 50 + "\n")
+    outstream.write("commit: %s\n" % commit.id)
+    if len(commit.parents) > 1:
+        outstream.write("merge: %s\n" % "...".join(commit.parents[1:]))
+    outstream.write("author: %s\n" % commit.author)
+    outstream.write("committer: %s\n" % commit.committer)
+    outstream.write("\n")
+    outstream.write(commit.message + "\n")
+    outstream.write("\n")
+
+
+def log(repo=".", outstream=sys.stdout):
+    """Write commit logs.
+
+    :param repo: Path to repository
+    :param outstream: Stream to write log output to
+    """
+    r = open_repo(repo)
+    walker = r.get_walker()
+    for entry in walker:
+        print_commit(entry.commit, outstream)

+ 11 - 0
dulwich/tests/test_porcelain.py

@@ -130,3 +130,14 @@ class RemoveTests(PorcelainTestCase):
             f.close()
         porcelain.add(self.repo.path, paths=["foo"])
         porcelain.rm(self.repo.path, paths=["foo"])
+
+
+class LogTests(PorcelainTestCase):
+
+    def test_simple(self):
+        c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
+            [3, 1, 2]])
+        self.repo.refs["HEAD"] = c3.id
+        outstream = StringIO()
+        porcelain.log(self.repo.path, outstream=outstream)
+        self.assertTrue(outstream.getvalue().startswith("-" * 50))