diff --git a/base/pkg.jl b/base/pkg.jl index 5bf1124bb7f48..43188cc0a2afd 100644 --- a/base/pkg.jl +++ b/base/pkg.jl @@ -2,9 +2,9 @@ module Pkg -export Git, Dir, Types, Reqs, Cache, Read, Query, Resolve, Write, Entry, Git +export Dir, Types, Reqs, Cache, Read, Query, Resolve, Write, Entry, Git export dir, init, rm, add, available, installed, status, clone, checkout, - update, resolve, test, build, free, pin, PkgError + update, resolve, test, build, free, pin, PkgError, setprotocol! const DEFAULT_META = "https://github.com/JuliaLang/METADATA.jl" const META_BRANCH = "metadata-v2" @@ -56,6 +56,8 @@ test(pkgs::AbstractString...; coverage::Bool=false) = cd(Entry.test,AbstractStri dependents(packagename::AbstractString) = Reqs.dependents(packagename) +setprotocol!(proto::AbstractString) = Cache.setprotocol!(proto) + # point users to PkgDev register(args...) = diff --git a/base/pkg/cache.jl b/base/pkg/cache.jl index 5ad11cf503c15..f4b33db198326 100644 --- a/base/pkg/cache.jl +++ b/base/pkg/cache.jl @@ -5,6 +5,11 @@ module Cache import ...LibGit2, ..Dir, ...Pkg.PkgError using ..Types +rewrite_url_to = "https" + +const GITHUB_REGEX = + r"^(?:git@|git://|https://(?:[\w\.\+\-]+@)?)github.com[:/](([^/].+)/(.+?))(?:\.git)?$"i + path(pkg::AbstractString) = abspath(".cache", pkg) function mkcachedir() @@ -30,23 +35,24 @@ end function prefetch(pkg::AbstractString, url::AbstractString, sha1s::Vector) isdir(".cache") || mkcachedir() - #TODO: force switch to https - #url = LibGit2.normalize_url(url) + cache = path(pkg) + normalized_url = normalize_url(url) + repo = if isdir(cache) LibGit2.GitRepo(cache) # open repo, free it at the end else - info("Cloning cache of $pkg from $url") + info("Cloning cache of $pkg from $normalized_url") try # clone repo, free it at the end - LibGit2.clone(url, cache, isbare = true, remote_cb = LibGit2.mirror_cb()) + LibGit2.clone(normalized_url, cache, isbare = true, remote_cb = LibGit2.mirror_cb()) catch err isdir(cache) && rm(cache, recursive=true) - throw(PkgError("Cannot clone $pkg from $url. $(err.msg)")) + throw(PkgError("Cannot clone $pkg from $normalized_url. $(err.msg)")) end end try - LibGit2.set_remote_url(repo, url) + LibGit2.set_remote_url(repo, normalized_url) in_cache = BitArray(map(sha1->LibGit2.iscommit(sha1, repo), sha1s)) if !all(in_cache) info("Updating cache of $pkg...") @@ -61,4 +67,23 @@ end prefetch(pkg::AbstractString, url::AbstractString, sha1::AbstractString...) = prefetch(pkg, url, AbstractString[sha1...]) +function setprotocol!(proto::AbstractString) + global rewrite_url_to + + if length(proto) == 0 + rewrite_url_to = nothing + else + rewrite_url_to = proto + end +end + +function normalize_url(url::AbstractString) + global rewrite_url_to + + m = match(GITHUB_REGEX,url) + (m === nothing || rewrite_url_to === nothing) ? + url : "$rewrite_url_to://github.com/$(m.captures[1]).git" + +end + end # module diff --git a/base/pkg/write.jl b/base/pkg/write.jl index 706d28b883432..ff9d9d9ed15e6 100644 --- a/base/pkg/write.jl +++ b/base/pkg/write.jl @@ -27,7 +27,7 @@ function fetch(repo::GitRepo, pkg::AbstractString, sha1::AbstractString) end function checkout(repo::GitRepo, pkg::AbstractString, sha1::AbstractString) - LibGit2.set_remote_url(repo, Read.url(pkg)) + LibGit2.set_remote_url(repo, Cache.normalize_url(Read.url(pkg))) LibGit2.checkout!(repo, sha1) end