Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions stdlib/LibGit2/src/tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ function GitTree(c::GitCommit)
GitTree(repository(c), tree_out[])
end

"""
GitTree(repo::GitRepo, tree_oid::GitHash)

Look up a tree object in the repository using its GitHash.
This constructor wraps the libgit2 git_tree_lookup function.

# Examples
```julia
tree_hash = LibGit2.GitHash(repo, "HEAD^{tree}")
tree = LibGit2.GitTree(repo, tree_hash)
```
"""
function GitTree(repo::GitRepo, tree_oid::GitHash)
ensure_initialized()
tree_out = Ref{Ptr{Cvoid}}(C_NULL)
oid_ptr = Ref(tree_oid)
@check ccall((:git_tree_lookup, libgit2), Cint,
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{GitHash}),
tree_out, repo, oid_ptr)
return GitTree(repo, tree_out[])
end

"""
treewalk(f, tree::GitTree, post::Bool=false)

Expand Down
8 changes: 8 additions & 0 deletions stdlib/LibGit2/test/libgit2-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,14 @@ mktempdir() do dir
rethrow()
end
end

# Test GitTree constructor with GitHash
tree1 = LibGit2.GitTree(repo, "HEAD^{tree}")
tree_hash = LibGit2.GitHash(tree1)
tree2 = LibGit2.GitTree(repo, tree_hash)
@test isa(tree2, LibGit2.GitTree)
@test LibGit2.GitHash(tree1) == LibGit2.GitHash(tree2)
@test LibGit2.count(tree1) == LibGit2.count(tree2)
end
end

Expand Down