Skip to content

Commit

Permalink
add tests for isdiff/isdirty, fix an error
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Jan 20, 2017
1 parent 83012fa commit 724c020
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
14 changes: 9 additions & 5 deletions base/libgit2/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,22 @@ end
isdirty(repo::GitRepo, paths::AbstractString=""; cached::Bool=false) =
isdiff(repo, Consts.HEAD_FILE, paths, cached=cached)

""" git diff-index <treeish> [-- <path>]"""
"""
LibGit2.isdiff(repo::GitRepo, treeish[, paths]; [cached=false])
Checks if there are any differences between the tree specified by `treeish` and working tree (if `cached=false`) or the index (if `cached=true`).
See `git diff-index <treeish> [-- <path>]`
"""
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) && return 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
Expand Down
30 changes: 30 additions & 0 deletions test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,36 @@ mktempdir() do dir
close(repo)
end
end

@testset "diff" begin
repo = LibGit2.GitRepo(cache_repo)
try
@test !LibGit2.isdirty(repo)
@test !LibGit2.isdiff(repo, "HEAD")
@test !LibGit2.isdirty(repo, 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.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
Expand Down

0 comments on commit 724c020

Please sign in to comment.