Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LibGit2 support for config iterator #20752

Merged
merged 2 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions base/libgit2/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,67 @@ function set!(c::GitConfig, name::AbstractString, value::Int64)
@check ccall((:git_config_set_int64, :libgit2), Cint,
(Ptr{Void}, Cstring, Cintmax_t), c.ptr, name, value)
end

function GitConfigIter(cfg::GitConfig)
ci_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_config_iterator_new, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}), ci_ptr, cfg.ptr)
return GitConfigIter(ci_ptr[])
end

function GitConfigIter(cfg::GitConfig, name::AbstractString)
ci_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_config_multivar_iterator_new, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Cstring, Cstring),
ci_ptr, cfg.ptr, name, C_NULL)
return GitConfigIter(ci_ptr[])
end

function GitConfigIter(cfg::GitConfig, name::AbstractString, value::Regex)
ci_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_config_multivar_iterator_new, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Cstring, Cstring),
ci_ptr, cfg.ptr, name, value.pattern)
return GitConfigIter(ci_ptr[])
end

function GitConfigIter(cfg::GitConfig, name::Regex)
ci_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_config_iterator_glob_new, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Cstring),
ci_ptr, cfg.ptr, name.pattern)
return GitConfigIter(ci_ptr[])
end

function Base.start(ci::GitConfigIter)
entry_ptr_ptr = Ref{Ptr{ConfigEntry}}(C_NULL)
err = ccall((:git_config_next, :libgit2), Cint,
(Ptr{Ptr{ConfigEntry}}, Ptr{Void}), entry_ptr_ptr, ci.ptr)
if err == Cint(Error.GIT_OK)
state = Nullable{ConfigEntry}(unsafe_load(entry_ptr_ptr[]))
elseif err == Cint(Error.ITEROVER)
state = Nullable{ConfigEntry}()
else
throw(GitError(err))
end
return state
end

Base.done(ci::GitConfigIter, state) = isnull(state)

function Base.next(ci::GitConfigIter, state)
entry = Base.get(state)
entry_ptr_ptr = Ref{Ptr{ConfigEntry}}(C_NULL)
err = ccall((:git_config_next, :libgit2), Cint,
(Ptr{Ptr{ConfigEntry}}, Ptr{Void}), entry_ptr_ptr, ci.ptr)
if err == Cint(Error.GIT_OK)
state = Nullable{ConfigEntry}(unsafe_load(entry_ptr_ptr[]))
elseif err == Cint(Error.ITEROVER)
state = Nullable{ConfigEntry}()
else
throw(GitError(err))
end
return (entry, state)
end

Base.iteratorsize(::Type{GitConfigIter}) = Base.SizeUnknown()
20 changes: 19 additions & 1 deletion base/libgit2/types.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

import Base.@kwdef
import .Consts: GIT_SUBMODULE_IGNORE, GIT_MERGE_FILE_FAVOR, GIT_MERGE_FILE
import .Consts: GIT_SUBMODULE_IGNORE, GIT_MERGE_FILE_FAVOR, GIT_MERGE_FILE, GIT_CONFIG

const OID_RAWSZ = 20
const OID_HEXSZ = OID_RAWSZ * 2
Expand Down Expand Up @@ -516,6 +516,23 @@ function Base.show(io::IO, fh::FetchHead)
println(io, "Merged: $(fh.ismerge)")
end

"""
LibGit2.ConfigEntry

Matches the [`git_config_entry`](https://libgit2.github.com/libgit2/#HEAD/type/git_config_entry) struct.
"""
@kwdef struct ConfigEntry
name::Cstring
value::Cstring
level::GIT_CONFIG = Consts.CONFIG_LEVEL_DEFAULT
free::Ptr{Void}
payload::Ptr{Void}
end

function Base.show(io::IO, ce::ConfigEntry)
print(io, "ConfigEntry(\"", unsafe_string(ce.name), "\", \"", unsafe_string(ce.value), "\")")
end

# Abstract object types
abstract type AbstractGitObject end
Base.isempty(obj::AbstractGitObject) = (obj.ptr == C_NULL)
Expand All @@ -536,6 +553,7 @@ for (typ, owntyp, sup, cname) in [
(:GitRebase, :GitRepo, :AbstractGitObject, :git_rebase),
(:GitStatus, :GitRepo, :AbstractGitObject, :git_status_list),
(:GitBranchIter, :GitRepo, :AbstractGitObject, :git_branch_iterator),
(:GitConfigIter, nothing, :AbstractGitObject, :git_config_iterator),
(:GitUnknownObject, :GitRepo, :GitObject, :git_object),
(:GitCommit, :GitRepo, :GitObject, :git_commit),
(:GitBlob, :GitRepo, :GitObject, :git_blob),
Expand Down
50 changes: 50 additions & 0 deletions test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,56 @@ mktempdir() do dir
@test LibGit2.get(cfg, "tmp.int32", Int32(0)) == Int32(1)
@test LibGit2.get(cfg, "tmp.int64", Int64(0)) == Int64(1)
@test LibGit2.get(cfg, "tmp.bool", false) == true

# Ordering of entries appears random when using `LibGit2.set!`
count = 0
for entry in LibGit2.GitConfigIter(cfg, r"tmp.*")
count += 1
name, value = unsafe_string(entry.name), unsafe_string(entry.value)
if name == "tmp.str"
@test value == "AAAA"
elseif name == "tmp.int32"
@test value == "1"
elseif name == "tmp.int64"
@test value == "1"
elseif name == "tmp.bool"
@test value == "true"
else
error("Found unexpected entry: $name")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a throw? Or better, can we rewrite it as a test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only situation this error would occur is when additional fields are added to the test configuration without also updating these tests or the expected name is mistyped. I could switch this to use throw but I would just be raising an ErrorException. Do you have any suggestions for a better exception type to use?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error here is appropriate.

end
end
@test count == 4
finally
close(cfg)
end
end

@testset "Configuration Iteration" begin
config_path = joinpath(dir, config_file)

# Write config entries with duplicate names
open(config_path, "a") do fp
write(fp, """
[credential]
\thelper = store
[credential]
\thelper = cache
""")
end

cfg = LibGit2.GitConfig(config_path, LibGit2.Consts.CONFIG_LEVEL_APP)
try
# Will only see the last entry
@test LibGit2.get(cfg, "credential.helper", "") == "cache"

count = 0
for entry in LibGit2.GitConfigIter(cfg, "credential.helper")
count += 1
name, value = unsafe_string(entry.name), unsafe_string(entry.value)
@test name == "credential.helper"
@test value == (count == 1 ? "store" : "cache")
end
@test count == 2
finally
close(cfg)
end
Expand Down