Skip to content

Commit

Permalink
Make sure repo.url points to a git repo before adding (#451)
Browse files Browse the repository at this point in the history
* Check that repo urls point to a git repo

* Move `validate_repo_url` to `GitTools` module

* Add Pkg-specific error messages to GitTools `clone` and `fetch`

* Alter a test to be Windows-compatible

(cherry picked from commit 046bde4d660715291f6a84eaba3cd08917dc7cee)
  • Loading branch information
00vareladavid authored and KristofferC committed Jul 5, 2018
1 parent 3bd8ce0 commit a3fa396
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
13 changes: 11 additions & 2 deletions stdlib/Pkg/src/GitTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ function clone(url, source_path; header=nothing, kwargs...)
catch err
rm(source_path; force=true, recursive=true)
err isa LibGit2.GitError || rethrow(err)
Pkg.Types.cmderror("failed to clone from $(url), error: $err")
if (err.class == LibGit2.Error.Net && err.code == LibGit2.Error.EINVALIDSPEC) ||
(err.class == LibGit2.Error.Repository && err.code == LibGit2.Error.ENOTFOUND)
Pkg.Types.cmderror("Git repository not found at '$(url)'")
else
Pkg.Types.cmderror("failed to clone from $(url), error: $err")
end
finally
print(stdout, "\033[2K") # clear line
print(stdout, "\e[?25h") # put back cursor
Expand All @@ -122,7 +127,11 @@ function fetch(repo::LibGit2.GitRepo, remoteurl=nothing; header=nothing, kwargs.
return LibGit2.fetch(repo; remoteurl=remoteurl, callbacks=callbacks, kwargs...)
catch err
err isa LibGit2.GitError || rethrow(err)
Pkg.Types.cmderror("failed to fetch from $(remoteurl), error: $err")
if (err.class == LibGit2.Error.Repository && err.code == LibGit2.Error.ERROR)
Pkg.Types.cmderror("Git repository not found at '$(remoteurl)'")
else
Pkg.Types.cmderror("failed to fetch from $(remoteurl), error: $err")
end
finally
print(stdout, "\033[2K") # clear line
print(stdout, "\e[?25h") # put back cursor
Expand Down
38 changes: 38 additions & 0 deletions stdlib/Pkg/test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,44 @@ temp_pkg_dir() do project_path
end # @testset
end

temp_pkg_dir() do project_path
@testset "invalid repo url" begin
cd(project_path) do
@test_throws CommandError Pkg.add("https://github.com")
Pkg.generate("FooBar")
@test_throws CommandError Pkg.add("./Foobar")
end
end
end

temp_pkg_dir() do project_path
function with_dummy_env(f)
TEST_SIG = LibGit2.Signature("TEST", "[email protected]", round(time()), 0)
env_path = joinpath(mktempdir(), "Dummy")
Pkg.generate(env_path)
repo = LibGit2.init(env_path)
LibGit2.add!(repo, "*")
LibGit2.commit(repo, "initial commit"; author=TEST_SIG, committer=TEST_SIG)
Pkg.activate(env_path)
try
f()
finally
Pkg.activate()
end
end
# pkg assumes `Example.jl` is still a git repo, it will try to fetch on `update`
# `fetch` should warn that it is no longer a git repo
with_dummy_env() do
@testset "inconsistent repo state" begin
package_path = joinpath(project_path, "Example")
LibGit2.clone("https://github.com/JuliaLang/Example.jl", package_path)
Pkg.add(package_path)
rm(joinpath(package_path, ".git"); force=true, recursive=true)
@test_throws CommandError Pkg.up()
end
end
end

include("repl.jl")

end # module

0 comments on commit a3fa396

Please sign in to comment.