Skip to content

Commit 7ba6ad6

Browse files
authored
Merge pull request #19473 from JuliaLang/sb/libgit2-kw
Add macro to simplify libgit2 type constructor definitions
2 parents d44977c + fb16580 commit 7ba6ad6

File tree

10 files changed

+289
-411
lines changed

10 files changed

+289
-411
lines changed

base/libgit2/commit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function message(c::GitCommit, raw::Bool=false)
44
local msg_ptr::Cstring
55
msg_ptr = raw? ccall((:git_commit_message_raw, :libgit2), Cstring, (Ptr{Void},), c.ptr) :
66
ccall((:git_commit_message, :libgit2), Cstring, (Ptr{Void},), c.ptr)
7-
if msg_ptr == Cstring_NULL
7+
if msg_ptr == C_NULL
88
return nothing
99
end
1010
return unsafe_string(msg_ptr)

base/libgit2/diff.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ function Base.count(diff::GitDiff)
3535
return ccall((:git_diff_num_deltas, :libgit2), Cint, (Ptr{Void},), diff.ptr)
3636
end
3737

38-
function Base.getindex(diff::GitDiff, i::Csize_t)
39-
delta_ptr = ccall((:git_diff_get_delta, :libgit2), Ptr{Void},
40-
(Ptr{Void}, Csize_t), diff.ptr, i-1)
38+
function Base.getindex(diff::GitDiff, i::Integer)
39+
delta_ptr = ccall((:git_diff_get_delta, :libgit2),
40+
Ptr{DiffDelta},
41+
(Ptr{Void}, Csize_t), diff.ptr, i-1)
4142
delta_ptr == C_NULL && return nothing
42-
return unsafe_load(convert(Ptr{DiffDelta}, delta_ptr), 1)
43+
return unsafe_load(delta_ptr)
4344
end
44-
Base.getindex(diff::GitDiff, i::Int) = getindex(diff, Csize_t(i))

base/libgit2/index.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ function Base.count(idx::GitIndex)
111111
return ccall((:git_index_entrycount, :libgit2), Csize_t, (Ptr{Void},), idx.ptr)
112112
end
113113

114-
function Base.getindex(idx::GitIndex, i::Csize_t)
115-
ie_ptr = ccall((:git_index_get_byindex, :libgit2), Ptr{Void},
116-
(Ptr{Void}, Csize_t), idx.ptr, i-1)
114+
function Base.getindex(idx::GitIndex, i::Integer)
115+
ie_ptr = ccall((:git_index_get_byindex, :libgit2),
116+
Ptr{IndexEntry},
117+
(Ptr{Void}, Csize_t), idx.ptr, i-1)
117118
ie_ptr == C_NULL && return nothing
118-
return unsafe_load(convert(Ptr{IndexEntry}, ie_ptr), 1)
119+
return unsafe_load(ie_ptr)
119120
end
120-
Base.getindex(idx::GitIndex, i::Int) = getindex(idx, Csize_t(i))
121121

122122
function Base.find(path::String, idx::GitIndex)
123123
pos_ref = Ref{Csize_t}(0)

base/libgit2/rebase.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ function current(rb::GitRebase)
2020
return ccall((:git_rebase_operation_current, :libgit2), Csize_t, (Ptr{Void},), rb.ptr)
2121
end
2222

23-
function Base.getindex(rb::GitRebase, i::Csize_t)
24-
rb_op_ptr = ccall((:git_rebase_operation_byindex, :libgit2), Ptr{Void},
25-
(Ptr{Void}, Csize_t), rb.ptr, i-1)
23+
function Base.getindex(rb::GitRebase, i::Integer)
24+
rb_op_ptr = ccall((:git_rebase_operation_byindex, :libgit2),
25+
Ptr{RebaseOperation},
26+
(Ptr{Void}, Csize_t), rb.ptr, i-1)
2627
rb_op_ptr == C_NULL && return nothing
27-
return unsafe_load(convert(Ptr{RebaseOperation}, rb_op_ptr), 1)
28+
return unsafe_load(rb_op_ptr)
2829
end
29-
Base.getindex(rb::GitRebase, i::Int) = getindex(rb, Csize_t(i))
3030

