Skip to content

Commit

Permalink
make sure LibGit2 repo closes if we fail in install_git (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jul 10, 2018
1 parent b6f9244 commit 0d48326
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions stdlib/Pkg/src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -442,41 +442,47 @@ function install_git(
version::Union{VersionNumber,Nothing},
version_path::String
)::Nothing
repo, git_hash = Base.shred!(LibGit2.CachedCredentials()) do creds
clones_dir = joinpath(depots()[1], "clones")
ispath(clones_dir) || mkpath(clones_dir)
repo_path = joinpath(clones_dir, string(uuid))
repo = ispath(repo_path) ? LibGit2.GitRepo(repo_path) : begin
GitTools.clone(urls[1], repo_path; isbare=true, header = "[$uuid] $name from $(urls[1])", credentials=creds)
end
git_hash = LibGit2.GitHash(hash.bytes)
for url in urls
try LibGit2.with(LibGit2.GitObject, repo, git_hash) do g
repo = nothing
tree = nothing
try
repo, git_hash = Base.shred!(LibGit2.CachedCredentials()) do creds
clones_dir = joinpath(depots()[1], "clones")
ispath(clones_dir) || mkpath(clones_dir)
repo_path = joinpath(clones_dir, string(uuid))
repo = ispath(repo_path) ? LibGit2.GitRepo(repo_path) : begin
GitTools.clone(urls[1], repo_path; isbare=true, header = "[$uuid] $name from $(urls[1])", credentials=creds)
end
git_hash = LibGit2.GitHash(hash.bytes)
for url in urls
try LibGit2.with(LibGit2.GitObject, repo, git_hash) do g
end
break # object was found, we can stop
catch err
err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow(err)
end
break # object was found, we can stop
catch err
err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow(err)
GitTools.fetch(repo, url, refspecs=refspecs, credentials=creds)
end
GitTools.fetch(repo, url, refspecs=refspecs, credentials=creds)
return repo, git_hash
end
return repo, git_hash
end
tree = try
LibGit2.GitObject(repo, git_hash)
catch err
err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow(err)
error("$name: git object $(string(hash)) could not be found")
end
tree isa LibGit2.GitTree ||
error("$name: git object $(string(hash)) should be a tree, not $(typeof(tree))")
mkpath(version_path)
opts = LibGit2.CheckoutOptions(
checkout_strategy = LibGit2.Consts.CHECKOUT_FORCE,
target_directory = Base.unsafe_convert(Cstring, version_path)
)
LibGit2.checkout_tree(repo, tree, options=opts)
close(repo); close(tree)
return
tree = try
LibGit2.GitObject(repo, git_hash)
catch err
err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow(err)
error("$name: git object $(string(hash)) could not be found")
end
tree isa LibGit2.GitTree ||
error("$name: git object $(string(hash)) should be a tree, not $(typeof(tree))")
mkpath(version_path)
opts = LibGit2.CheckoutOptions(
checkout_strategy = LibGit2.Consts.CHECKOUT_FORCE,
target_directory = Base.unsafe_convert(Cstring, version_path)
)
LibGit2.checkout_tree(repo, tree, options=opts)
return
finally
repo !== nothing && LibGit2.close(repo)
tree !== nothing && LibGit2.close(tree)
end
end

# install & update manifest
Expand Down

0 comments on commit 0d48326

Please sign in to comment.