Skip to content

Commit

Permalink
fixed get with default for GitConfig
Browse files Browse the repository at this point in the history
fixed `libgit2` tests
  • Loading branch information
wildart committed May 18, 2015
1 parent 7efbda3 commit c6612f1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion base/pkg/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function fetch(repo::GitRepo, remote::AbstractString="origin";
end

try
fetch(rmt)
fetch(repo, rmt, msg="from $(url(rmt))")
catch err
warn("fetch: $err")
finally
Expand Down
4 changes: 3 additions & 1 deletion base/pkg/libgit2/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ function get{T}(::Type{T}, c::GitConfig, name::AbstractString)
end

function get{T}(c::GitConfig, name::AbstractString, default::T)
return try get(T,c,name) catch default end
res = default
try res = get(T,c,name) end
return res
end

function set!{T}(c::GitConfig, name::AbstractString, value::T)
Expand Down
19 changes: 9 additions & 10 deletions base/pkg/libgit2/repository.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,13 @@ function checkout_head(repo::GitRepo; options::CheckoutOptionsStruct = CheckoutO
repo.ptr, Ref(options))
end

function fetch(rmt::GitRemote)
@check ccall((:git_remote_fetch, :libgit2), Cint,
(Ptr{Void}, Ptr{Void}, Ptr{UInt8}),
rmt.ptr, C_NULL, C_NULL)
function fetch(repo::GitRepo, rmt::GitRemote; msg::AbstractString="")
msg = "pkg.libgit2.fetch: $msg"
with(default_signature(repo)) do sig
@check ccall((:git_remote_fetch, :libgit2), Cint,
(Ptr{Void}, Ptr{Void}, Ptr{SignatureStruct}, Ptr{UInt8}),
rmt.ptr, C_NULL, sig.ptr, msg)
end
end

function reset!(repo::GitRepo, obj::Nullable{GitAnyObject}, pathspecs::AbstractString...)
Expand All @@ -155,14 +158,10 @@ end

function reset!(repo::GitRepo, obj::GitObject, mode::Cint;
checkout_opts::CheckoutOptionsStruct = CheckoutOptionsStruct())
sig = default_signature(repo)
msg = "pkg.libgit2.reset: moving to $(string(Oid(obj)))"
try
with(default_signature(repo)) do sig
msg = "pkg.libgit2.reset: moving to $(string(Oid(obj)))"
@check ccall((:git_reset, :libgit2), Cint,
(Ptr{Void}, Ptr{Void}, Cint, Ptr{CheckoutOptionsStruct}, Ptr{SignatureStruct}, Ptr{UInt8}),
repo.ptr, obj.ptr, mode, Ref(checkout_opts), sig.ptr, msg)
finally
finalize(sig)
end
return
end
27 changes: 21 additions & 6 deletions test/libgit2.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# check that libgit2 has been installed correctly

const LIBGIT2_VER = v"0.22+"
const LIBGIT2_VER = v"0.22.2+"

function check_version()
major, minor, patch = Cint[0], Cint[0], Cint[0]
Expand All @@ -16,12 +16,16 @@ end

@test check_version()

function credentials!(cfg::Pkg.LibGit2.GitConfig, usr="Test User", usr_email="[email protected]")
git_user = Pkg.LibGit2.get(cfg, "user.name", usr)
usr==git_user && Pkg.LibGit2.set!(cfg, "user.name", usr)
git_user_email = Pkg.LibGit2.get(cfg, "user.email", usr_email)
usr_email==git_user_email && Pkg.LibGit2.set!(cfg, "user.email", usr_email)
end

#TODO: tests need 'user.name' & 'user.email' in config ???
Pkg.LibGit2.with(Pkg.LibGit2.GitConfig) do cfg
git_user = Pkg.LibGit2.get(cfg, "user.name", "")
isempty(git_user) && Pkg.LibGit2.set(cfg, "user.email", "Test User")
git_user_email = Pkg.LibGit2.get(cfg, "user.email", "")
isempty(git_user) && Pkg.LibGit2.set(cfg, "user.email", "[email protected]")
credentials!(cfg)
end

function temp_dir(fn::Function, remove_tmp_dir::Bool=true)
Expand Down Expand Up @@ -90,6 +94,9 @@ temp_dir() do dir_cache
url = "https://github.com/JuliaLang/Example.jl"
path_cache = joinpath(dir_cache, "Example.Bare")
repo = Pkg.LibGit2.clone(url, path_cache, bare = true, remote_cb = Pkg.LibGit2.mirror_cb)
Pkg.LibGit2.with(Pkg.LibGit2.GitConfig, repo) do cfg
credentials!(cfg)
end
Pkg.LibGit2.finalize(repo)


Expand All @@ -99,8 +106,10 @@ temp_dir() do dir_cache
# clone repo
path = joinpath(dir, "Example")
repo = Pkg.LibGit2.clone(path_cache, path)
Pkg.LibGit2.with(Pkg.LibGit2.GitConfig, repo) do cfg
credentials!(cfg)
end

# fetch
Pkg.LibGit2.fetch(repo)
refs1 = parse(Int, readchomp(pipe(`find $(joinpath(path, ".git/refs"))`,`wc -l`)))

Expand All @@ -118,6 +127,9 @@ temp_dir() do dir_cache
temp_dir() do dir
path = joinpath(dir, "Example")
repo = Pkg.LibGit2.clone(path_cache, path)
Pkg.LibGit2.with(Pkg.LibGit2.GitConfig, repo) do cfg
credentials!(cfg)
end
oids = Pkg.LibGit2.map((oid,repo)->string(oid), repo, by = Pkg.LibGit2.GitConst.SORT_TIME)
@test length(oids) > 0
Pkg.LibGit2.finalize(repo)
Expand Down Expand Up @@ -161,6 +173,9 @@ temp_dir() do dir_cache
# clone repo
path = joinpath(dir, "Example")
repo = Pkg.LibGit2.clone(path_cache, path)
Pkg.LibGit2.with(Pkg.LibGit2.GitConfig, repo) do cfg
credentials!(cfg)
end
cp(joinpath(path, "REQUIRE"), joinpath(path, "CCC"))
cp(joinpath(path, "REQUIRE"), joinpath(path, "AAA"))
Pkg.LibGit2.add!(repo, "AAA")
Expand Down

0 comments on commit c6612f1

Please sign in to comment.