Skip to content

Commit a6252b1

Browse files
omusKristofferC
authored andcommitted
Add credential handling
1 parent a1392e8 commit a6252b1

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/GitTools.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,23 @@ function transfer_progress(progress::Ptr{LibGit2.GitTransferProgress}, p::Any)
4646
return Cint(0)
4747
end
4848

49-
function clone(url, source_path; isbare::Bool=false, header = nothing, branch = nothing)
49+
function clone(url, source_path; isbare::Bool=false, header = nothing, branch = nothing, credentials = nothing)
5050
isdir(source_path) && error("$source_path already exists")
5151
printstyled(stdout, "Cloning "; color = :green, bold=true)
5252
if header == nothing
5353
println(stdout, "from git repo: ", url, ".")
5454
else
5555
println(stdout, header)
5656
end
57-
p = MiniProgressBar(header = "Fetching:", color = Base.info_color())
57+
transfer_payload = MiniProgressBar(header = "Fetching:", color = Base.info_color())
58+
cred_payload = LibGit2.CredentialPayload(credentials)
5859
print(stdout, "\e[?25l")
5960
try
6061
GC.@preserve p branch begin
61-
callbacks = LibGit2.RemoteCallbacks(transfer_progress=cfunction(transfer_progress, Cint, Tuple{Ptr{LibGit2.GitTransferProgress}, Any}),
62-
payload = pointer_from_objref(p))
62+
callbacks = LibGit2.RemoteCallbacks(
63+
credentials=(LibGit2.credential_cb(), pointer_from_objref(cred_payload)),
64+
transfer_progress=(cfunction(transfer_progress, Cint, Tuple{Ptr{LibGit2.GitTransferProgress}, Any}), pointer_from_objref(transfer_payload)),
65+
)
6366
fetch_opts = LibGit2.FetchOptions(callbacks = callbacks)
6467
if branch == nothing
6568
clone_opts = LibGit2.CloneOptions(fetch_opts=fetch_opts, bare=isbare)
@@ -69,12 +72,17 @@ function clone(url, source_path; isbare::Bool=false, header = nothing, branch =
6972
return LibGit2.clone(url, source_path, clone_opts)
7073
end
7174
catch e
75+
if isa(e, LibGit2.GitError) && e.code == LibGit2.Error.EAUTH
76+
LibGit2.reject(cred_payload)
77+
end
78+
7279
rm(source_path; force=true, recursive=true)
7380
rethrow(e)
7481
finally
7582
print(stdout, "\e[?25h")
7683
println(stdout)
7784
end
85+
LibGit2.approve(cred_payload)
7886
end
7987

80-
end # module
88+
end # module

src/Operations.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,12 @@ function install_git(
375375
version::Union{VersionNumber,Nothing},
376376
version_path::String
377377
)::Nothing
378+
creds = LibGit2.CachedCredentials()
378379
clones_dir = joinpath(depots()[1], "clones")
379380
ispath(clones_dir) || mkpath(clones_dir)
380381
repo_path = joinpath(clones_dir, string(uuid))
381382
repo = ispath(repo_path) ? LibGit2.GitRepo(repo_path) : begin
382-
GitTools.clone(urls[1], repo_path; isbare=true, header = "[$uuid] $name from $(urls[1])")
383+
GitTools.clone(urls[1], repo_path; isbare=true, header = "[$uuid] $name from $(urls[1])", credentials=creds)
383384
end
384385
git_hash = LibGit2.GitHash(hash.bytes)
385386
for i = 2:length(urls)
@@ -390,7 +391,7 @@ function install_git(
390391
err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow(err)
391392
end
392393
url = urls[i]
393-
LibGit2.fetch(repo, remoteurl=url, refspecs=refspecs)
394+
LibGit2.fetch(repo, remoteurl=url, refspecs=refspecs, credentials=creds)
394395
end
395396
tree = try
396397
with(LibGit2.GitObject, repo, git_hash) do g

src/Types.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ function isdir_windows_workaround(path::String)
601601
end
602602

603603
function handle_repos_develop!(ctx::Context, pkgs::AbstractVector{PackageSpec})
604+
creds = LibGit2.CachedCredentials()
604605
env = ctx.env
605606
for pkg in pkgs
606607
pkg.repo == nothing && continue
@@ -623,12 +624,12 @@ function handle_repos_develop!(ctx::Context, pkgs::AbstractVector{PackageSpec})
623624
repo, just_cloned = ispath(repo_path) ? (LibGit2.GitRepo(repo_path), false) : begin
624625
printpkgstyle(ctx, :Cloning, "package from $(pkg.repo.url)")
625626
r = LibGit2.clone(pkg.repo.url, repo_path)
626-
LibGit2.fetch(r, remoteurl=pkg.repo.url, refspecs=refspecs)
627+
GitTools.fetch(r, remoteurl=pkg.repo.url, refspecs=refspecs, credentials=creds)
627628
r, true
628629
end
629630
if !just_cloned
630631
printpkgstyle(ctx, :Updating, "repo from $(pkg.repo.url)")
631-
LibGit2.fetch(repo, remoteurl=pkg.repo.url, refspecs=refspecs)
632+
GitTools.fetch(repo, remoteurl=pkg.repo.url, refspecs=refspecs, credentials=creds)
632633
end
633634
close(repo)
634635

@@ -660,6 +661,7 @@ function handle_repos_develop!(ctx::Context, pkgs::AbstractVector{PackageSpec})
660661
end
661662

662663
function handle_repos_add!(ctx::Context, pkgs::AbstractVector{PackageSpec}; upgrade_or_add::Bool=true)
664+
creds = LibGit2.CachedCredentials()
663665
env = ctx.env
664666
for pkg in pkgs
665667
pkg.repo == nothing && continue
@@ -669,15 +671,15 @@ function handle_repos_add!(ctx::Context, pkgs::AbstractVector{PackageSpec}; upgr
669671
mkpath(clones_dir)
670672
repo_path = joinpath(clones_dir, string(hash(pkg.repo.url)))
671673
repo, just_cloned = ispath(repo_path) ? (LibGit2.GitRepo(repo_path), false) : begin
672-
r = GitTools.clone(pkg.repo.url, repo_path, isbare=true)
673-
LibGit2.fetch(r, remoteurl=pkg.repo.url, refspecs=refspecs)
674+
r = GitTools.clone(pkg.repo.url, repo_path, isbare=true, credentials=creds)
675+
GitTools.fetch(r, remoteurl=pkg.repo.url, refspecs=refspecs, credentials=creds)
674676
r, true
675677
end
676678
info = manifest_info(env, pkg.uuid)
677679
pinned = (info != nothing && get(info, "pinned", false))
678680
if upgrade_or_add && !pinned && !just_cloned
679681
printpkgstyle(ctx, :Updating, "repo from $(pkg.repo.url)")
680-
LibGit2.fetch(repo, remoteurl=pkg.repo.url, refspecs=refspecs)
682+
GitTools.fetch(repo, remoteurl=pkg.repo.url, refspecs=refspecs, credentials=creds)
681683
end
682684
if upgrade_or_add && !pinned
683685
rev = pkg.repo.rev
@@ -928,10 +930,11 @@ function registries()::Vector{String}
928930
user_regs = abspath(depots()[1], "registries")
929931
if !ispath(user_regs)
930932
mkpath(user_regs)
933+
creds = LibGit2.CachedCredentials()
931934
printpkgstyle(stdout, :Cloning, "default registries into $user_regs")
932935
for (reg, url) in DEFAULT_REGISTRIES
933936
path = joinpath(user_regs, reg)
934-
GitTools.clone(url, path; header = "registry $reg from $(repr(url))")
937+
GitTools.clone(url, path; header = "registry $reg from $(repr(url))", credentials = creds)
935938
end
936939
end
937940
return [r for d in depots() for r in registries(d)]

0 commit comments

Comments
 (0)