Skip to content

Commit

Permalink
Merge pull request #11312 from LachlanGunn/master
Browse files Browse the repository at this point in the history
WIP: Rewrite git://github.com to https://github.com in package URLs.
  • Loading branch information
tkelman committed Nov 4, 2015
2 parents 99dc2eb + ce2500e commit 8798aab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
6 changes: 4 additions & 2 deletions base/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...) =
Expand Down
37 changes: 31 additions & 6 deletions base/pkg/cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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...")
Expand All @@ -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
2 changes: 1 addition & 1 deletion base/pkg/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8798aab

Please sign in to comment.