Skip to content

Commit 65ffa10

Browse files
authored
LibGit2: Add GitTree constructor that wraps git_tree_lookup (#59994)
This adds a new constructor GitTree(repo::GitRepo, tree_oid::GitHash) that directly wraps the libgit2 git_tree_lookup function. This provides a more efficient and direct way to create a GitTree object when you already have the tree hash, complementing the existing constructor that creates a GitTree from a GitCommit. Written by Claude. Co-authored-by: Keno Fischer <[email protected]>
1 parent ed705d8 commit 65ffa10

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

stdlib/LibGit2/src/tree.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ function GitTree(c::GitCommit)
66
GitTree(repository(c), tree_out[])
77
end
88

9+
"""
10+
GitTree(repo::GitRepo, tree_oid::GitHash)
11+
12+
Look up a tree object in the repository using its GitHash.
13+
This constructor wraps the libgit2 git_tree_lookup function.
14+
15+
# Examples
16+
```julia
17+
tree_hash = LibGit2.GitHash(repo, "HEAD^{tree}")
18+
tree = LibGit2.GitTree(repo, tree_hash)
19+
```
20+
"""
21+
function GitTree(repo::GitRepo, tree_oid::GitHash)
22+
ensure_initialized()
23+
tree_out = Ref{Ptr{Cvoid}}(C_NULL)
24+
oid_ptr = Ref(tree_oid)
25+
@check ccall((:git_tree_lookup, libgit2), Cint,
26+
(Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{GitHash}),
27+
tree_out, repo, oid_ptr)
28+
return GitTree(repo, tree_out[])
29+
end
30+
931
"""
1032
treewalk(f, tree::GitTree, post::Bool=false)
1133

stdlib/LibGit2/test/libgit2-tests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,14 @@ mktempdir() do dir
10821082
rethrow()
10831083
end
10841084
end
1085+
1086+
# Test GitTree constructor with GitHash
1087+
tree1 = LibGit2.GitTree(repo, "HEAD^{tree}")
1088+
tree_hash = LibGit2.GitHash(tree1)
1089+
tree2 = LibGit2.GitTree(repo, tree_hash)
1090+
@test isa(tree2, LibGit2.GitTree)
1091+
@test LibGit2.GitHash(tree1) == LibGit2.GitHash(tree2)
1092+
@test LibGit2.count(tree1) == LibGit2.count(tree2)
10851093
end
10861094
end
10871095

0 commit comments

Comments
 (0)