-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
write.jl
64 lines (55 loc) · 1.94 KB
/
write.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
# This file is a part of Julia. License is MIT: http:https://julialang.org/license
module Write
import ...LibGit2, ..Cache, ..Read, ...Pkg.PkgError
importall ...LibGit2
function prefetch(pkg::AbstractString, sha1::AbstractString)
isempty(Cache.prefetch(pkg, Read.url(pkg), sha1)) && return
throw(PkgError("$pkg: couldn't find commit $(sha1[1:10])"))
end
function fetch(repo::GitRepo, pkg::AbstractString, sha1::AbstractString)
cache = Cache.path(pkg)
LibGit2.fetch(repo, remoteurl=cache, refspecs=["+refs/*:refs/remotes/cache/*"])
LibGit2.need_update(repo)
LibGit2.iscommit(sha1, repo) && return
f = with(GitRepo, cache) do repo
LibGit2.iscommit(sha1, repo)
end ? "fetch" : "prefetch"
url = Read.issue_url(pkg)
if isempty(url)
throw(PkgError("$pkg: $f failed to get commit $(sha1[1:10]), please file a bug report with the package author."))
else
throw(PkgError("$pkg: $f failed to get commit $(sha1[1:10]), please file an issue at $url"))
end
end
function checkout(repo::GitRepo, pkg::AbstractString, sha1::AbstractString)
LibGit2.set_remote_url(repo, Cache.normalize_url(Read.url(pkg)))
LibGit2.checkout!(repo, sha1)
end
function install(pkg::AbstractString, sha1::AbstractString)
prefetch(pkg, sha1)
repo = if isdir(".trash/$pkg")
mv(".trash/$pkg", "./$pkg") #TODO check for newer version in cache before moving
GitRepo(pkg)
else
LibGit2.clone(Cache.path(pkg), pkg)
end
try
fetch(repo, pkg, sha1)
checkout(repo, pkg, sha1)
finally
finalize(repo)
end
end
function update(pkg::AbstractString, sha1::AbstractString)
prefetch(pkg, sha1)
with(GitRepo, pkg) do repo
fetch(repo, pkg, sha1)
checkout(repo, pkg, sha1)
end
end
function remove(pkg::AbstractString)
isdir(".trash") || mkdir(".trash")
ispath(".trash/$pkg") && rm(".trash/$pkg", recursive=true)
mv(pkg, ".trash/$pkg")
end
end # module