-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
cache.jl
85 lines (69 loc) · 2.3 KB
/
cache.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# This file is a part of Julia. License is MIT: http:https://julialang.org/license
module Cache
import ...LibGit2, ..Dir, ...Pkg.PkgError
using ..Types
rewrite_url_to = "https"
const GITHUB_REGEX =
r"^(?:git@|git:https://|https://(?:[\w\.\+\-]+@)?)github.com[:/](([^/].+)/(.+?))(?:\.git)?$"i
path(pkg::AbstractString) = abspath(".cache", pkg)
function mkcachedir()
cache = joinpath(realpath("."), ".cache")
if isdir(cache)
return
end
@unix_only if Dir.isversioned(pwd())
rootcache = joinpath(realpath(".."), ".cache")
if !isdir(rootcache)
mkdir(rootcache)
end
symlink(rootcache, cache)
return
end
mkdir(cache)
end
function prefetch(pkg::AbstractString, url::AbstractString, sha1s::Vector)
isdir(".cache") || mkcachedir()
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 $normalized_url")
try
# clone repo, free it at the end
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 $normalized_url. $(err.msg)"))
end
end
try
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...")
LibGit2.fetch(repo)
in_cache = BitArray(map(sha1->LibGit2.iscommit(sha1, repo), sha1s))
end
sha1s[!in_cache]
finally
finalize(repo) # closing repo opened/created above
end
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:https://github.com/$(m.captures[1]).git"
end
end # module