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

JuliaLang / julia / #37638

01 Oct 2023 06:36PM UTC coverage: 87.461% (-0.1%) from 87.584%
#37638

push

local

web-flow
Update libc.jl compatability note (#51535)

Update libc.jl compatability note to julia 1.11

73897 of 84491 relevant lines covered (87.46%)

11729457.42 hits per line

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

94.94
/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)
6✔
4
    ensure_initialized()
6✔
5
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
6✔
6
    @check ccall((:git_reference_lookup, libgit2), Cint,
6✔
7
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cstring),
8
                   ref_ptr_ptr, repo.ptr, refname)
9
    return GitReference(repo, ref_ptr_ptr[])
6✔
10
end
11

12
function GitReference(repo::GitRepo, obj_oid::GitHash, refname::AbstractString = Consts.HEAD_FILE;
12✔
13
                      force::Bool=false, msg::AbstractString="")
14
    ensure_initialized()
4✔
15
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
4✔
16
    @check ccall((:git_reference_create, libgit2), Cint,
8✔
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[])
4✔
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)
1✔
30
    ensure_initialized()
3✔
31
    r = @check ccall((:git_repository_head_unborn, libgit2), Cint,
3✔
32
                     (Ptr{Cvoid},), repo.ptr)
33
    r != 0
3✔
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)
55✔
42
    ensure_initialized()
55✔
43
    head_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
55✔
44
    @check ccall((:git_repository_head, libgit2), Cint,
55✔
45
                  (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}), head_ptr_ptr, repo.ptr)
46
    return GitReference(repo, head_ptr_ptr[])
55✔
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)
11✔
68
    isempty(ref) && return ""
13✔
69
    ensure_initialized()
13✔
70
    GC.@preserve ref begin
13✔
71
        name_ptr = ccall((:git_reference_shorthand, libgit2), Cstring, (Ptr{Cvoid},), ref.ptr)
13✔
72
        name_ptr == C_NULL && return ""
13✔
73
        name = unsafe_string(name_ptr)
13✔
74
    end
75
    return name
13✔
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()
35✔
88
    return ccall((:git_reference_type, libgit2), Cint, (Ptr{Cvoid},), ref.ptr)
35✔
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)
2✔
99
    isempty(ref) && return ""
2✔
100
    reftype(ref) == Consts.REF_OID && return ""
2✔
101
    ensure_initialized()
1✔
102
    GC.@preserve ref begin
1✔
103
        rname = ccall((:git_reference_symbolic_target, libgit2), Cstring, (Ptr{Cvoid},), ref.ptr)
1✔
104
        rname == C_NULL && return ""
1✔
105
        name = unsafe_string(rname)
1✔
106
    end
107
    return name
1✔
108
end
109

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

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

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

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

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

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

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

170
function Base.show(io::IO, ref::GitReference)
3✔
171
    println(io, "GitReference:")
3✔
172
    if isremote(ref)
6✔
173
        println(io, "Remote with name ", name(ref))
×
174
    elseif isbranch(ref)
6✔
175
        println(io, "Branch with name ", name(ref))
4✔
176
        if ishead(ref)
4✔
177
            println(io, "Branch is HEAD.")
1✔
178
        else
179
            println(io, "Branch is not HEAD.")
1✔
180
        end
181
    elseif istag(ref)
2✔
182
        println(io, "Tag with name ", name(ref))
1✔
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
35✔
200
    ensure_initialized()
35✔
201
    obj_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
35✔
202
    @check ccall((:git_reference_peel, libgit2), Cint,
35✔
203
                 (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cint), obj_ptr_ptr, ref.ptr, Consts.OBJECT(T))
204
    return T(ref.owner, obj_ptr_ptr[])
35✔
205
end
206
peel(ref::GitReference) = peel(GitObject, ref)
1✔
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)
1✔
214
    ensure_initialized()
1✔
215
    sa_ref = Ref(StrArrayStruct())
1✔
216
    @check ccall((:git_reference_list, libgit2), Cint,
1✔
217
                      (Ptr{StrArrayStruct}, Ptr{Cvoid}), sa_ref, repo.ptr)
218
    res = convert(Vector{String}, sa_ref[])
1✔
219
    free(sa_ref)
1✔
220
    res
1✔
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,
28✔
233
                       bname::AbstractString,
234
                       commit_obj::GitCommit;
235
                       force::Bool=false)
236
    ensure_initialized()
14✔
237
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
14✔
238
    @check ccall((:git_branch_create, libgit2), Cint,
14✔
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[])
14✔
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)
1✔
250
    ensure_initialized()
1✔
251
    @check ccall((:git_branch_delete, libgit2), Cint, (Ptr{Cvoid},), branch.ptr)
1✔
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)
21✔
260
    ensure_initialized()
21✔
261
    ref_name = name(ref)
42✔
262
    @check ccall((:git_repository_set_head, libgit2), Cint,
21✔
263
                  (Ptr{Cvoid}, Cstring), repo.ptr, ref_name)
264
    return ref
21✔
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,
51✔
278
                       branch_name::AbstractString,
279
                       remote::Bool=false)
280
    ensure_initialized()
51✔
281
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
28✔
282
    branch_type = remote ? Consts.BRANCH_REMOTE : Consts.BRANCH_LOCAL
28✔
283
    err = ccall((:git_branch_lookup, libgit2), Cint,
28✔
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)
28✔
287
        if err == Int(Error.ENOTFOUND)
16✔
288
            return nothing
16✔
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[])
12✔
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)
5✔
307
    isempty(ref) && return nothing
5✔
308
    ensure_initialized()
5✔
309
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
5✔
310
    err = ccall((:git_branch_upstream, libgit2), Cint,
5✔
311
                  (Ref{Ptr{Cvoid}}, Ptr{Cvoid},), ref_ptr_ptr, ref.ptr)
312
    if err != Int(Error.GIT_OK)
5✔
313
        if err == Int(Error.ENOTFOUND)
5✔
314
            return nothing
4✔
315
        end
316
        if ref_ptr_ptr[] != C_NULL
1✔
317
            close(GitReference(ref.owner, ref_ptr_ptr[]))
×
318
        end
319
        throw(Error.GitError(err))
1✔
320
    end
321
    return GitReference(ref.owner, ref_ptr_ptr[])
×
322
end
323

324
repository(ref::GitReference) = ref.owner
1✔
325

326
function target!(ref::GitReference, new_oid::GitHash; msg::AbstractString="")
6✔
327
    ensure_initialized()
3✔
328
    ref_ptr_ptr = Ref{Ptr{Cvoid}}(C_NULL)
3✔
329
    @check ccall((:git_reference_set_target, libgit2), Cint,
6✔
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[])
3✔
333
end
334

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

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

359
Base.IteratorSize(::Type{GitBranchIter}) = Base.SizeUnknown()
2✔
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

© 2026 Coveralls, Inc