Skip to content

Commit

Permalink
Fix getindex in a bunch of libgit2
Browse files Browse the repository at this point in the history
Now actually `throw(BoundsError)` when we should, and don't just
fail silently if something bad happens.
  • Loading branch information
kshyatt committed Jun 8, 2016
1 parent 8b98675 commit 974fd4b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
5 changes: 4 additions & 1 deletion base/libgit2/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ function Base.count(diff::GitDiff)
end

function Base.getindex(diff::GitDiff, i::Csize_t)
if i < 1 || i >= count(diff)
throw(BoundsError())
end
delta_ptr = ccall((:git_diff_get_delta, :libgit2), Ptr{Void},
(Ptr{Void}, Csize_t), diff.ptr, i-1)
delta_ptr == C_NULL && return nothing
delta_ptr == C_NULL && throw(Error.GitError(Error.ERROR))
return unsafe_load(convert(Ptr{DiffDelta}, delta_ptr), 1)
end
Base.getindex(diff::GitDiff, i::Int) = getindex(diff, Csize_t(i))
5 changes: 4 additions & 1 deletion base/libgit2/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ function Base.count(idx::GitIndex)
end

function Base.getindex(idx::GitIndex, i::Csize_t)
if i < 1 || i >= count(idx)
throw(BoundsError())
end
ie_ptr = ccall((:git_index_get_byindex, :libgit2), Ptr{Void},
(Ptr{Void}, Csize_t), idx.ptr, i-1)
ie_ptr == C_NULL && return nothing
ie_ptr == C_NULL && throw(Error.GitError(Error.ERROR))
return unsafe_load(convert(Ptr{IndexEntry}, ie_ptr), 1)
end
Base.getindex(idx::GitIndex, i::Int) = getindex(idx, Csize_t(i))
5 changes: 4 additions & 1 deletion base/libgit2/rebase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ function current(rb::GitRebase)
end

function Base.getindex(rb::GitRebase, i::Csize_t)
if i < 1 || i >= count(rb)
throw(BoundsError())
end
rb_op_ptr = ccall((:git_rebase_operation_byindex, :libgit2), Ptr{Void},
(Ptr{Void}, Csize_t), rb.ptr, i-1)
rb_op_ptr == C_NULL && return nothing
rb_op_ptr == C_NULL && throw(Error.GitError(Error.ERROR))
return unsafe_load(convert(Ptr{RebaseOperation}, rb_op_ptr), 1)
end
Base.getindex(rb::GitRebase, i::Int) = getindex(rb, Csize_t(i))
Expand Down
4 changes: 2 additions & 2 deletions base/libgit2/tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ end

function filename(te::GitTreeEntry)
str = ccall((:git_tree_entry_name, :libgit2), Cstring, (Ptr{Void},), te.ptr)
str != C_NULL && return unsafe_string(str)
return nothing
str == C_NULL && throw(Error.GitError(Error.ERROR))
return unsafe_string(str)
end

function filemode(te::GitTreeEntry)
Expand Down
4 changes: 2 additions & 2 deletions base/libgit2/walker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ end

function repository(w::GitRevWalker)
ptr = ccall((:git_revwalk_repository, :libgit2), Ptr{Void}, (Ptr{Void},), w.ptr)
ptr != C_NULL && return GitRepo(ptr)
return nothing
ptr == C_NULL && throw(Error.GitError(Error.ERROR))
return GitRepo(ptr)
end

function Base.map(f::Function, walker::GitRevWalker;
Expand Down

0 comments on commit 974fd4b

Please sign in to comment.