Skip to content

Commit

Permalink
Merge pull request JuliaLang#19473 from JuliaLang/sb/libgit2-kw
Browse files Browse the repository at this point in the history
Add macro to simplify libgit2 type constructor definitions
  • Loading branch information
tkelman authored Dec 30, 2016
2 parents d44977c + fb16580 commit 7ba6ad6
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 411 deletions.
2 changes: 1 addition & 1 deletion base/libgit2/commit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function message(c::GitCommit, raw::Bool=false)
local msg_ptr::Cstring
msg_ptr = raw? ccall((:git_commit_message_raw, :libgit2), Cstring, (Ptr{Void},), c.ptr) :
ccall((:git_commit_message, :libgit2), Cstring, (Ptr{Void},), c.ptr)
if msg_ptr == Cstring_NULL
if msg_ptr == C_NULL
return nothing
end
return unsafe_string(msg_ptr)
Expand Down
10 changes: 5 additions & 5 deletions base/libgit2/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ function Base.count(diff::GitDiff)
return ccall((:git_diff_num_deltas, :libgit2), Cint, (Ptr{Void},), diff.ptr)
end

function Base.getindex(diff::GitDiff, i::Csize_t)
delta_ptr = ccall((:git_diff_get_delta, :libgit2), Ptr{Void},
(Ptr{Void}, Csize_t), diff.ptr, i-1)
function Base.getindex(diff::GitDiff, i::Integer)
delta_ptr = ccall((:git_diff_get_delta, :libgit2),
Ptr{DiffDelta},
(Ptr{Void}, Csize_t), diff.ptr, i-1)
delta_ptr == C_NULL && return nothing
return unsafe_load(convert(Ptr{DiffDelta}, delta_ptr), 1)
return unsafe_load(delta_ptr)
end
Base.getindex(diff::GitDiff, i::Int) = getindex(diff, Csize_t(i))
10 changes: 5 additions & 5 deletions base/libgit2/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ function Base.count(idx::GitIndex)
return ccall((:git_index_entrycount, :libgit2), Csize_t, (Ptr{Void},), idx.ptr)
end

function Base.getindex(idx::GitIndex, i::Csize_t)
ie_ptr = ccall((:git_index_get_byindex, :libgit2), Ptr{Void},
(Ptr{Void}, Csize_t), idx.ptr, i-1)
function Base.getindex(idx::GitIndex, i::Integer)
ie_ptr = ccall((:git_index_get_byindex, :libgit2),
Ptr{IndexEntry},
(Ptr{Void}, Csize_t), idx.ptr, i-1)
ie_ptr == C_NULL && return nothing
return unsafe_load(convert(Ptr{IndexEntry}, ie_ptr), 1)
return unsafe_load(ie_ptr)
end
Base.getindex(idx::GitIndex, i::Int) = getindex(idx, Csize_t(i))

function Base.find(path::String, idx::GitIndex)
pos_ref = Ref{Csize_t}(0)
Expand Down
12 changes: 6 additions & 6 deletions base/libgit2/rebase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ function current(rb::GitRebase)
return ccall((:git_rebase_operation_current, :libgit2), Csize_t, (Ptr{Void},), rb.ptr)
end

function Base.getindex(rb::GitRebase, i::Csize_t)
rb_op_ptr = ccall((:git_rebase_operation_byindex, :libgit2), Ptr{Void},
(Ptr{Void}, Csize_t), rb.ptr, i-1)
function Base.getindex(rb::GitRebase, i::Integer)
rb_op_ptr = ccall((:git_rebase_operation_byindex, :libgit2),
Ptr{RebaseOperation},
(Ptr{Void}, Csize_t), rb.ptr, i-1)
rb_op_ptr == C_NULL && return nothing
return unsafe_load(convert(Ptr{RebaseOperation}, rb_op_ptr), 1)
return unsafe_load(rb_op_ptr)
end
Base.getindex(rb::GitRebase, i::Int) = getindex(rb, Csize_t(i))

function Base.next(rb::GitRebase)
rb_op_ptr_ptr = Ref{Ptr{RebaseOperation}}(C_NULL)
Expand All @@ -38,7 +38,7 @@ function Base.next(rb::GitRebase)
err.code == Error.ITEROVER && return nothing
rethrow(err)
end
return unsafe_load(convert(Ptr{RebaseOperation}, rb_op_ptr_ptr[]), 1)
return unsafe_load(rb_op_ptr_ptr[])
end


Expand Down
6 changes: 3 additions & 3 deletions base/libgit2/reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function GitReference(repo::GitRepo, obj_oid::Oid, refname::AbstractString = Con
@check ccall((:git_reference_create, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Ptr{UInt8}, Ptr{Oid}, Cint, Cstring),
ref_ptr_ptr, repo.ptr, refname, Ref(obj_oid), Cint(force),
isempty(msg) ? Cstring_NULL : msg)
isempty(msg) ? C_NULL : msg)
return GitReference(ref_ptr_ptr[])
end

Expand Down Expand Up @@ -53,7 +53,7 @@ end

function branch(ref::GitReference)
isempty(ref) && return ""
str_ptr_ptr = Ref(LibGit2.Cstring_NULL)
str_ptr_ptr = Ref{Cstring}()
@check ccall((:git_branch_name, :libgit2), Cint,
(Ptr{Cstring}, Ptr{Void},), str_ptr_ptr, ref.ptr)
return unsafe_string(str_ptr_ptr[])
Expand Down Expand Up @@ -179,7 +179,7 @@ function target!(ref::GitReference, new_oid::Oid; msg::AbstractString="")
ref_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
@check ccall((:git_reference_set_target, :libgit2), Cint,
(Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Oid}, Cstring),
ref_ptr_ptr, ref.ptr, Ref(new_oid), isempty(msg) ? Cstring_NULL : msg)
ref_ptr_ptr, ref.ptr, Ref(new_oid), isempty(msg) ? C_NULL : msg)
return GitReference(ref_ptr_ptr[])
end

Expand Down
2 changes: 1 addition & 1 deletion base/libgit2/remote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end

function url(rmt::GitRemote)
url_ptr = ccall((:git_remote_url, :libgit2), Cstring, (Ptr{Void}, ), rmt.ptr)
url_ptr == Cstring_NULL && return ""
url_ptr == C_NULL && return ""
return unsafe_string(url_ptr)
end

Expand Down
15 changes: 7 additions & 8 deletions base/libgit2/status.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ function Base.length(status::GitStatus)
(Ptr{Ptr{Void}}, ), status.ptr))
end

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

function status(repo::GitRepo, path::String)
status_ptr = Ref{Cuint}(0)
Expand Down
Loading

0 comments on commit 7ba6ad6

Please sign in to comment.