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

JuliaLang / julia / #37889

31 Aug 2024 02:18AM UTC coverage: 86.287% (-1.6%) from 87.882%
#37889

push

local

web-flow
Fix an assertion in new binding code (#55636)

This snuck by me late in the new bindings PR and I didn't notice the
assert builder handn't finished yet.

76579 of 88749 relevant lines covered (86.29%)

16183774.44 hits per line

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

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

3
# Parse "GIT URLs" syntax (URLs and a scp-like syntax). For details see:
4
# https://git-scm.com/docs/git-clone#_git_urls_a_id_urls_a
5
# Note that using a Regex like this is inherently insecure with regards to its
6
# handling of passwords; we are unable to deterministically and securely erase
7
# the passwords from memory after use.
8
# TODO: reimplement with a Julian parser instead of leaning on this regex
9
const URL_REGEX = r"""
10
^(?:(?<scheme>ssh|git|https?)://)?+
11
(?:
12
    (?<user>.*?)
13
    (?:\:(?<password>.*?))?@
14
)?
15
(?<host>[A-Za-z0-9\-\.]+)
16
(?(<scheme>)
17
    # Only parse port when not using scp-like syntax
18
    (?:\:(?<port>\d+))?
19
    /?
20
    |
21
    :?
22
)
23
(?<path>
24
    # Require path to be preceded by '/'. Alternatively, ':' when using scp-like syntax.
25
    (?<=(?(<scheme>)/|:))
26
    .*
27
)?
28
$
29
"""x
30

31
"""
32
    version() -> VersionNumber
33

34
Return the version of libgit2 in use, as a [`VersionNumber`](@ref man-version-number-literals).
35
"""
36
function version()
×
37
    major = Ref{Cint}(0)
×
38
    minor = Ref{Cint}(0)
×
39
    patch = Ref{Cint}(0)
×
40
    @check ccall((:git_libgit2_version, libgit2), Cint,
×
41
                 (Ref{Cint}, Ref{Cint}, Ref{Cint}), major, minor, patch)
42
    return VersionNumber(major[], minor[], patch[])
×
43
end
44
const VERSION = version()
45

46
"""
47
    isset(val::Integer, flag::Integer)
48

49
Test whether the bits of `val` indexed by `flag` are set (`1`) or unset (`0`).
50
"""
51
isset(val::Integer, flag::Integer) = (val & flag == flag)
6✔
52

53
"""
54
    reset(val::Integer, flag::Integer)
55

56
Unset the bits of `val` indexed by `flag`, returning them to `0`.
57
"""
58
reset(val::Integer, flag::Integer) = (val &= ~flag)
×
59

60
"""
61
    toggle(val::Integer, flag::Integer)
62

63
Flip the bits of `val` indexed by `flag`, so that if a bit is `0` it
64
will be `1` after the toggle, and vice-versa.
65
"""
66
toggle(val::Integer, flag::Integer) = (val |= flag)
×
67

68
"""
69
    features()
70

71
Return a list of git features the current version of libgit2 supports, such as
72
threading or using HTTPS or SSH.
73
"""
74
function features()
×
75
    feat = ccall((:git_libgit2_features, libgit2), Cint, ())
×
76
    res = Consts.GIT_FEATURE[]
×
77
    for f in instances(Consts.GIT_FEATURE)
×
78
        isset(feat, Cuint(f)) && Base.push!(res, f)
×
79
    end
×
80
    return res
×
81
end
82

83
"""
84
    LibGit2.posixpath(path)
85

86
Standardise the path string `path` to use POSIX separators.
87
"""
88
function posixpath end
89
if Sys.iswindows()
90
    posixpath(path) = replace(path,'\\' => '/')
×
91
elseif Sys.isunix()
92
    posixpath(path) = path
×
93
end
94

95
"""
96
    LibGit2.git_url(; kwargs...) -> String
97

98
Create a string based upon the URL components provided. When the `scheme` keyword is not
99
provided the URL produced will use the alternative [scp-like syntax](https://git-scm.com/docs/git-clone#_git_urls_a_id_urls_a).
100

101
# Keywords
102

103
  * `scheme::AbstractString=""`: the URL scheme which identifies the protocol to be used.
104
    For HTTP use "http", SSH use "ssh", etc. When `scheme` is not provided the output format
105
    will be "ssh" but using the scp-like syntax.
106
  * `username::AbstractString=""`: the username to use in the output if provided.
107
  * `password::AbstractString=""`: the password to use in the output if provided.
108
  * `host::AbstractString=""`: the hostname to use in the output. A hostname is required to
109
    be specified.
110
  * `port::Union{AbstractString,Integer}=""`: the port number to use in the output if
111
    provided. Cannot be specified when using the scp-like syntax.
112
  * `path::AbstractString=""`: the path to use in the output if provided.
113

114
!!! warning
115
    Avoid using passwords in URLs. Unlike the credential objects, Julia is not able
116
    to securely zero or destroy the sensitive data after use and the password may
117
    remain in memory; possibly to be exposed by an uninitialized memory.
118

119
# Examples
120
```jldoctest
121
julia> LibGit2.git_url(username="git", host="github.com", path="JuliaLang/julia.git")
122
"git@github.com:JuliaLang/julia.git"
123

124
julia> LibGit2.git_url(scheme="https", host="github.com", path="/JuliaLang/julia.git")
125
"https://github.com/JuliaLang/julia.git"
126

127
julia> LibGit2.git_url(scheme="ssh", username="git", host="github.com", port=2222, path="JuliaLang/julia.git")
128
"ssh://git@github.com:2222/JuliaLang/julia.git"
129
```
130
"""
131
function git_url(;
×
132
        scheme::AbstractString="",
133
        username::AbstractString="",
134
        host::AbstractString="",
135
        port::Union{AbstractString, Integer}="",
136
        path::AbstractString="")
137

138
    port_str = port isa Integer ? string(port) : port
×
139
    scp_syntax = isempty(scheme)
×
140

141
    isempty(host) && throw(ArgumentError("A host needs to be specified"))
×
142
    scp_syntax && !isempty(port_str) && throw(ArgumentError("Port cannot be specified when using scp-like syntax"))
×
143

144
    io = IOBuffer()
×
145
    !isempty(scheme) && write(io, scheme, "://")
×
146

147
    if !isempty(username)
×
148
        write(io, username)
×
149
        write(io, '@')
×
150
    end
151

152
    write(io, host)
×
153
    !isempty(port_str) && write(io, ':', port_str)
×
154

155
    if !isempty(path)
×
156
        if scp_syntax
×
157
            write(io, ':')
×
158
        elseif !startswith(path, '/')
×
159
            write(io, '/')
×
160
        end
161
        write(io, path)
×
162
    end
163
    seekstart(io)
×
164

165
    return String(take!(io))
×
166
end
167

168
function credential_identifier(scheme::AbstractString, host::AbstractString)
169
    string(isempty(scheme) ? "ssh" : scheme, "://", host)
×
170
end
171

172
function credential_identifier(url::AbstractString)
×
173
    m = match(URL_REGEX, url)
×
174
    scheme = something(m[:scheme], SubString(""))
×
175
    host = something(m[:host])
×
176
    credential_identifier(scheme, host)
×
177
end
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