From b7426b4d5114831691a61c1c613fb5e49f65f129 Mon Sep 17 00:00:00 2001 From: Bob Lail Date: Fri, 22 Oct 2021 12:05:39 -0500 Subject: [PATCH] When using `git fetch origin` to pull resources from GitHub, pull the default branch instead of all refs --- Library/Homebrew/download_strategy.rb | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 128a55b8ec..ea8a21162e 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -897,10 +897,16 @@ class GitDownloadStrategy < VCSDownloadStrategy case @ref_type when :branch then "+refs/heads/#{@ref}:refs/remotes/origin/#{@ref}" when :tag then "+refs/tags/#{@ref}:refs/tags/#{@ref}" - else "+refs/heads/*:refs/remotes/origin/*" + else default_refspec end end + sig { returns(String) } + def default_refspec + # https://git-scm.com/book/en/v2/Git-Internals-The-Refspec + "+refs/heads/*:refs/remotes/origin/*" + end + sig { void } def config_repo command! "git", @@ -1069,6 +1075,30 @@ class GitHubGitDownloadStrategy < GitDownloadStrategy super end end + + sig { returns(String) } + def default_refspec + default_branch.yield_self do |branch| + if branch + "+refs/heads/#{branch}:refs/remotes/origin/#{branch}" + else + super + end + end + end + + sig { returns(String) } + def default_branch + command! "git", + args: ["remote", "set-head", "origin", "--auto"], + chdir: cached_location + + result = command! "git", + args: ["symbolic-ref", "refs/remotes/origin/HEAD"], + chdir: cached_location + + result.stdout[%r{^refs/remotes/origin/(.*)$}, 1] + end end # Strategy for downloading a CVS repository.