Always suggest a HEAD branch name if we can find one

- If a HEAD branch name isn't specified at all, then the user probably
  wants to shortcut adding one by being told what the default branch for
  the repo is. Otherwise they have to click the URL, look at the GitHub
  UI, then type the branch name into `branch: "foo"` syntax.
This commit is contained in:
Issy Long 2025-08-10 21:46:53 +01:00
parent 05b27aa847
commit d0e9a2d7d6
No known key found for this signature in database
2 changed files with 29 additions and 15 deletions

View File

@ -178,23 +178,22 @@ module Homebrew
end end
def audit_head_branch def audit_head_branch
return unless @online
return if spec_name != :head return if spec_name != :head
return if specs[:tag].present? return if specs[:tag].present?
return if specs[:revision].present? return if specs[:revision].present?
# Skip `resource` URLs as they use SHAs instead of branch specifiers. # Skip `resource` URLs as they use SHAs instead of branch specifiers.
return if name != owner.name return if name != owner.name
return unless url.end_with?(".git") return unless url.end_with?(".git")
problem "Git `head` URL must specify a branch name" if specs[:branch].blank?
return unless @online
return unless Utils::Git.remote_exists?(url) return unless Utils::Git.remote_exists?(url)
detected_branch = Utils.popen_read("git", "ls-remote", "--symref", url, "HEAD") detected_branch = Utils.popen_read("git", "ls-remote", "--symref", url, "HEAD")
.match(%r{ref: refs/heads/(.*?)\s+HEAD})&.to_a&.second .match(%r{ref: refs/heads/(.*?)\s+HEAD})&.to_a&.second
return if detected_branch.blank? || detected_branch == specs[:branch]
problem "Detected a default branch \"#{detected_branch}\", not \"#{specs[:branch]}\"" message = "Git `head` URL must specify a branch name"
message += " - try `branch: \"#{detected_branch}\"`" if detected_branch.present?
problem message if specs[:branch].blank? || detected_branch != specs[:branch]
end end
def problem(text) def problem(text)

View File

@ -715,16 +715,17 @@ RSpec.describe Homebrew::FormulaAuditor do
end end
it "requires `branch:` to be specified for Git head URLs" do it "requires `branch:` to be specified for Git head URLs" do
fa = formula_auditor "foo", <<~RUBY fa = formula_auditor "foo", <<~RUBY, online: true
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e" sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
head "https://github.com/example/foo.git" head "https://github.com/Homebrew/homebrew-test-bot.git"
end end
RUBY RUBY
fa.audit_specs fa.audit_specs
expect(fa.problems.first[:message]).to match("Git `head` URL must specify a branch name") # This is `.last` because the first problem is the unreachable stable URL.
expect(fa.problems.last[:message]).to match("Git `head` URL must specify a branch name")
end end
it "suggests a detected default branch for Git head URLs" do it "suggests a detected default branch for Git head URLs" do
@ -732,17 +733,31 @@ RSpec.describe Homebrew::FormulaAuditor do
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e" sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
head "https://github.com/Homebrew/homebrew-core.git", branch: "master" head "https://github.com/Homebrew/homebrew-test-bot.git", branch: "master"
end
RUBY
message = "Git `head` URL must specify a branch name - try `branch: \"main\"`"
fa.audit_specs
# This is `.last` because the first problem is the unreachable stable URL.
expect(fa.problems.last[:message]).to match(message)
end
it "ignores a pre-existing correct HEAD branch name" do
fa = formula_auditor "foo", <<~RUBY, online: true
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
head "https://github.com/Homebrew/homebrew-test-bot.git", branch: "main"
end end
RUBY RUBY
fa.audit_specs fa.audit_specs
# This is `.last` because the first problem is the unreachable stable URL. expect(fa.problems).not_to match("Git `head` URL must specify a branch name")
expect(fa.problems.last[:message]).to match('Detected a default branch "main", not "master"')
end end
it "ignores `branch:` for non-Git head URLs" do it "ignores `branch:` for non-Git head URLs" do
fa = formula_auditor "foo", <<~RUBY fa = formula_auditor "foo", <<~RUBY, online: true
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e" sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
@ -755,7 +770,7 @@ RSpec.describe Homebrew::FormulaAuditor do
end end
it "ignores `branch:` for `resource` URLs" do it "ignores `branch:` for `resource` URLs" do
fa = formula_auditor "foo", <<~RUBY fa = formula_auditor "foo", <<~RUBY, online: true
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e" sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
@ -768,7 +783,7 @@ RSpec.describe Homebrew::FormulaAuditor do
RUBY RUBY
fa.audit_specs fa.audit_specs
expect(fa.problems).to be_empty expect(fa.problems).not_to match("Git `head` URL must specify a branch name")
end end
it "allows versions with no throttle rate" do it "allows versions with no throttle rate" do