diff --git a/src/Types.jl b/src/Types.jl index 73e6007fd1..0b7f567594 100644 --- a/src/Types.jl +++ b/src/Types.jl @@ -594,66 +594,68 @@ function handle_repo_add!(ctx::Context, pkg::PackageSpec) end end - LibGit2.with(GitTools.ensure_clone(ctx, add_repo_cache_path(repo_source), repo_source; isbare=true)) do repo - GitTools.check_valid_HEAD(repo) + let repo_source = repo_source + LibGit2.with(GitTools.ensure_clone(ctx, add_repo_cache_path(repo_source), repo_source; isbare=true)) do repo + GitTools.check_valid_HEAD(repo) - # If the user didn't specify rev, assume they want the default (master) branch if on a branch, otherwise the current commit - if pkg.repo.rev === nothing - pkg.repo.rev = LibGit2.isattached(repo) ? LibGit2.branch(repo) : string(LibGit2.GitHash(LibGit2.head(repo))) - end + # If the user didn't specify rev, assume they want the default (master) branch if on a branch, otherwise the current commit + if pkg.repo.rev === nothing + pkg.repo.rev = LibGit2.isattached(repo) ? LibGit2.branch(repo) : string(LibGit2.GitHash(LibGit2.head(repo))) + end - obj_branch = get_object_or_branch(repo, pkg.repo.rev) - fetched = false - if obj_branch === nothing - fetched = true - GitTools.fetch(ctx, repo, repo_source; refspecs=refspecs) obj_branch = get_object_or_branch(repo, pkg.repo.rev) + fetched = false if obj_branch === nothing - pkgerror("Did not find rev $(pkg.repo.rev) in repository") + fetched = true + GitTools.fetch(ctx, repo, repo_source; refspecs=refspecs) + obj_branch = get_object_or_branch(repo, pkg.repo.rev) + if obj_branch === nothing + pkgerror("Did not find rev $(pkg.repo.rev) in repository") + end + end + gitobject, isbranch = obj_branch + + # If we are tracking a branch and are not pinned we want to update the repo if we haven't done that yet + innerentry = manifest_info(ctx, pkg.uuid) + ispinned = innerentry !== nothing && innerentry.pinned + if isbranch && !fetched && !ispinned + GitTools.fetch(ctx, repo, repo_source; refspecs=refspecs) + gitobject, isbranch = get_object_or_branch(repo, pkg.repo.rev) end - end - gitobject, isbranch = obj_branch - - # If we are tracking a branch and are not pinned we want to update the repo if we haven't done that yet - entry = manifest_info(ctx, pkg.uuid) - ispinned = entry !== nothing && entry.pinned - if isbranch && !fetched && !ispinned - GitTools.fetch(ctx, repo, repo_source; refspecs=refspecs) - gitobject, isbranch = get_object_or_branch(repo, pkg.repo.rev) - end - # Now we have the gitobject for our ref, time to find the tree hash for it - tree_hash_object = LibGit2.peel(LibGit2.GitTree, gitobject) - if pkg.repo.subdir !== nothing - try - tree_hash_object = tree_hash_object[pkg.repo.subdir] - catch e - e isa KeyError || rethrow() - pkgerror("Did not find subdirectory `$(pkg.repo.subdir)`") + # Now we have the gitobject for our ref, time to find the tree hash for it + tree_hash_object = LibGit2.peel(LibGit2.GitTree, gitobject) + if pkg.repo.subdir !== nothing + try + tree_hash_object = tree_hash_object[pkg.repo.subdir] + catch e + e isa KeyError || rethrow() + pkgerror("Did not find subdirectory `$(pkg.repo.subdir)`") + end end - end - pkg.tree_hash = SHA1(string(LibGit2.GitHash(tree_hash_object))) + pkg.tree_hash = SHA1(string(LibGit2.GitHash(tree_hash_object))) - # If we already resolved a uuid, we can bail early if this package is already installed at the current tree_hash - if has_uuid(pkg) - version_path = Pkg.Operations.source_path(ctx, pkg) - isdir(version_path) && return false - end + # If we already resolved a uuid, we can bail early if this package is already installed at the current tree_hash + if has_uuid(pkg) + version_path = Pkg.Operations.source_path(ctx, pkg) + isdir(version_path) && return false + end - temp_path = mktempdir() - GitTools.checkout_tree_to_path(repo, tree_hash_object, temp_path) - package = resolve_projectfile!(ctx, pkg, temp_path) + temp_path = mktempdir() + GitTools.checkout_tree_to_path(repo, tree_hash_object, temp_path) + package = resolve_projectfile!(ctx, pkg, temp_path) - # Now that we are fully resolved (name, UUID, tree_hash, repo.source, repo.rev), we can finally - # check to see if the package exists at its canonical path. - version_path = Pkg.Operations.source_path(ctx, pkg) - isdir(version_path) && return false + # Now that we are fully resolved (name, UUID, tree_hash, repo.source, repo.rev), we can finally + # check to see if the package exists at its canonical path. + version_path = Pkg.Operations.source_path(ctx, pkg) + isdir(version_path) && return false - # Otherwise, move the temporary path into its correct place and set read only - mkpath(version_path) - mv(temp_path, version_path; force=true) - set_readonly(version_path) - return true + # Otherwise, move the temporary path into its correct place and set read only + mkpath(version_path) + mv(temp_path, version_path; force=true) + set_readonly(version_path) + return true + end end end