Skip to content

Commit

Permalink
Improve URL regex
Browse files Browse the repository at this point in the history
(cherry picked from commit d2bbdd2)
ref #18066
  • Loading branch information
omus authored and tkelman committed Aug 20, 2016
1 parent edcd7f9 commit 95acc0f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
10 changes: 4 additions & 6 deletions base/libgit2/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,10 @@ function credentials_callback(libgit2credptr::Ptr{Ptr{Void}}, url_ptr::Cstring,
url = unsafe_string(url_ptr)

# parse url for schema and host
urlparts = match(urlmatcher, url)
schema = urlparts.captures[1]
urlusername = urlparts.captures[4]
urlusername = urlusername === nothing ? "" : String(urlusername)
host = urlparts.captures[5]
schema = schema === nothing ? "" : schema*"://"
urlparts = match(URL_REGEX, url)
schema = urlparts[:scheme] === nothing ? "" : urlparts[:scheme] * "://"
urlusername = urlparts[:user] === nothing ? "" : urlparts[:user]
host = urlparts[:host]

# get credentials object from payload pointer
@assert payload_ptr != C_NULL
Expand Down
8 changes: 7 additions & 1 deletion base/libgit2/utils.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

const urlmatcher = r"^(http[s]?|git|ssh)?(:\/\/)?((\w+)@)?([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$"
const URL_REGEX = r"""
^(?:(?<scheme>https?|git|ssh)\:\/\/)?
(?:(?<user>.*?)(?:\:(?<password>.*?))?@)?
(?<host>[A-Za-z0-9\-\.]+)
(?:\:(?<port>\d+)?)?
(?<path>.*?)$
"""x

function version()
major = Ref{Cint}(0)
Expand Down
33 changes: 33 additions & 0 deletions test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,39 @@ const LIBGIT2_MIN_VER = v"0.23.0"
@test sig3.email == sig.email
#end

#@testset "URL parsing" begin
# Use all named group
m = match(LibGit2.URL_REGEX, "https://user:pass@hostname.com:80/path/repo.git")
@test m[:scheme] == "https"
@test m[:user] == "user"
@test m[:password] == "pass"
@test m[:host] == "hostname.com"
@test m[:port] == "80"
@test m[:path] == "/path/repo.git"

# Realistic example HTTP example
m = match(LibGit2.URL_REGEX, "https://github.com/JuliaLang/Example.jl.git")
@test m[:scheme] == "https"
@test m[:user] == nothing
@test m[:password] == nothing
@test m[:host] == "github.com"
@test m[:port] == nothing
@test m[:path] == "/JuliaLang/Example.jl.git"

# Realistic example SSH example
m = match(LibGit2.URL_REGEX, "git@github.com:JuliaLang/Example.jl.git")
@test m[:scheme] == nothing
@test m[:user] == "git"
@test m[:password] == nothing
@test m[:host] == "github.com"
@test m[:port] == nothing
@test m[:path] == "JuliaLang/Example.jl.git"

# Make sure that a username can contain special characters
m = match(LibGit2.URL_REGEX, "user-name@hostname.com")
@test m[:user] == "user-name"
#end

mktempdir() do dir
# test parameters
repo_url = "https://github.com/JuliaLang/Example.jl"
Expand Down

0 comments on commit 95acc0f

Please sign in to comment.