forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.jl
50 lines (43 loc) · 1.28 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
module Cache
import ..Git, ..Dir
using ..Types
path(pkg::String) = abspath(".cache", pkg)
function mkcachedir()
cache = joinpath(realpath("."), ".cache")
if isdir(cache)
return
end
@windows_only mkdir(cache)
@unix_only begin
if Dir.isversioned(pwd())
rootcache = joinpath(realpath(".."), ".cache")
if !isdir(rootcache)
mkdir(rootcache)
end
symlink(rootcache, cache)
return
end
mkdir(cache)
end
end
function prefetch{S<:String}(pkg::String, url::String, sha1s::Vector{S})
isdir(".cache") || mkcachedir()
cache = path(pkg)
if !isdir(cache)
info("Cloning cache of $pkg from $url")
try Git.run(`clone -q --mirror $url $cache`)
catch
rm(cache, recursive=true)
rethrow()
end
end
Git.set_remote_url(url, dir=cache)
if !all(sha1->Git.iscommit(sha1, dir=cache), sha1s)
info("Updating cache of $pkg...")
Git.success(`remote update`, dir=cache) ||
error("couldn't update $cache using `git remote update`")
end
filter(sha1->!Git.iscommit(sha1, dir=cache), sha1s)
end
prefetch(pkg::String, url::String, sha1::String...) = prefetch(pkg, url, String[sha1...])
end # module