Skip to content

Commit

Permalink
Avoid full clones when already at the tip of a branch
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Nov 4, 2022
1 parent 389ae99 commit 95c6f95
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions common/lib/dependabot/git_commit_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def pinned_ref_looks_like_commit_sha?
ref_looks_like_commit_sha?(ref)
end

def head_commit_for_pinned_ref
ref = dependency_source_details.fetch(:ref)
local_repo_git_metadata_fetcher.head_commit_for_ref_sha(ref)
end

def ref_looks_like_commit_sha?(ref)
return false unless ref&.match?(/^[0-9a-f]{6,40}$/)

Expand Down
6 changes: 6 additions & 0 deletions common/lib/dependabot/git_metadata_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def head_commit_for_ref(ref)
commit_sha
end

def head_commit_for_ref_sha(ref)
refs_for_upload_pack.
find { |r| r.ref_sha == ref }&.
commit_sha
end

private

attr_reader :url, :credentials
Expand Down
21 changes: 13 additions & 8 deletions github_actions/lib/dependabot/github_actions/update_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,23 @@ def fetch_latest_version_for_git_dependency

def latest_commit_for_pinned_ref
@latest_commit_for_pinned_ref ||= begin
url = dependency_source_details[:url]
source = Source.from_url(url)
head_commit_for_ref_sha = git_commit_checker.head_commit_for_pinned_ref
if head_commit_for_ref_sha
head_commit_for_ref_sha
else
url = dependency_source_details[:url]
source = Source.from_url(url)

SharedHelpers.in_a_temporary_directory(File.dirname(source.repo)) do |temp_dir|
repo_contents_path = File.join(temp_dir, File.basename(source.repo))
SharedHelpers.in_a_temporary_directory(File.dirname(source.repo)) do |temp_dir|
repo_contents_path = File.join(temp_dir, File.basename(source.repo))

SharedHelpers.run_shell_command("git clone --no-recurse-submodules #{url} #{repo_contents_path}")
SharedHelpers.run_shell_command("git clone --no-recurse-submodules #{url} #{repo_contents_path}")

Dir.chdir(repo_contents_path) do
ref_branch = find_container_branch(dependency_source_details[:ref])
Dir.chdir(repo_contents_path) do
ref_branch = find_container_branch(dependency_source_details[:ref])

git_commit_checker.head_commit_for_local_branch(ref_branch)
git_commit_checker.head_commit_for_local_branch(ref_branch)
end
end
end
end
Expand Down

0 comments on commit 95c6f95

Please sign in to comment.