Skip to content

Commit 9b5379f

Browse files
committed
bump-formula-pr: support bumping resource using revision as version
1 parent 4ef2edc commit 9b5379f

2 files changed

Lines changed: 87 additions & 24 deletions

File tree

Library/Homebrew/dev-cmd/bump-formula-pr.rb

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -750,40 +750,52 @@ def parse_resource_versions_arg
750750
).returns(Symbol)
751751
}
752752
def update_resource_block!(formula, resource, new_version)
753-
ohai "Updating resource \"#{resource.name}\" from #{resource.version} to #{new_version}"
753+
old_version = resource.version.to_s
754+
ohai "Updating resource \"#{resource.name}\" from #{old_version} to #{new_version}"
754755

755756
old_url = resource.url
756757
raise ArgumentError, "resource \"#{resource.name}\" has no URL" if old_url.nil?
757758

758-
if (old_tag = resource.specs[:tag].presence) && resource.download_strategy <= GitDownloadStrategy
759-
tag = update_url(old_tag, resource.version.to_s, new_version)
760-
if tag == old_tag
761-
opoo <<~EOS
762-
You need to bump resource "#{resource.name}" manually since the new tag
763-
and old tag are both:
764-
#{tag}
765-
EOS
766-
return :tag_unchanged
767-
end
759+
if resource.download_strategy <= GitDownloadStrategy
760+
old_tag = resource.specs[:tag].presence
761+
old_revision = resource.specs[:revision].presence
762+
763+
if old_tag
764+
tag = update_url(old_tag, old_version, new_version)
765+
if tag == old_tag
766+
opoo <<~EOS
767+
You need to bump resource "#{resource.name}" manually since the new tag
768+
and old tag are both:
769+
#{tag}
770+
EOS
771+
return :tag_unchanged
772+
end
768773

769-
# `specs` omits `using:`, so pass it through to keep an explicit strategy
770-
git_specs = { tag: }
771-
if (using = resource.using.presence)
772-
git_specs[:using] = using
773-
end
774-
resource_path, forced_version = fetch_resource_and_forced_version(resource, new_version, old_url,
775-
**git_specs)
776-
new_revision = Utils.popen_read("git", "-C", resource_path.to_s, "rev-parse", "-q", "--verify",
777-
"HEAD").strip
778-
if new_revision.blank?
779-
opoo "Could not resolve a revision for resource \"#{resource.name}\" tag #{tag}."
774+
# `specs` omits `using:`, so pass it through to keep an explicit strategy
775+
git_specs = { tag: }
776+
if (using = resource.using.presence)
777+
git_specs[:using] = using
778+
end
779+
resource_path, forced_version = fetch_resource_and_forced_version(resource, new_version, old_url,
780+
**git_specs)
781+
new_revision = Utils.popen_read("git", "-C", resource_path.to_s, "rev-parse", "-q", "--verify",
782+
"HEAD").strip
783+
if new_revision.blank?
784+
opoo "Could not resolve a revision for resource \"#{resource.name}\" tag #{tag}."
785+
return :revision_unresolved
786+
end
787+
elsif old_revision == old_version
788+
new_revision = new_version
789+
forced_version = true
790+
else
791+
opoo "Could not resolve a revision for resource \"#{resource.name}\" version #{new_version}."
780792
return :revision_unresolved
781793
end
782794

783795
resource_name = resource.name.to_s
784796
formula_ast = Utils::AST::FormulaAST.new(formula.path.read)
785-
formula_ast.replace_resource_stanza_hash_value(resource_name, :url, :tag, tag)
786-
if resource.specs[:revision].present?
797+
formula_ast.replace_resource_stanza_hash_value(resource_name, :url, :tag, tag) if tag
798+
if old_revision.present?
787799
formula_ast.replace_resource_stanza_hash_value(resource_name, :url, :revision, new_revision)
788800
end
789801

Library/Homebrew/test/dev-cmd/bump-formula-pr_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,33 @@ class Gitresourceball < Formula
424424
expect(formula_path.read).to include("revision: \"#{"b" * 40}\"")
425425
end
426426

427+
it "updates revision for a git resource without a tag" do
428+
old_revision = "a" * 40
429+
new_revision = "b" * 40
430+
formula_path = CoreTap.instance.new_formula_path("gitresourceball")
431+
formula_path.dirname.mkpath
432+
formula_path.write <<~RUBY
433+
class Gitresourceball < Formula
434+
url "https://brew.sh/gitresourceball-1.0.tar.gz"
435+
436+
resource "foo" do
437+
url "https://brew.sh/foo.git",
438+
revision: "#{old_revision}"
439+
version "#{old_revision}"
440+
end
441+
end
442+
RUBY
443+
CoreTap.instance.clear_cache
444+
Formulary.clear_cache
445+
Formula.clear_cache
446+
formula = Formulary.from_contents("gitresourceball", formula_path, formula_path.read)
447+
448+
resource_versions = { "foo" => { current_version: old_revision, latest_version: new_revision } }
449+
450+
expect(bump_formula_pr.update_resources!(formula, resource_versions:)).to eq({ "foo" => :success })
451+
expect(formula_path.read).to include("revision: \"#{new_revision}\"", "version \"#{new_revision}\"")
452+
end
453+
427454
it "updates the URL for a non-git resource carrying a tag" do
428455
formula_path = CoreTap.instance.new_formula_path("tarballwithtag")
429456
formula_path.dirname.mkpath
@@ -478,6 +505,30 @@ class Sametagball < Formula
478505
expect(bump_formula_pr.update_resources!(formula, resource_versions:)).to eq({ "foo" => :tag_unchanged })
479506
end
480507

508+
it "reports git resources where new revision cannot be detected from new version" do
509+
formula_path = CoreTap.instance.new_formula_path("sametagball")
510+
formula_path.dirname.mkpath
511+
formula_path.write <<~RUBY
512+
class Sametagball < Formula
513+
url "https://brew.sh/sametagball-1.0.tar.gz"
514+
515+
resource "foo" do
516+
url "https://brew.sh/foo.git",
517+
revision: "#{"a" * 40}"
518+
version "1.2.3"
519+
end
520+
end
521+
RUBY
522+
CoreTap.instance.clear_cache
523+
Formulary.clear_cache
524+
Formula.clear_cache
525+
formula = Formulary.from_contents("sametagball", formula_path, formula_path.read)
526+
527+
resource_versions = { "foo" => { current_version: "1.2.3", latest_version: "2.0.0" } }
528+
529+
expect(bump_formula_pr.update_resources!(formula, resource_versions:)).to eq({ "foo" => :revision_unresolved })
530+
end
531+
481532
it "downgrades to requested version" do
482533
version = "0.1.2"
483534
resource_versions = { "foo" => { current_version: "1.2.3", latest_version: version } }

0 commit comments

Comments
 (0)