From 2192d192a7b5fb180821e107dff755d8053222a9 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 16 Aug 2016 11:23:27 -0500 Subject: [PATCH 1/5] Fix bug with operator precendence --- base/libgit2/callbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/libgit2/callbacks.jl b/base/libgit2/callbacks.jl index fd2580da27ee7..cbab587516e82 100644 --- a/base/libgit2/callbacks.jl +++ b/base/libgit2/callbacks.jl @@ -160,7 +160,7 @@ function authenticate_userpass(creds::UserPasswordCredentials, libgit2credptr::P urlusername : username) userpass = prompt("Password for '$schema$username@$host'", password=true) end - (creds.user != username) || (creds.pass != userpass) && reset!(creds) + ((creds.user != username) || (creds.pass != userpass)) && reset!(creds) creds.user = username # save credentials creds.pass = userpass # save credentials From d2bbdd2cfcee768864c4b8149230a3f9b537846a Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 16 Aug 2016 14:34:46 -0500 Subject: [PATCH 2/5] Improve URL regex --- base/libgit2/callbacks.jl | 10 ++++------ base/libgit2/utils.jl | 8 +++++++- test/libgit2.jl | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/base/libgit2/callbacks.jl b/base/libgit2/callbacks.jl index cbab587516e82..13f2f88e168de 100644 --- a/base/libgit2/callbacks.jl +++ b/base/libgit2/callbacks.jl @@ -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 diff --git a/base/libgit2/utils.jl b/base/libgit2/utils.jl index ba2149dbda576..ab8928687e3c7 100644 --- a/base/libgit2/utils.jl +++ b/base/libgit2/utils.jl @@ -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""" +^(?:(?https?|git|ssh)\:\/\/)? +(?:(?.*?)(?:\:(?.*?))?@)? +(?[A-Za-z0-9\-\.]+) +(?:\:(?\d+)?)? +(?.*?)$ +"""x function version() major = Ref{Cint}(0) diff --git a/test/libgit2.jl b/test/libgit2.jl index a0466c21c7e15..96a183c3d6e27 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -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" From 749d099e13f44d516c0916e9c9d02c917f96edc9 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Tue, 16 Aug 2016 15:13:07 -0500 Subject: [PATCH 3/5] Correct invalid SSH URL An SSH URL with a path uses a colon between the host and the path: git@github.com:JuliaLang/Example.jl (correct) git@github.com/JuliaLang/Example.jl (incorrect) --- test/libgit2-online.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/libgit2-online.jl b/test/libgit2-online.jl index 436003eb37160..61b683f087138 100644 --- a/test/libgit2-online.jl +++ b/test/libgit2-online.jl @@ -7,13 +7,12 @@ ######### # init & clone mktempdir() do dir - repo_url = "github.com/JuliaLang/Example.jl" - https_prefix = "https://" - ssh_prefix = "git@" + repo_url_https = "https://github.com/JuliaLang/Example.jl" + repo_url_git = "git@github.com:JuliaLang/Example.jl" #@testset "Cloning repository" begin #@testset "with 'https' protocol" begin repo_path = joinpath(dir, "Example1") - repo = LibGit2.clone(https_prefix*repo_url, repo_path) + repo = LibGit2.clone(repo_url_https, repo_path) try @test isdir(repo_path) @test isdir(joinpath(repo_path, ".git")) @@ -27,7 +26,7 @@ mktempdir() do dir repo_path = joinpath(dir, "Example2") # credentials are required because github tries to authenticate on unknown repo cred = LibGit2.UserPasswordCredentials("","") # empty credentials cause authentication error - LibGit2.clone(https_prefix*repo_url*randstring(10), repo_path, payload=Nullable(cred)) + LibGit2.clone(repo_url_https*randstring(10), repo_path, payload=Nullable(cred)) error("unexpected") catch ex @test isa(ex, LibGit2.Error.GitError) @@ -37,12 +36,13 @@ mktempdir() do dir #TODO: remove or condition on libgit2 features this test when ssh protocol will be supported #@testset "with 'ssh' protocol (by default is not supported)" begin + repo_path = joinpath(dir, "Example3") + repo = LibGit2.clone(repo_url_git, repo_path) try - repo_path = joinpath(dir, "Example3") - @test_throws LibGit2.Error.GitError LibGit2.clone(ssh_prefix*repo_url, repo_path) - catch ex - # but we cloned succesfully, so check that repo was created - ex.fail == 1 && @test isdir(joinpath(path, ".git")) + @test isdir(repo_path) + @test isdir(joinpath(repo_path, ".git")) + finally + finalize(repo) end #end #end From 1e98a5ab2a230caac320f69b1d801bccd95c344f Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 17 Aug 2016 13:24:57 -0500 Subject: [PATCH 4/5] Removed SSH test from libgit2-online Test was only working for environments which had GitHub SSH keys setup. --- test/libgit2-online.jl | 19 +++---------------- test/libgit2.jl | 1 - 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/test/libgit2-online.jl b/test/libgit2-online.jl index 61b683f087138..dd02d6c42d37f 100644 --- a/test/libgit2-online.jl +++ b/test/libgit2-online.jl @@ -7,12 +7,11 @@ ######### # init & clone mktempdir() do dir - repo_url_https = "https://github.com/JuliaLang/Example.jl" - repo_url_git = "git@github.com:JuliaLang/Example.jl" + repo_url = "https://github.com/JuliaLang/Example.jl" #@testset "Cloning repository" begin #@testset "with 'https' protocol" begin repo_path = joinpath(dir, "Example1") - repo = LibGit2.clone(repo_url_https, repo_path) + repo = LibGit2.clone(repo_url, repo_path) try @test isdir(repo_path) @test isdir(joinpath(repo_path, ".git")) @@ -26,25 +25,13 @@ mktempdir() do dir repo_path = joinpath(dir, "Example2") # credentials are required because github tries to authenticate on unknown repo cred = LibGit2.UserPasswordCredentials("","") # empty credentials cause authentication error - LibGit2.clone(repo_url_https*randstring(10), repo_path, payload=Nullable(cred)) + LibGit2.clone(repo_url*randstring(10), repo_path, payload=Nullable(cred)) error("unexpected") catch ex @test isa(ex, LibGit2.Error.GitError) @test ex.code == LibGit2.Error.EAUTH end #end - - #TODO: remove or condition on libgit2 features this test when ssh protocol will be supported - #@testset "with 'ssh' protocol (by default is not supported)" begin - repo_path = joinpath(dir, "Example3") - repo = LibGit2.clone(repo_url_git, repo_path) - try - @test isdir(repo_path) - @test isdir(joinpath(repo_path, ".git")) - finally - finalize(repo) - end - #end #end end diff --git a/test/libgit2.jl b/test/libgit2.jl index 96a183c3d6e27..ed07633b47b48 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -112,7 +112,6 @@ const LIBGIT2_MIN_VER = v"0.23.0" mktempdir() do dir # test parameters repo_url = "https://github.com/JuliaLang/Example.jl" - ssh_prefix = "git@" cache_repo = joinpath(dir, "Example") test_repo = joinpath(dir, "Example.Test") test_sig = LibGit2.Signature("TEST", "TEST@TEST.COM", round(time(), 0), 0) From ea8fa251fdb7434e2e84fab58d74d5079412bf78 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 17 Aug 2016 13:40:45 -0500 Subject: [PATCH 5/5] Update URL parsing tests Added a new explicit SSH protocol test which generated other changes. --- test/libgit2.jl | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/test/libgit2.jl b/test/libgit2.jl index ed07633b47b48..d2a8fbc91b48d 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -77,16 +77,34 @@ const LIBGIT2_MIN_VER = v"0.23.0" #end #@testset "URL parsing" begin - # Use all named group - m = match(LibGit2.URL_REGEX, "https://user:pass@hostname.com:80/path/repo.git") + # HTTPS URL + m = match(LibGit2.URL_REGEX, "https://user:pass@server.com:80/org/project.git") @test m[:scheme] == "https" @test m[:user] == "user" @test m[:password] == "pass" - @test m[:host] == "hostname.com" + @test m[:host] == "server.com" @test m[:port] == "80" - @test m[:path] == "/path/repo.git" + @test m[:path] == "/org/project.git" - # Realistic example HTTP example + # SSH URL + m = match(LibGit2.URL_REGEX, "ssh://user:pass@server:22/project.git") + @test m[:scheme] == "ssh" + @test m[:user] == "user" + @test m[:password] == "pass" + @test m[:host] == "server" + @test m[:port] == "22" + @test m[:path] == "/project.git" + + # SSH URL using scp-like syntax + m = match(LibGit2.URL_REGEX, "user@server:project.git") + @test m[:scheme] == nothing + @test m[:user] == "user" + @test m[:password] == nothing + @test m[:host] == "server" + @test m[:port] == nothing + @test m[:path] == "project.git" + + # Realistic example from GitHub using HTTPS m = match(LibGit2.URL_REGEX, "https://github.com/JuliaLang/Example.jl.git") @test m[:scheme] == "https" @test m[:user] == nothing @@ -95,7 +113,7 @@ const LIBGIT2_MIN_VER = v"0.23.0" @test m[:port] == nothing @test m[:path] == "/JuliaLang/Example.jl.git" - # Realistic example SSH example + # Realistic example from GitHub using SSH m = match(LibGit2.URL_REGEX, "git@github.com:JuliaLang/Example.jl.git") @test m[:scheme] == nothing @test m[:user] == "git" @@ -104,7 +122,7 @@ const LIBGIT2_MIN_VER = v"0.23.0" @test m[:port] == nothing @test m[:path] == "JuliaLang/Example.jl.git" - # Make sure that a username can contain special characters + # Make sure usernames can contain special characters m = match(LibGit2.URL_REGEX, "user-name@hostname.com") @test m[:user] == "user-name" #end