Skip to content

Commit 2b891b7

Browse files
authored
fix some LibGit2 errors (JuliaLang#20155)
fix some LibGit2 errors (wrong name for DiffOptionsStruct, missing cached kwarg). Add some tests for the above.
1 parent 8ef1352 commit 2b891b7

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

base/libgit2/diff.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
function Base.cconvert(::Type{Ptr{DiffOptionsStruct}}, pathspecs::AbstractString)
55
str_ref = Base.cconvert(Ref{Cstring}, [pathspecs])
66
sa = StrArrayStruct(Base.unsafe_convert(Ref{Cstring}, str_ref), 1)
7-
do_ref = Ref(DiffOptions(pathspec = sa))
7+
do_ref = Ref(DiffOptionsStruct(pathspec = sa))
88
do_ref, str_ref
99
end
1010
function Base.unsafe_convert(::Type{Ptr{DiffOptionsStruct}}, rr::Tuple{Ref{DiffOptionsStruct}, Ref{Cstring}})
11-
Base.unsafe_convert(Ptr{DiffOptionStruct}, first(rr))
11+
Base.unsafe_convert(Ptr{DiffOptionsStruct}, first(rr))
1212
end
1313

1414

@@ -42,6 +42,6 @@ function Base.getindex(diff::GitDiff, i::Integer)
4242
delta_ptr = ccall((:git_diff_get_delta, :libgit2),
4343
Ptr{DiffDelta},
4444
(Ptr{Void}, Csize_t), diff.ptr, i-1)
45-
delta_ptr == C_NULL && return nothing
45+
delta_ptr == C_NULL && throw(BoundsError(diff, (i,)))
4646
return unsafe_load(delta_ptr)
4747
end

base/libgit2/libgit2.jl

+18-6
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,34 @@ function iscommit(id::AbstractString, repo::GitRepo)
7373
return res
7474
end
7575

76-
""" git diff-index HEAD [-- <path>]"""
76+
"""
77+
LibGit2.isdirty(repo::GitRepo[, paths]; cached=false)
78+
79+
Checks if there have been any changes to tracked files in the working tree (if
80+
`cached=false`) or the index (if `cached=true`).
81+
82+
See `git diff-index HEAD [-- <path>]`
83+
"""
7784
isdirty(repo::GitRepo, paths::AbstractString=""; cached::Bool=false) =
7885
isdiff(repo, Consts.HEAD_FILE, paths, cached=cached)
7986

80-
""" git diff-index <treeish> [-- <path>]"""
87+
"""
88+
LibGit2.isdiff(repo::GitRepo, treeish[, paths]; cached=false)
89+
90+
Checks if there are any differences between the tree specified by `treeish` and the
91+
tracked files in the working tree (if `cached=false`) or the index (if `cached=true`).
92+
93+
See `git diff-index <treeish> [-- <path>]`
94+
"""
8195
function isdiff(repo::GitRepo, treeish::AbstractString, paths::AbstractString=""; cached::Bool=false)
8296
tree_oid = revparseid(repo, "$treeish^{tree}")
83-
iszero(tree_oid) && return true
97+
iszero(tree_oid) && error("invalid treeish $treeish") # this can be removed by #20104
8498
result = false
8599
tree = get(GitTree, repo, tree_oid)
86100
try
87-
diff = diff_tree(repo, tree, paths)
101+
diff = diff_tree(repo, tree, paths, cached=cached)
88102
result = count(diff) > 0
89103
close(diff)
90-
catch err
91-
result = true
92104
finally
93105
close(tree)
94106
end

test/libgit2.jl

+35
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,41 @@ mktempdir() do dir
452452
close(repo)
453453
end
454454
end
455+
456+
@testset "diff" begin
457+
repo = LibGit2.GitRepo(cache_repo)
458+
try
459+
@test !LibGit2.isdirty(repo)
460+
@test !LibGit2.isdirty(repo, test_file)
461+
@test !LibGit2.isdirty(repo, "nonexistent")
462+
@test !LibGit2.isdiff(repo, "HEAD")
463+
@test !LibGit2.isdirty(repo, cached=true)
464+
@test !LibGit2.isdirty(repo, test_file, cached=true)
465+
@test !LibGit2.isdirty(repo, "nonexistent", cached=true)
466+
@test !LibGit2.isdiff(repo, "HEAD", cached=true)
467+
open(joinpath(cache_repo,test_file), "a") do f
468+
println(f, "zzzz")
469+
end
470+
@test LibGit2.isdirty(repo)
471+
@test LibGit2.isdirty(repo, test_file)
472+
@test !LibGit2.isdirty(repo, "nonexistent")
473+
@test LibGit2.isdiff(repo, "HEAD")
474+
@test !LibGit2.isdirty(repo, cached=true)
475+
@test !LibGit2.isdiff(repo, "HEAD", cached=true)
476+
LibGit2.add!(repo, test_file)
477+
@test LibGit2.isdirty(repo)
478+
@test LibGit2.isdiff(repo, "HEAD")
479+
@test LibGit2.isdirty(repo, cached=true)
480+
@test LibGit2.isdiff(repo, "HEAD", cached=true)
481+
LibGit2.commit(repo, "zzz")
482+
@test !LibGit2.isdirty(repo)
483+
@test !LibGit2.isdiff(repo, "HEAD")
484+
@test !LibGit2.isdirty(repo, cached=true)
485+
@test !LibGit2.isdiff(repo, "HEAD", cached=true)
486+
finally
487+
close(repo)
488+
end
489+
end
455490
end
456491

457492
@testset "Fetch from cache repository" begin

0 commit comments

Comments
 (0)