Skip to content

Commit

Permalink
Fix dulwich.porcelain.show on commits with Python3. Fixes jelmer#532
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Jul 30, 2017
1 parent 7f02fa1 commit b9643bc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
* Handle race condition when mtime doesn't change between writes/reads.
(Jelmer Vernooij, #541)

* Fix ``dulwich.porcelain.show`` on commits with Python 3.
(Jelmer Vernooij, #532)

IMPROVEMENTS

* Add basic support for reading ignore files in ``dulwich.ignore``.
Expand Down
1 change: 0 additions & 1 deletion dulwich/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ def lines(blob):
old_path, new_path))


# TODO(jelmer): Support writing unicode, rather than bytes.
def write_tree_diff(f, store, old_tree, new_tree, diff_binary=False):
"""Write tree diff.
Expand Down
15 changes: 13 additions & 2 deletions dulwich/porcelain.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
closing,
contextmanager,
)
from io import BytesIO
import os
import posixpath
import stat
Expand Down Expand Up @@ -470,9 +471,19 @@ def show_commit(repo, commit, decode, outstream=sys.stdout):
:param outstream: Stream to write to
"""
print_commit(commit, decode=decode, outstream=outstream)
parent_commit = repo[commit.parents[0]]
if commit.parents:
parent_commit = repo[commit.parents[0]]
base_tree = parent_commit.tree
else:
base_tree = None
diffstream = BytesIO()
write_tree_diff(
outstream, repo.object_store, parent_commit.tree, commit.tree)
diffstream,
repo.object_store, base_tree, commit.tree)
diffstream.seek(0)
outstream.write(
diffstream.getvalue().decode(
commit.encoding or DEFAULT_ENCODING, 'replace'))


def show_tree(repo, tree, decode, outstream=sys.stdout):
Expand Down
59 changes: 59 additions & 0 deletions dulwich/tests/test_porcelain.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
)
from dulwich.tests.utils import (
build_commit_graph,
make_commit,
make_object,
)

Expand Down Expand Up @@ -376,6 +377,64 @@ def test_blob(self):
porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
self.assertEqual(outstream.getvalue(), "The Foo\n")

def test_commit_no_parent(self):
a = Blob.from_string(b"The Foo\n")
ta = Tree()
ta.add(b"somename", 0o100644, a.id)
ca = make_commit(tree=ta.id)
self.repo.object_store.add_objects([(a, None), (ta, None), (ca, None)])
outstream = StringIO()
porcelain.show(self.repo.path, objects=[ca.id], outstream=outstream)
self.assertEqual(outstream.getvalue(), """\
--------------------------------------------------
commit: 344da06c1bb85901270b3e8875c988a027ec087d
Author: Test Author <test@nodomain.com>
Committer: Test Committer <test@nodomain.com>
Date: Fri Jan 01 2010 00:00:00 +0000
Test message.
diff --git /dev/null b/somename
new mode 100644
index 0000000..ea5c7bf 100644
--- /dev/null
+++ b/somename
@@ -1,0 +1,1 @@
+The Foo
""")

def test_commit_with_change(self):
a = Blob.from_string(b"The Foo\n")
ta = Tree()
ta.add(b"somename", 0o100644, a.id)
ca = make_commit(tree=ta.id)
b = Blob.from_string(b"The Bar\n")
tb = Tree()
tb.add(b"somename", 0o100644, a.id)
cb = make_commit(tree=tb.id)
self.repo.object_store.add_objects(
[(a, None), (b, None), (ta, None), (tb, None),
(ca, None), (cb, None)])
outstream = StringIO()
porcelain.show(self.repo.path, objects=[cb.id], outstream=outstream)
self.assertEqual(outstream.getvalue(), """\
--------------------------------------------------
commit: 344da06c1bb85901270b3e8875c988a027ec087d
Author: Test Author <test@nodomain.com>
Committer: Test Committer <test@nodomain.com>
Date: Fri Jan 01 2010 00:00:00 +0000
Test message.
diff --git /dev/null b/somename
new mode 100644
index 0000000..ea5c7bf 100644
--- /dev/null
+++ b/somename
@@ -1,0 +1,1 @@
+The Foo
""")


class SymbolicRefTests(PorcelainTestCase):

Expand Down

0 comments on commit b9643bc

Please sign in to comment.