Skip to content

Commit

Permalink
avoid defining convert(Vector{String}, ...) in LibGit2 (#56082)
Browse files Browse the repository at this point in the history
This is a weird conversion function to define. Seems cleaner to use the
iteration interface for this. Also avoids some invalidations
(#56080 (comment))

Co-authored-by: KristofferC <[email protected]>
  • Loading branch information
KristofferC and KristofferC authored Oct 10, 2024
1 parent dc609a7 commit a007e80
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function ref_list(repo::GitRepo)
sa_ref = Ref(StrArrayStruct())
@check ccall((:git_reference_list, libgit2), Cint,
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, repo)
res = convert(Vector{String}, sa_ref[])
res = collect(sa_ref[])
free(sa_ref)
res
end
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LibGit2/src/remote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function fetch_refspecs(rmt::GitRemote)
sa_ref = Ref(StrArrayStruct())
@check ccall((:git_remote_get_fetch_refspecs, libgit2), Cint,
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, rmt)
res = convert(Vector{String}, sa_ref[])
res = collect(sa_ref[])
free(sa_ref)
res
end
Expand Down Expand Up @@ -245,7 +245,7 @@ function push_refspecs(rmt::GitRemote)
sa_ref = Ref(StrArrayStruct())
@check ccall((:git_remote_get_push_refspecs, libgit2), Cint,
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, rmt)
res = convert(Vector{String}, sa_ref[])
res = collect(sa_ref[])
free(sa_ref)
res
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/repository.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ function remotes(repo::GitRepo)
@assert repo.ptr != C_NULL
@check ccall((:git_remote_list, libgit2), Cint,
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, repo)
res = convert(Vector{String}, sa_ref[])
res = collect(sa_ref[])
free(sa_ref)
return res
end
Expand Down
7 changes: 4 additions & 3 deletions stdlib/LibGit2/src/strarray.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license


function Base.cconvert(::Type{Ptr{StrArrayStruct}}, x::Vector)
str_ref = Base.cconvert(Ref{Cstring}, x)
sa_ref = Ref(StrArrayStruct(Base.unsafe_convert(Ref{Cstring}, str_ref), length(x)))
Expand All @@ -10,6 +9,8 @@ function Base.unsafe_convert(::Type{Ptr{StrArrayStruct}}, rr::Tuple{Ref{StrArray
Base.unsafe_convert(Ptr{StrArrayStruct}, first(rr))
end

function Base.convert(::Type{Vector{String}}, sa::StrArrayStruct)
[unsafe_string(unsafe_load(sa.strings, i)) for i = 1:sa.count]
Base.length(sa::StrArrayStruct) = sa.count
function Base.iterate(sa::StrArrayStruct, state=1)
state > sa.count && return nothing
(unsafe_string(unsafe_load(sa.strings, state)), state+1)
end
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/tag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function tag_list(repo::GitRepo)
sa_ref = Ref(StrArrayStruct())
@check ccall((:git_tag_list, libgit2), Cint,
(Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, repo)
res = convert(Vector{String}, sa_ref[])
res = collect(sa_ref[])
free(sa_ref)
res
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ When fetching data from LibGit2, a typical usage would look like:
```julia
sa_ref = Ref(StrArrayStruct())
@check ccall(..., (Ptr{StrArrayStruct},), sa_ref)
res = convert(Vector{String}, sa_ref[])
res = collect(sa_ref[])
free(sa_ref)
```
In particular, note that `LibGit2.free` should be called afterward on the `Ref` object.
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/test/libgit2-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end
p = ["XXX","YYY"]
a = Base.cconvert(Ptr{LibGit2.StrArrayStruct}, p)
b = Base.unsafe_convert(Ptr{LibGit2.StrArrayStruct}, a)
@test p == convert(Vector{String}, unsafe_load(b))
@test p == collect(unsafe_load(b))
@noinline gcuse(a) = a
gcuse(a)
end
Expand Down

0 comments on commit a007e80

Please sign in to comment.