Skip to content

Commit

Permalink
Merge pull request JuliaLang#15662 from JuliaLang/ksh/libgit
Browse files Browse the repository at this point in the history
Added tests for libgit2 and an error throw
  • Loading branch information
kshyatt committed Mar 29, 2016
2 parents fc11cf2 + 9e3afb2 commit 48a8493
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
10 changes: 9 additions & 1 deletion base/libgit2/reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,16 @@ end
function upstream(ref::GitReference)
isempty(ref) && return nothing
ref_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_branch_upstream, :libgit2), Cint,
err = ccall((:git_branch_upstream, :libgit2), Cint,
(Ref{Ptr{Void}}, Ptr{Void},), ref_ptr_ptr, ref.ptr)
if err == Int(Error.ENOTFOUND)
return nothing
elseif err != Int(Error.GIT_OK)
if repo_ptr_ptr[] != C_NULL
finalize(GitReference(ref_ptr_ptr[]))
end
throw(Error.GitError(err))
end
return GitReference(ref_ptr_ptr[])
end

Expand Down
5 changes: 3 additions & 2 deletions base/libgit2/status.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ function Base.length(status::GitStatus)
end

function Base.getindex(status::GitStatus, i::Csize_t)
if length(status) == 0
throw(BoundsError())
end
entry_ptr = ccall((:git_status_byindex, :libgit2), Ptr{Void},
(Ptr{Void}, Csize_t), status.ptr, i-1)
entry_ptr == C_NULL && return nothing
return unsafe_load(convert(Ptr{StatusEntry}, entry_ptr), 1)
end
Base.getindex(status::GitStatus, i::Int) = getindex(status, Csize_t(i))


47 changes: 46 additions & 1 deletion test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ const LIBGIT2_VER = v"0.23.0"
@test sig.name == sig2.name
@test sig.email == sig2.email
@test sig.time == sig2.time
sig3 = LibGit2.Signature("AAA","[email protected]")
@test sig3.name == sig.name
@test sig3.email == sig.email
#end

mktempdir() do dir
Expand Down Expand Up @@ -117,6 +120,7 @@ mktempdir() do dir

remote = LibGit2.get(LibGit2.GitRemote, repo, branch)
@test LibGit2.url(remote) == repo_url
@test LibGit2.isattached(repo)
finalize(remote)
finally
finalize(repo)
Expand All @@ -129,9 +133,19 @@ mktempdir() do dir
try
@test isdir(path)
@test isfile(joinpath(path, LibGit2.Consts.HEAD_FILE))
@test LibGit2.isattached(repo)
finally
finalize(repo)
end

path = joinpath("garbagefakery", "Example.Bare")
try
LibGit2.GitRepo(path)
error("unexpected")
catch e
@test typeof(e) == LibGit2.GitError
@test startswith(sprint(show,e),"GitError(Code:ENOTFOUND, Class:OS, Failed to resolve path")
end
#end
#end

Expand All @@ -143,6 +157,8 @@ mktempdir() do dir
try
@test isdir(repo_path)
@test isfile(joinpath(repo_path, LibGit2.Consts.HEAD_FILE))
@test LibGit2.isattached(repo)
@test LibGit2.remotes(repo) == ["origin"]
finally
finalize(repo)
end
Expand All @@ -156,6 +172,8 @@ mktempdir() do dir
rmt = LibGit2.get(LibGit2.GitRemote, repo, "origin")
try
@test LibGit2.fetch_refspecs(rmt)[1] == "+refs/*:refs/*"
@test LibGit2.isattached(repo)
@test LibGit2.remotes(repo) == ["origin"]
finally
finalize(rmt)
end
Expand All @@ -168,6 +186,7 @@ mktempdir() do dir
try
@test isdir(test_repo)
@test isdir(joinpath(test_repo, ".git"))
@test LibGit2.isattached(repo)
finally
finalize(repo)
end
Expand Down Expand Up @@ -228,8 +247,15 @@ mktempdir() do dir
repo = LibGit2.GitRepo(cache_repo)
try
brnch = LibGit2.branch(repo)
brref = LibGit2.head(repo)
@test LibGit2.isbranch(brref)
@test LibGit2.name(brref) == "refs/heads/master"
@test LibGit2.shortname(brref) == "master"
@test LibGit2.ishead(brref)
@test LibGit2.upstream(brref) == nothing
@test repo.ptr == LibGit2.owner(brref).ptr
@test brnch == "master"

@test LibGit2.headname(repo) == "master"
LibGit2.branch!(repo, test_branch, string(commit_oid1), set_head=false)

branches = map(b->LibGit2.shortname(b[1]), LibGit2.GitBranchIter(repo))
Expand Down Expand Up @@ -290,6 +316,25 @@ mktempdir() do dir
finalize(repo)
end
#end

#@testset "status" begin
repo = LibGit2.GitRepo(cache_repo)
try
status = LibGit2.GitStatus(repo)
@test length(status) == 0
@test_throws BoundsError status[1]
repo_file = open(joinpath(cache_repo,"statusfile"), "a")

# create commits
println(repo_file, commit_msg1)
flush(repo_file)
LibGit2.add!(repo, test_file)
status = LibGit2.GitStatus(repo)
@test length(status) != 0
finally
finalize(repo)
close(repo_file)
end
#end

#@testset "Fetch from cache repository" begin
Expand Down

0 comments on commit 48a8493

Please sign in to comment.