From c6612f14989a454ba7ed1260f287d66051ade401 Mon Sep 17 00:00:00 2001 From: Art Wild Date: Sun, 17 May 2015 23:58:01 -0400 Subject: [PATCH] fixed `get` with default for GitConfig fixed `libgit2` tests --- base/pkg/libgit2.jl | 2 +- base/pkg/libgit2/config.jl | 4 +++- base/pkg/libgit2/repository.jl | 19 +++++++++---------- test/libgit2.jl | 27 +++++++++++++++++++++------ 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/base/pkg/libgit2.jl b/base/pkg/libgit2.jl index 549317f59284a..79f290ab4e6a4 100644 --- a/base/pkg/libgit2.jl +++ b/base/pkg/libgit2.jl @@ -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 diff --git a/base/pkg/libgit2/config.jl b/base/pkg/libgit2/config.jl index 6fb84ffaa5804..a724e679e86ce 100644 --- a/base/pkg/libgit2/config.jl +++ b/base/pkg/libgit2/config.jl @@ -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) diff --git a/base/pkg/libgit2/repository.jl b/base/pkg/libgit2/repository.jl index 9ca2e815f4650..431ae30349745 100644 --- a/base/pkg/libgit2/repository.jl +++ b/base/pkg/libgit2/repository.jl @@ -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...) @@ -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 \ No newline at end of file diff --git a/test/libgit2.jl b/test/libgit2.jl index 73f35aa0f2433..5e8c38e1a85c7 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -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] @@ -16,12 +16,16 @@ end @test check_version() +function credentials!(cfg::Pkg.LibGit2.GitConfig, usr="Test User", usr_email="Test@User.com") + 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", "Test@User.com") + credentials!(cfg) end function temp_dir(fn::Function, remove_tmp_dir::Bool=true) @@ -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) @@ -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`))) @@ -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) @@ -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")