From 2b891b743fa985c31c566d51b2fbf54aeef6c57b Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Sat, 21 Jan 2017 10:23:12 +0000 Subject: [PATCH] fix some LibGit2 errors (#20155) fix some LibGit2 errors (wrong name for DiffOptionsStruct, missing cached kwarg). Add some tests for the above. --- base/libgit2/diff.jl | 6 +++--- base/libgit2/libgit2.jl | 24 ++++++++++++++++++------ test/libgit2.jl | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/base/libgit2/diff.jl b/base/libgit2/diff.jl index 96d494d6687d7..c3f202a90cec9 100644 --- a/base/libgit2/diff.jl +++ b/base/libgit2/diff.jl @@ -4,11 +4,11 @@ function Base.cconvert(::Type{Ptr{DiffOptionsStruct}}, pathspecs::AbstractString) str_ref = Base.cconvert(Ref{Cstring}, [pathspecs]) sa = StrArrayStruct(Base.unsafe_convert(Ref{Cstring}, str_ref), 1) - do_ref = Ref(DiffOptions(pathspec = sa)) + do_ref = Ref(DiffOptionsStruct(pathspec = sa)) do_ref, str_ref end function Base.unsafe_convert(::Type{Ptr{DiffOptionsStruct}}, rr::Tuple{Ref{DiffOptionsStruct}, Ref{Cstring}}) - Base.unsafe_convert(Ptr{DiffOptionStruct}, first(rr)) + Base.unsafe_convert(Ptr{DiffOptionsStruct}, first(rr)) end @@ -42,6 +42,6 @@ function Base.getindex(diff::GitDiff, i::Integer) delta_ptr = ccall((:git_diff_get_delta, :libgit2), Ptr{DiffDelta}, (Ptr{Void}, Csize_t), diff.ptr, i-1) - delta_ptr == C_NULL && return nothing + delta_ptr == C_NULL && throw(BoundsError(diff, (i,))) return unsafe_load(delta_ptr) end diff --git a/base/libgit2/libgit2.jl b/base/libgit2/libgit2.jl index 4acf48df22658..781a3a595f56c 100644 --- a/base/libgit2/libgit2.jl +++ b/base/libgit2/libgit2.jl @@ -73,22 +73,34 @@ function iscommit(id::AbstractString, repo::GitRepo) return res end -""" git diff-index HEAD [-- ]""" +""" + LibGit2.isdirty(repo::GitRepo[, paths]; cached=false) + +Checks if there have been any changes to tracked files in the working tree (if +`cached=false`) or the index (if `cached=true`). + +See `git diff-index HEAD [-- ]` +""" isdirty(repo::GitRepo, paths::AbstractString=""; cached::Bool=false) = isdiff(repo, Consts.HEAD_FILE, paths, cached=cached) -""" git diff-index [-- ]""" +""" + LibGit2.isdiff(repo::GitRepo, treeish[, paths]; cached=false) + +Checks if there are any differences between the tree specified by `treeish` and the +tracked files in the working tree (if `cached=false`) or the index (if `cached=true`). + +See `git diff-index [-- ]` +""" function isdiff(repo::GitRepo, treeish::AbstractString, paths::AbstractString=""; cached::Bool=false) tree_oid = revparseid(repo, "$treeish^{tree}") - iszero(tree_oid) && return true + iszero(tree_oid) && error("invalid treeish $treeish") # this can be removed by #20104 result = false tree = get(GitTree, repo, tree_oid) try - diff = diff_tree(repo, tree, paths) + diff = diff_tree(repo, tree, paths, cached=cached) result = count(diff) > 0 close(diff) - catch err - result = true finally close(tree) end diff --git a/test/libgit2.jl b/test/libgit2.jl index c86994547b602..038ff31b39d18 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -452,6 +452,41 @@ mktempdir() do dir close(repo) end end + + @testset "diff" begin + repo = LibGit2.GitRepo(cache_repo) + try + @test !LibGit2.isdirty(repo) + @test !LibGit2.isdirty(repo, test_file) + @test !LibGit2.isdirty(repo, "nonexistent") + @test !LibGit2.isdiff(repo, "HEAD") + @test !LibGit2.isdirty(repo, cached=true) + @test !LibGit2.isdirty(repo, test_file, cached=true) + @test !LibGit2.isdirty(repo, "nonexistent", cached=true) + @test !LibGit2.isdiff(repo, "HEAD", cached=true) + open(joinpath(cache_repo,test_file), "a") do f + println(f, "zzzz") + end + @test LibGit2.isdirty(repo) + @test LibGit2.isdirty(repo, test_file) + @test !LibGit2.isdirty(repo, "nonexistent") + @test LibGit2.isdiff(repo, "HEAD") + @test !LibGit2.isdirty(repo, cached=true) + @test !LibGit2.isdiff(repo, "HEAD", cached=true) + LibGit2.add!(repo, test_file) + @test LibGit2.isdirty(repo) + @test LibGit2.isdiff(repo, "HEAD") + @test LibGit2.isdirty(repo, cached=true) + @test LibGit2.isdiff(repo, "HEAD", cached=true) + LibGit2.commit(repo, "zzz") + @test !LibGit2.isdirty(repo) + @test !LibGit2.isdiff(repo, "HEAD") + @test !LibGit2.isdirty(repo, cached=true) + @test !LibGit2.isdiff(repo, "HEAD", cached=true) + finally + close(repo) + end + end end @testset "Fetch from cache repository" begin