diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index f2c34437d9..7c08865607 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/master:refs/remotes/origin/master" + 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 + if default_branch + "+refs/heads/#{default_branch}:refs/remotes/origin/#{default_branch}" + else + super + end + end + + sig { returns(String) } + def default_branch + return @default_branch if defined?(@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 + + @default_branch = result.stdout[%r{^refs/remotes/origin/(.*)$}, 1] + end end # Strategy for downloading a CVS repository.