• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37551

pending completion
#37551

push

local

web-flow
Improve error when indexing is interpreted as a typed comprehension (#49939)

* Improve errors for typed_hcat by adding a special error for indexing that gets resolved as a typed comprehension.

* Add a test for issue #49676

1 of 1 new or added line in 1 file covered. (100.0%)

71890 of 83662 relevant lines covered (85.93%)

34589844.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.43
/stdlib/LibGit2/src/reference.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
function GitReference(repo::GitRepo, refname::AbstractString)
×
4
    ensure_initialized()
×
5
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
6
    @check ccall((:git_reference_lookup, :libgit2), Cint,
×
7
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cstring),
8
                   ref_ptr_ptr, repo.ptr, refname)
9
    return GitReference(repo, ref_ptr_ptr[])
×
10
end
11

12
function GitReference(repo::GitRepo, obj_oid::GitHash, refname::AbstractString = Consts.HEAD_FILE;
×
13
                      force::Bool=false, msg::AbstractString="")
14
    ensure_initialized()
×
15
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
16
    @check ccall((:git_reference_create, :libgit2), Cint,
×
17
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{UInt8}, Ptr{GitHash}, Cint, Cstring),
18
                   ref_ptr_ptr, repo.ptr, refname, Ref(obj_oid), Cint(force),
19
                   isempty(msg) ? C_NULL : msg)
20
    return GitReference(repo, ref_ptr_ptr[])
×
21
end
22

23
"""
24
    LibGit2.isorphan(repo::GitRepo)
25

26
Check if the current branch is an "orphan" branch, i.e. has no commits. The first commit
27
to this branch will have no parents.
28
"""
29
function isorphan(repo::GitRepo)
×
30
    ensure_initialized()
×
31
    r = @check ccall((:git_repository_head_unborn, :libgit2), Cint,
×
32
                     (Ptr{Cvoid},), repo.ptr)
33
    r != 0
×
34
end
35

36
"""
37
    LibGit2.head(repo::GitRepo) -> GitReference
38

39
Return a `GitReference` to the current HEAD of `repo`.
40
"""
41
function head(repo::GitRepo)
19✔
42
    ensure_initialized()
19✔
43
    head_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
19✔
44
    @check ccall((:git_repository_head, :libgit2), Cint,
19✔
45
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}), head_ptr_ptr, repo.ptr)
46
    return GitReference(repo, head_ptr_ptr[])
19✔
47
end
48

49
"""
50
    LibGit2.shortname(ref::GitReference)
51

52
Return a shortened version of the name of `ref` that's
53
"human-readable".
54

55
```julia-repl
56
julia> repo = GitRepo(path_to_repo);
57

58
julia> branch_ref = LibGit2.head(repo);
59

60
julia> LibGit2.name(branch_ref)
61
"refs/heads/master"
62

63
julia> LibGit2.shortname(branch_ref)
64
"master"
65
```
66
"""
67
function shortname(ref::GitReference)
×
68
    isempty(ref) && return ""
×
69
    ensure_initialized()
×
70
    GC.@preserve ref begin
×
71
        name_ptr = ccall((:git_reference_shorthand, :libgit2), Cstring, (Ptr{Cvoid},), ref.ptr)
×
72
        name_ptr == C_NULL && return ""
×
73
        name = unsafe_string(name_ptr)
×
74
    end
75
    return name
×
76
end
77

78
"""
79
    LibGit2.reftype(ref::GitReference) -> Cint
80

81
Return a `Cint` corresponding to the type of `ref`:
82
  * `0` if the reference is invalid
83
  * `1` if the reference is an object id
84
  * `2` if the reference is symbolic
85
"""
86
function reftype(ref::GitReference)
×
87
    ensure_initialized()
19✔
88
    return ccall((:git_reference_type, :libgit2), Cint, (Ptr{Cvoid},), ref.ptr)
19✔
89
end
90

91
"""
92
    LibGit2.fullname(ref::GitReference)
93

94
Return the name of the reference pointed to by the
95
symbolic reference `ref`. If `ref` is not a symbolic
96
reference, return an empty string.
97
"""
98
function fullname(ref::GitReference)
×
99
    isempty(ref) && return ""
×
100
    reftype(ref) == Consts.REF_OID && return ""
×
101
    ensure_initialized()
×
102
    GC.@preserve ref begin
×
103
        rname = ccall((:git_reference_symbolic_target, :libgit2), Cstring, (Ptr{Cvoid},), ref.ptr)
×
104
        rname == C_NULL && return ""
×
105
        name = unsafe_string(rname)
×
106
    end
107
    return name
×
108
end
109

110
"""
111
    LibGit2.name(ref::GitReference)
112

113
Return the full name of `ref`.
114
"""
115
function name(ref::GitReference)
×
116
    isempty(ref) && return ""
×
117
    ensure_initialized()
×
118
    GC.@preserve ref begin
×
119
        name_ptr = ccall((:git_reference_name, :libgit2), Cstring, (Ptr{Cvoid},), ref.ptr)
×
120
        name_ptr == C_NULL && return ""
×
121
        name = unsafe_string(name_ptr)
×
122
    end
