From 69e0908a8dc3170e51285b50bed6f404357e85bd Mon Sep 17 00:00:00 2001 From: kshyatt Date: Mon, 9 Jan 2017 11:21:51 -0800 Subject: [PATCH] Show methods for Signatures and Commits Add `show` methods for `Signature` and `Commit`s in LibGit2. Display the time of the commit using Julia's built-in date handling. --- base/libgit2/commit.jl | 6 ++++++ base/libgit2/signature.jl | 2 ++ test/libgit2.jl | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/base/libgit2/commit.jl b/base/libgit2/commit.jl index 87fbe402114d2..1afd4054702b8 100644 --- a/base/libgit2/commit.jl +++ b/base/libgit2/commit.jl @@ -22,6 +22,12 @@ function committer(c::GitCommit) return Signature(ptr) end +function Base.show(io::IO, c::GitCommit) + authstr = sprint(show, author(c)) + cmtrstr = sprint(show, committer(c)) + print(io, "Commit Author: $authstr\nCommitter: $cmtrstr\nMessage:\n$(message(c))") +end + """ Wrapper around `git_commit_create` """ function commit(repo::GitRepo, refname::AbstractString, diff --git a/base/libgit2/signature.jl b/base/libgit2/signature.jl index 69f477b70d1a3..60d55526ad715 100644 --- a/base/libgit2/signature.jl +++ b/base/libgit2/signature.jl @@ -35,6 +35,8 @@ function Base.convert(::Type{GitSignature}, sig::Signature) return GitSignature(sig_ptr_ptr[]) end +Base.show(io::IO, sig::Signature) = print(io, "Name: $(sig.name), Email: $(sig.email), Time: $(Dates.unix2datetime(sig.time + sig.time_offset))") + """Return signature object. Free it after use.""" function default_signature(repo::GitRepo) sig_ptr_ptr = Ref{Ptr{SignatureStruct}}(C_NULL) diff --git a/test/libgit2.jl b/test/libgit2.jl index 05007b489e329..d66a96566684a 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -302,6 +302,12 @@ mktempdir() do dir @test cmtr.time == test_sig.time @test cmtr.email == test_sig.email @test LibGit2.message(cmt) == commit_msg1 + showstr = split(sprint(show, cmt), "\n") + # the time of the commit will vary so just test the first two parts + @test contains(showstr[1], "Commit Author: Name: TEST, Email: TEST@TEST.COM, Time:") + @test contains(showstr[2], "Committer: Name: TEST, Email: TEST@TEST.COM, Time:") + @test showstr[3] == "Message:" + @test showstr[4] == commit_msg1 finally finalize(cmt) end