Skip to content

Commit 2e5e64d

Browse files
committed
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.
1 parent 05b27aa commit 2e5e64d

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

Library/Homebrew/resource_auditor.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,24 @@ def audit_urls
178178
end
179179

180180
def audit_head_branch
181+
return unless @online
181182
return if spec_name != :head
182183
return if specs[:tag].present?
183184
return if specs[:revision].present?
184185
# Skip `resource` URLs as they use SHAs instead of branch specifiers.
185186
return if name != owner.name
186187
return unless url.end_with?(".git")
187188

188-
problem "Git `head` URL must specify a branch name" if specs[:branch].blank?
189+
message = "Git `head` URL must specify a branch name"
189190

190-
return unless @online
191191
return unless Utils::Git.remote_exists?(url)
192192

193193
detected_branch = Utils.popen_read("git", "ls-remote", "--symref", url, "HEAD")
194194
.match(%r{ref: refs/heads/(.*?)\s+HEAD})&.to_a&.second
195-
return if detected_branch.blank? || detected_branch == specs[:branch]
196195

197-
problem "Detected a default branch \"#{detected_branch}\", not \"#{specs[:branch]}\""
196+
message += " - try `branch: \"#{detected_branch}\"`)" if detected_branch != specs[:branch]
197+
198+
problem message
198199
end
199200

200201
def problem(text)

Library/Homebrew/test/formula_auditor_spec.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -715,34 +715,35 @@ class Foo < Formula
715715
end
716716

717717
it "requires `branch:` to be specified for Git head URLs" do
718-
fa = formula_auditor "foo", <<~RUBY
718+
fa = formula_auditor "foo", <<~RUBY, online: true
719719
class Foo < Formula
720720
url "https://brew.sh/foo-1.0.tgz"
721721
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
722-
head "https://github.com/example/foo.git"
722+
head "https://github.com/Homebrew/homebrew-test-bot.git"
723723
end
724724
RUBY
725725

726726
fa.audit_specs
727-
expect(fa.problems.first[:message]).to match("Git `head` URL must specify a branch name")
727+
# This is `.last` because the first problem is the unreachable stable URL.
728+
expect(fa.problems.last[:message]).to match("Git `head` URL must specify a branch name")
728729
end
729730

730731
it "suggests a detected default branch for Git head URLs" do
731732
fa = formula_auditor "foo", <<~RUBY, online: true
732733
class Foo < Formula
733734
url "https://brew.sh/foo-1.0.tgz"
734735
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
735-
head "https://github.com/Homebrew/homebrew-core.git", branch: "master"
736+
head "https://github.com/Homebrew/homebrew-test-bot.git", branch: "master"
736737
end
737738
RUBY
738739

739740
fa.audit_specs
740741
# This is `.last` because the first problem is the unreachable stable URL.
741-
expect(fa.problems.last[:message]).to match('Detected a default branch "main", not "master"')
742+
expect(fa.problems.last[:message]).to match("Git `head` URL must specify a branch name - try `branch: \"main\"`")
742743
end
743744

744745
it "ignores `branch:` for non-Git head URLs" do
745-
fa = formula_auditor "foo", <<~RUBY
746+
fa = formula_auditor "foo", <<~RUBY, online: true
746747
class Foo < Formula
747748
url "https://brew.sh/foo-1.0.tgz"
748749
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
@@ -755,7 +756,7 @@ class Foo < Formula
755756
end
756757

757758
it "ignores `branch:` for `resource` URLs" do
758-
fa = formula_auditor "foo", <<~RUBY
759+
fa = formula_auditor "foo", <<~RUBY, online: true
759760
class Foo < Formula
760761
url "https://brew.sh/foo-1.0.tgz"
761762
sha256 "31cccfc6630528db1c8e3a06f6decf2a370060b982841cfab2b8677400a5092e"
@@ -768,7 +769,7 @@ class Foo < Formula
768769
RUBY
769770

770771
fa.audit_specs
771-
expect(fa.problems).to be_empty
772+
expect(fa.problems).not_to match("Git `head` URL must specify a branch name")
772773
end
773774

774775
it "allows versions with no throttle rate" do

0 commit comments

Comments
 (0)