123
    return name
×
124
end
125

126
function branch(ref::GitReference)
×
127
    isempty(ref) && return ""
×
128
    ensure_initialized()
×
129
    str_ptr_ptr = Ref{Cstring}()
×
130
    GC.@preserve ref begin
×
131
        @check ccall((:git_branch_name, :libgit2), Cint,
×
132
                      (Ptr{Cstring}, Ptr{Cvoid},), str_ptr_ptr, ref.ptr)
133
        str = unsafe_string(str_ptr_ptr[])
×
134
    end
135
    return str
×
136
end
137

138
function ishead(ref::GitReference)
×
139
    isempty(ref) && return false
×
140
    ensure_initialized()
×
141
    err = ccall((:git_branch_is_head, :libgit2), Cint,
×
142
                  (Ptr{Cvoid},), ref.ptr)
143
    return err == 1
×
144
end
145

146
function isbranch(ref::GitReference)
×
147
    isempty(ref) && return false
×
148
    ensure_initialized()
×
149
    err = ccall((:git_reference_is_branch, :libgit2), Cint,
×
150
                  (Ptr{Cvoid},), ref.ptr)
151
    return err == 1
×
152
end
153

154
function istag(ref::GitReference)
×
155
    isempty(ref) && return false
×
156
    ensure_initialized()
×
157
    err = ccall((:git_reference_is_tag, :libgit2), Cint,
×
158
                  (Ptr{Cvoid},), ref.ptr)
159
    return err == 1
×
160
end
161

162
function isremote(ref::GitReference)
×
163
    isempty(ref) && return false
×
164
    ensure_initialized()
×
165
    err = ccall((:git_reference_is_remote, :libgit2), Cint,
×
166
                  (Ptr{Cvoid},), ref.ptr)
167
    return err == 1
×
168
end
169

170
function Base.show(io::IO, ref::GitReference)
×
171
    println(io, "GitReference:")
×
172
    if isremote(ref)
×
173
        println(io, "Remote with name ", name(ref))
×
174
    elseif isbranch(ref)
×
175
        println(io, "Branch with name ", name(ref))
×
176
        if ishead(ref)
×
177
            println(io, "Branch is HEAD.")
×
178
        else
179
            println(io, "Branch is not HEAD.")
×
180
        end
181
    elseif istag(ref)
×
182
        println(io, "Tag with name ", name(ref))
×
183
    end
184
end
185

186
"""
187
    peel([T,] ref::GitReference)
188

189
Recursively peel `ref` until an object of type `T` is obtained. If no `T` is provided,
190
then `ref` will be peeled until an object other than a [`GitTag`](@ref) is obtained.
191

192
- A `GitTag` will be peeled to the object it references.
193
- A [`GitCommit`](@ref) will be peeled to a [`GitTree`](@ref).
194

195
!!! note
196
    Only annotated tags can be peeled to `GitTag` objects. Lightweight tags (the default)
197
    are references under `refs/tags/` which point directly to `GitCommit` objects.
198
"""
199
function peel(::Type{T}, ref::GitReference) where T<:GitObject
×
200
    ensure_initialized()
×
201
    obj_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
202
    @check ccall((:git_reference_peel, :libgit2), Cint,
×
203
                 (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cint), obj_ptr_ptr, ref.ptr, Consts.OBJECT(T))
204
    return T(ref.owner, obj_ptr_ptr[])
×
205
end
206
peel(ref::GitReference) = peel(GitObject, ref)
×
207

208
"""
209
    LibGit2.ref_list(repo::GitRepo) -> Vector{String}
210

211
Get a list of all reference names in the `repo` repository.
212
"""
213
function ref_list(repo::GitRepo)
×
214
    ensure_initialized()
×
215
    sa_ref = Ref(StrArrayStruct())
×
216
    @check ccall((:git_reference_list, :libgit2), Cint,
×
217
                      (Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, repo.ptr)
218
    res = convert(Vector{String}, sa_ref[])
×
219
    free(sa_ref)
×
220
    res
×
221
end
222

223
"""
224
    LibGit2.create_branch(repo::GitRepo, bname::AbstractString, commit_obj::GitCommit; force::Bool=false)
225

226
Create a new branch in the repository `repo` with name `bname`, which
227
points to commit `commit_obj` (which has to be part of `repo`). If
228
`force` is `true`, overwrite an existing branch named `bname` if it
229
exists. If `force` is `false` and a branch already exists named `bname`,
230
this function will throw an error.
231
"""
232
function create_branch(repo::GitRepo,
×
233
                       bname::AbstractString,
234
                       commit_obj::GitCommit;
235
                       force::Bool=false)
236
    ensure_initialized()
×
237
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
238
    @check ccall((:git_branch_create, :libgit2), Cint,
×
239
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cstring, Ptr{Cvoid}, Cint),
240
                   ref_ptr_ptr, repo.ptr, bname, commit_obj.ptr, Cint(force))
241
    return GitReference(repo, ref_ptr_ptr[])
×
242
end
243

244
"""
245
    LibGit2.delete_branch(branch::GitReference)
246

247
Delete the branch pointed to by `branch`.
248
"""
249
function delete_branch(branch::GitReference)
×
250
    ensure_initialized()