3131
function Base.next(rb::GitRebase)
3232
rb_op_ptr_ptr = Ref{Ptr{RebaseOperation}}(C_NULL)
@@ -38,7 +38,7 @@ function Base.next(rb::GitRebase)
3838
err.code == Error.ITEROVER && return nothing
3939
rethrow(err)
4040
end
41-
return unsafe_load(convert(Ptr{RebaseOperation}, rb_op_ptr_ptr[]), 1)
41+
return unsafe_load(rb_op_ptr_ptr[])
4242
end
4343

4444

base/libgit2/reference.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function GitReference(repo::GitRepo, obj_oid::Oid, refname::AbstractString = Con
1414
@check ccall((:git_reference_create, :libgit2), Cint,
1515
(Ptr{Ptr{Void}}, Ptr{Void}, Ptr{UInt8}, Ptr{Oid}, Cint, Cstring),
1616
ref_ptr_ptr, repo.ptr, refname, Ref(obj_oid), Cint(force),
17-
isempty(msg) ? Cstring_NULL : msg)
17+
isempty(msg) ? C_NULL : msg)
1818
return GitReference(ref_ptr_ptr[])
1919
end
2020

@@ -53,7 +53,7 @@ end
5353

5454
function branch(ref::GitReference)
5555
isempty(ref) && return ""
56-
str_ptr_ptr = Ref(LibGit2.Cstring_NULL)
56+
str_ptr_ptr = Ref{Cstring}()
5757
@check ccall((:git_branch_name, :libgit2), Cint,
5858
(Ptr{Cstring}, Ptr{Void},), str_ptr_ptr, ref.ptr)
5959
return unsafe_string(str_ptr_ptr[])
@@ -179,7 +179,7 @@ function target!(ref::GitReference, new_oid::Oid; msg::AbstractString="")
179179
ref_ptr_ptr = Ref{Ptr{Void}}(C_NULL)
180180
@check ccall((:git_reference_set_target, :libgit2), Cint,
181181
(Ptr{Ptr{Void}}, Ptr{Void}, Ptr{Oid}, Cstring),
182-
ref_ptr_ptr, ref.ptr, Ref(new_oid), isempty(msg) ? Cstring_NULL : msg)
182+
ref_ptr_ptr, ref.ptr, Ref(new_oid), isempty(msg) ? C_NULL : msg)
183183
return GitReference(ref_ptr_ptr[])
184184
end
185185

base/libgit2/remote.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434

3535
function url(rmt::GitRemote)
3636
url_ptr = ccall((:git_remote_url, :libgit2), Cstring, (Ptr{Void}, ), rmt.ptr)
37-
url_ptr == Cstring_NULL && return ""
37+
url_ptr == C_NULL && return ""
3838
return unsafe_string(url_ptr)
3939
end
4040

base/libgit2/status.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ function Base.length(status::GitStatus)
1313
(Ptr{Ptr{Void}}, ), status.ptr))
1414
end
1515

16-
function Base.getindex(status::GitStatus, i::Csize_t)
17-
if i < 1 || i > length(status)
18-
throw(BoundsError())
19-
end
20-
entry_ptr = ccall((:git_status_byindex, :libgit2), Ptr{Void},
21-
(Ptr{Void}, Csize_t), status.ptr, i-1)
16+
function Base.getindex(status::GitStatus, i::Integer)
17+
1 <= i <= length(status) || throw(BoundsError())
18+
entry_ptr = ccall((:git_status_byindex, :libgit2),
19+
Ptr{StatusEntry},
20+
(Ptr{Void}, Csize_t),
21+
status.ptr, i-1)
2222
entry_ptr == C_NULL && throw(Error.GitError(Error.ERROR))
23-
return unsafe_load(convert(Ptr{StatusEntry}, entry_ptr), 1)
23+
return unsafe_load(entry_ptr)
2424
end
25-
Base.getindex(status::GitStatus, i::Int) = getindex(status, Csize_t(i))
2625

2726
function status(repo::GitRepo, path::String)
2827
status_ptr = Ref{Cuint}(0)

0 commit comments

Comments
 (0)