Skip to content

Commit

Permalink
Merge #908
Browse files Browse the repository at this point in the history
908: Normalize Gitlab urls r=KristofferC a=morris25

Allows gitlab urls to be normalized by a separate protocol.

Co-authored-by: morris25 <sam.morrison@invenia.ca>
  • Loading branch information
bors[bot] and morris25 committed Jan 22, 2019
2 parents dc71544 + 8d1f449 commit 32396f2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
10 changes: 9 additions & 1 deletion src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,15 @@ function activate(path::AbstractString; shared::Bool=false)
Base.ACTIVE_PROJECT[] = Base.load_path_expand(fullpath)
end

setprotocol!(proto::Union{Nothing, AbstractString}=nothing) = GitTools.setprotocol!(proto)
function setprotocol!(;
domain::AbstractString="github.com",
protocol::Union{Nothing, AbstractString}=nothing
)
GitTools.setprotocol!(domain=domain, protocol=protocol)
return nothing
end

@deprecate setprotocol!(proto::Union{Nothing, AbstractString}) setprotocol!(protocol = proto) false

# API constructor
function Package(;name::Union{Nothing,AbstractString} = nothing,
Expand Down
37 changes: 27 additions & 10 deletions src/GitTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,39 @@ function transfer_progress(progress::Ptr{LibGit2.TransferProgress}, p::Any)
return Cint(0)
end

const GIT_REGEX =
r"^(?:(?<proto>git|ssh|https)://)?(?:[\w\.\+\-:]+@)?(?<hostname>.+?)(?(<proto>)/|:)(?<path>.+?)(?:\.git)?$"
const GIT_PROTOCOLS = Dict{String, Union{Nothing, String}}()
const GIT_USERS = Dict{String, Union{Nothing, String}}()

const GITHUB_REGEX =
r"^(?:git@|git://|https://(?:[\w\.\+\-]+@)?)github.com[:/](([^/].+)/(.+?))(?:\.git)?$"i
const GIT_PROTOCOL = Ref{Union{String, Nothing}}(nothing)
@deprecate setprotocol!(proto::Union{Nothing, AbstractString}) setprotocol!(protocol = proto) false

setprotocol!(proto::Union{Nothing, AbstractString}=nothing) = GIT_PROTOCOL[] = proto
function setprotocol!(;
domain::AbstractString="github.com",
protocol::Union{Nothing, AbstractString}=nothing,
user::Union{Nothing, AbstractString}=(protocol == "ssh" ? "git" : nothing)
)
domain = lowercase(domain)
GIT_PROTOCOLS[domain] = protocol
GIT_USERS[domain] = user
end

# TODO: extend this to more urls
function normalize_url(url::AbstractString)
m = match(GITHUB_REGEX, url)
if m === nothing || GIT_PROTOCOL[] === nothing
m = match(GIT_REGEX, url)
m === nothing && return url

host = m[:hostname]
path = "$(m[:path]).git"

proto = get(GIT_PROTOCOLS, lowercase(host), nothing)

if proto === nothing
url
elseif GIT_PROTOCOL[] == "ssh"
"ssh://git@github.com/$(m.captures[1]).git"
else
"$(GIT_PROTOCOL[])://github.com/$(m.captures[1]).git"
user = get(GIT_USERS, lowercase(host), nothing)
user = user === nothing ? "" : "$user@"

"$proto://$user$host/$path"
end
end

Expand Down
14 changes: 12 additions & 2 deletions src/Pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,21 @@ Below is a comparison between the REPL version and the `PackageSpec` version:
const PackageSpec = API.Package

"""
Pkg.setprotocol!(proto::Union{Nothing, AbstractString}=nothing)
setprotocol!(;
domain::AbstractString = "github.com",
protocol::Union{Nothing, AbstractString}=nothing
)
Set the protocol used to access GitHub-hosted packages when `add`ing a url or `develop`ing a package.
Set the protocol used to access hosted packages when `add`ing a url or `develop`ing a package.
Defaults to delegating the choice to the package developer (`proto == nothing`).
Other choices for `proto` are `"https"` or `"git"`.
# Examples
```julia-repl
julia> Pkg.setprotocol!(domain = "github.com", protocol = "ssh")
julia> Pkg.setprotocol!(domain = "gitlab.mycompany.com")
```
"""
const setprotocol! = API.setprotocol!

Expand Down
22 changes: 17 additions & 5 deletions test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ temp_pkg_dir() do project_path
mktempdir() do devdir
withenv("JULIA_PKG_DEVDIR" => devdir) do
try
Pkg.setprotocol!("notarealprotocol")
Pkg.setprotocol!(domain = "github.com", protocol = "notarealprotocol")
@test_throws PkgError Pkg.develop("Example")
Pkg.setprotocol!("https")
Pkg.setprotocol!(domain = "github.com", protocol = "https")
Pkg.develop("Example")
@test isinstalled(TEST_PKG)
finally
Pkg.setprotocol!()
Pkg.setprotocol!(domain = "github.com")
end
end
end
Expand All @@ -270,14 +270,26 @@ temp_pkg_dir() do project_path
https_url = "https://github.com/JuliaLang/Example.jl.git"
ssh_url = "ssh://git@github.com/JuliaLang/Example.jl.git"
@test Pkg.GitTools.normalize_url(https_url) == https_url
Pkg.setprotocol!("ssh")
Pkg.setprotocol!(domain = "github.com", protocol = "ssh")
@test Pkg.GitTools.normalize_url(https_url) == ssh_url
# TODO: figure out how to test this without
# having to deploy a ssh key on github
#Pkg.develop("Example")
#@test isinstalled(TEST_PKG)

https_url = "https://gitlab.example.com/example/Example.jl.git"
ssh_url = "ssh://git@gitlab.example.com/example/Example.jl.git"

@test Pkg.GitTools.normalize_url(https_url) == https_url
Pkg.setprotocol!(domain = "gitlab.example.com", protocol = "ssh")
@test Pkg.GitTools.normalize_url(https_url) == ssh_url

@test_deprecated Pkg.setprotocol!("ssh")
@test_deprecated Pkg.GitTools.setprotocol!("ssh")

finally
Pkg.setprotocol!()
Pkg.setprotocol!(domain = "github.com")
Pkg.setprotocol!(domain = "gitlab.example.com")
end
end
end
Expand Down

0 comments on commit 32396f2

Please sign in to comment.