×
251
    @check ccall((:git_branch_delete, :libgit2), Cint, (Ptr{Cvoid},), branch.ptr)
×
252
end
253

254
"""
255
    LibGit2.head!(repo::GitRepo, ref::GitReference) -> GitReference
256

257
Set the HEAD of `repo` to the object pointed to by `ref`.
258
"""
259
function head!(repo::GitRepo, ref::GitReference)
×
260
    ensure_initialized()
×
261
    ref_name = name(ref)
×
262
    @check ccall((:git_repository_set_head, :libgit2), Cint,
×
263
                  (Ptr{Cvoid}, Cstring), repo.ptr, ref_name)
264
    return ref
×
265
end
266

267
"""
268
    lookup_branch(repo::GitRepo, branch_name::AbstractString, remote::Bool=false) -> Union{GitReference, Nothing}
269

270
Determine if the branch specified by `branch_name` exists in the repository `repo`.
271
If `remote` is `true`, `repo` is assumed to be a remote git repository. Otherwise, it
272
is part of the local filesystem.
273

274
Return either a `GitReference` to the requested branch
275
if it exists, or [`nothing`](@ref) if not.
276
"""
277
function lookup_branch(repo::GitRepo,
×
278
                       branch_name::AbstractString,
279
                       remote::Bool=false)
280
    ensure_initialized()
×
281
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
282
    branch_type = remote ? Consts.BRANCH_REMOTE : Consts.BRANCH_LOCAL
×
283
    err = ccall((:git_branch_lookup, :libgit2), Cint,
×
284
                 (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{UInt8}, Cint),
285
                  ref_ptr_ptr, repo.ptr, branch_name, branch_type)
286
    if err != Int(Error.GIT_OK)
×
287
        if err == Int(Error.ENOTFOUND)
×
288
            return nothing
×
289
        end
290
        if ref_ptr_ptr[] != C_NULL
×
291
            close(GitReference(repo, ref_ptr_ptr[]))
×
292
        end
293
        throw(Error.GitError(err))
×
294
    end
295
    return GitReference(repo, ref_ptr_ptr[])
×
296
end
297

298
"""
299
    upstream(ref::GitReference) -> Union{GitReference, Nothing}
300

301
Determine if the branch containing `ref` has a specified upstream branch.
302

303
Return either a `GitReference` to the upstream branch if it exists,
304
or [`nothing`](@ref) if the requested branch does not have an upstream counterpart.
305
"""
306
function upstream(ref::GitReference)
×
307
    isempty(ref) && return nothing
×
308
    ensure_initialized()
×
309
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
310
    err = ccall((:git_branch_upstream, :libgit2), Cint,
×
311
                  (Ref{Ptr{Cvoid}}, Ptr{Cvoid},), ref_ptr_ptr, ref.ptr)
312
    if err != Int(Error.GIT_OK)
×
313
        if err == Int(Error.ENOTFOUND)
×
314
            return nothing
×
315
        end
316
        if ref_ptr_ptr[] != C_NULL
×
317
            close(GitReference(ref.owner, ref_ptr_ptr[]))
×
318
        end
319
        throw(Error.GitError(err))
×
320
    end
321
    return GitReference(ref.owner, ref_ptr_ptr[])
×
322
end
323

324
repository(ref::GitReference) = ref.owner
×
325

326
function target!(ref::GitReference, new_oid::GitHash; msg::AbstractString="")
×
327
    ensure_initialized()
×
328
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
329
    @check ccall((:git_reference_set_target, :libgit2), Cint,
×
330
             (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Ptr{GitHash}, Cstring),
331
             ref_ptr_ptr, ref.ptr, Ref(new_oid), isempty(msg) ? C_NULL : msg)
332
    return GitReference(ref.owner, ref_ptr_ptr[])
×
333
end
334

335
function GitBranchIter(repo::GitRepo, flags::Cint=Cint(Consts.BRANCH_LOCAL))
×
336
    ensure_initialized()
×
337
    bi_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
338
    @check ccall((:git_branch_iterator_new, :libgit2), Cint,
×
339
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cint), bi_ptr, repo.ptr, flags)
340
    return GitBranchIter(repo, bi_ptr[])
×
341
end
342

343
function Base.iterate(bi::GitBranchIter, state=nothing)
×
344
    ensure_initialized()
×
345
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
×
346
    btype = Ref{Cint}()
×
347
    err = ccall((:git_branch_next, :libgit2), Cint,
×
348
                 (Ptr{Ptr{Cvoid}}, Ptr{Cint}, Ptr{Cvoid}),
349
                  ref_ptr_ptr, btype, bi.ptr)
350
    if err == Cint(Error.GIT_OK)
×
351
        return ((GitReference(bi.owner, ref_ptr_ptr[]), btype[]), nothing)
×
352
    elseif err == Cint(Error.ITEROVER)
×
353
        return nothing
×
354
    else
355
        throw(GitError(err))
×
356
    end
357
end
358

359
Base.IteratorSize(::Type{GitBranchIter}) = Base.SizeUnknown()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc