Skip to content

Commit 1926acf

Browse files
authored
[Prefix] Take out function to find the git commit SHA for given git tree hash (#220)
1 parent 100c408 commit 1926acf

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

src/Prefix.jl

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,44 @@ function get_tree_hash(tree::LibGit2.GitTree)
413413
return unsafe_load(oid_ptr)
414414
end
415415

416+
"""
417+
get_commit_sha(url::String, tree_hash::Base.SHA1; verbose::Bool=false)
418+
419+
Find the latest git commit corresponding to the given git tree SHA1 for the remote
420+
repository with the given `url`. The repository is cached locally for quicker future
421+
access. If `verbose` is `true`, print to screen some debugging information.
422+
423+
The return value is the commit SHA as a `String`, if the corresponding revision is found,
424+
`nothing` otherwise.
425+
"""
426+
function get_commit_sha(url::String, tree_hash::Base.SHA1; verbose::Bool=false)
427+
git_commit_sha = nothing
428+
dir = cached_git_clone(url; verbose)
429+
430+
LibGit2.with(LibGit2.GitRepo(dir)) do repo
431+
LibGit2.with(LibGit2.GitRevWalker(repo)) do walker
432+
# The repo is cached, so locally it may be checking out an outdated commit.
433+
# Start the search from HEAD of the tracking upstream repo.
434+
try
435+
LibGit2.push!(walker, LibGit2.GitHash(LibGit2.peel(LibGit2.GitCommit, LibGit2.upstream(LibGit2.head(repo)))))
436+
catch
437+
@warn("Could not walk from origin branch!")
438+
LibGit2.push_head!(walker)
439+
end
440+
# For each commit in the git repo, check to see if its treehash
441+
# matches the one we're looking for.
442+
for oid in walker
443+
tree = LibGit2.peel(LibGit2.GitTree, LibGit2.GitCommit(repo, oid))
444+
if all(get_tree_hash(tree).val .== tree_hash.bytes)
445+
git_commit_sha = LibGit2.string(oid)
446+
break
447+
end
448+
end
449+
end
450+
end
451+
return git_commit_sha
452+
end
453+
416454
"""
417455
get_addable_spec(name::AbstractString, version::VersionNumber)
418456
@@ -467,39 +505,15 @@ function get_addable_spec(name::AbstractString, version::VersionNumber;
467505
end
468506

469507
tree_hash_sha1 = first(tree_hashes)
470-
tree_hash_bytes = tree_hash_sha1.bytes
471508

472509
# Once we have a tree hash, turn that into a git commit sha
473510
git_commit_sha = nothing
474511
valid_url = nothing
475512
for url in repo_urls
476-
dir = cached_git_clone(url; verbose)
477-
478-
LibGit2.with(LibGit2.GitRepo(dir)) do repo
479-
LibGit2.with(LibGit2.GitRevWalker(repo)) do walker
480-
# The repo is cached, so locally it may be checking out an outdated commit.
481-
# Start the search from HEAD of the tracking upstream repo.
482-
try
483-
LibGit2.push!(walker, LibGit2.GitHash(LibGit2.peel(LibGit2.GitCommit, LibGit2.upstream(LibGit2.head(repo)))))
484-
catch
485-
@warn("Could not walk from origin branch!")
486-
LibGit2.push_head!(walker)
487-
end
488-
# For each commit in the git repo, check to see if its treehash
489-
# matches the one we're looking for.
490-
for oid in walker
491-
tree = LibGit2.peel(LibGit2.GitTree, LibGit2.GitCommit(repo, oid))
492-
if all(get_tree_hash(tree).val .== tree_hash_bytes)
493-
git_commit_sha = LibGit2.string(oid)
494-
valid_url = url
495-
break
496-
end
497-
end
498-
end
499-
end
500-
513+
git_commit_sha = get_commit_sha(url, tree_hash_sha1; verbose)
501514
# Stop searching urls as soon as we find one
502515
if git_commit_sha !== nothing
516+
valid_url = url
503517
break
504518
end
505519
end
@@ -508,7 +522,7 @@ function get_addable_spec(name::AbstractString, version::VersionNumber;
508522
@error("Unable to find revision for specified dependency!",
509523
name,
510524
version,
511-
tree_hash = bytes2hex(tree_hash_bytes),
525+
tree_hash = bytes2hex(tree_hash_sha1.bytes),
512526
repo_urls,
513527
)
514528
error("Unable to find revision for specified dependency!")

0 commit comments

Comments
 (0)