Permalink
Browse files

Support non-commit objects and multiple objects in 'show' porcelain.

  • Loading branch information...
1 parent df9ff57 commit 3a657164363ac392730cbda6704ec0b44266729d @jelmer committed Jan 18, 2014
Showing with 91 additions and 10 deletions.
  1. +1 −1 bin/dulwich
  2. +74 −8 dulwich/porcelain.py
  3. +16 −1 dulwich/tests/test_porcelain.py
View
@@ -205,7 +205,7 @@ def cmd_symbolic_ref(args):
def cmd_show(args):
opts, args = getopt(args, "", [])
- porcelain.show(".")
+ porcelain.show(".", args)
def cmd_diff_tree(args):
View
@@ -222,6 +222,72 @@ def print_commit(commit, outstream):
outstream.write("\n")
+def print_tag(tag, outstream):
+ """Write a human-readable tag.
+
+ :param tag: A `Tag` object
+ :param outstream: A stream to write to
+ """
+ outstream.write("Tagger: %s\n" % tag.tagger)
+ outstream.write("Date: %s\n" % tag.tag_time)
+ outstream.write("\n")
+ outstream.write("%s\n" % tag.message)
+ outstream.write("\n")
+
+
+def show_blob(repo, blob, outstream):
+ """Write a blob to a stream.
+
+ :param repo: A `Repo` object
+ :param blob: A `Blob` object
+ :param outstream: A stream file to write to
+ """
+ outstream.write(blob.data)
+
+
+def show_commit(repo, commit, outstream):
+ """Show a commit to a stream.
+
+ :param repo: A `Repo` object
+ :param commit: A `Commit` object
+ :param outstream: Stream to write to
+ """
+ print_commit(commit, outstream)
+ parent_commit = repo[commit.parents[0]]
+ write_tree_diff(outstream, repo.object_store, parent_commit.tree, commit.tree)
+
+
+def show_tree(repo, tree, outstream):
+ """Print a tree to a stream.
+
+ :param repo: A `Repo` object
+ :param tree: A `Tree` object
+ :param outstream: Stream to write to
+ """
+ for n in tree:
+ outstream.write("%s\n" % n)
+
+
+def show_tag(repo, tag, outstream):
+ """Print a tag to a stream.
+
+ :param repo: A `Repo` object
+ :param tag: A `Tag` object
+ :param outstream: Stream to write to
+ """
+ print_tag(tag, outstream)
+ show_object(repo, repo[tag.object[1]], outstream)
+
+
+def show_object(repo, obj, outstream):
+ return {
+ "tree": show_tree,
+ "blob": show_blob,
+ "commit": show_commit,
+ "tag": show_tag,
+ }[obj.type_name](repo, obj, outstream)
+
+
def log(repo=".", outstream=sys.stdout):
"""Write commit logs.
@@ -234,20 +300,20 @@ def log(repo=".", outstream=sys.stdout):
print_commit(entry.commit, outstream)
-def show(repo=".", committish=None, outstream=sys.stdout):
+def show(repo=".", objects=None, outstream=sys.stdout):
"""Print the changes in a commit.
:param repo: Path to repository
- :param committish: Commit to show (defaults to HEAD)
+ :param objects: Objects to show (defaults to [HEAD])
:param outstream: Stream to write to
"""
- if committish is None:
- committish = "HEAD"
+ if objects is None:
+ objects = ["HEAD"]
+ if not isinstance(objects, list):
+ objects = [objects]
r = open_repo(repo)
- commit = r[committish]
- parent_commit = r[commit.parents[0]]
- print_commit(commit, outstream)
- write_tree_diff(outstream, r.object_store, parent_commit.tree, commit.tree)
+ for obj in objects:
+ show_object(r, r[obj], outstream)
def diff_tree(repo, old_tree, new_tree, outstream=sys.stdout):
@@ -212,14 +212,29 @@ def test_simple(self):
class ShowTests(PorcelainTestCase):
+ def test_nolist(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.show(self.repo.path, objects=c3.id, outstream=outstream)
+ self.assertTrue(outstream.getvalue().startswith("-" * 50))
+
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.show(self.repo.path, committish=c3.id, outstream=outstream)
+ porcelain.show(self.repo.path, objects=[c3.id], outstream=outstream)
self.assertTrue(outstream.getvalue().startswith("-" * 50))
+ def test_blob(self):
+ b = Blob.from_string("The Foo\n")
+ self.repo.object_store.add_object(b)
+ outstream = StringIO()
+ porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
+ self.assertEquals(outstream.getvalue(), "The Foo\n")
+
class SymbolicRefTests(PorcelainTestCase):

0 comments on commit 3a65716

Please sign in to comment.