Skip to content

Commit e83dc1c

Browse files
committed
Fallback to older bottle hashes when viable
1 parent e47bc2f commit e83dc1c

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

Library/Homebrew/dev-cmd/generate-formula-api.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def run
7575
version = Version.new(formula.dig("versions", "stable"))
7676
pkg_version = PkgVersion.new(version, formula["revision"])
7777
rebuild = formula.dig("bottle", "stable", "rebuild") || 0
78-
sha256 = formula.dig("bottle", "stable", "files", :all, "sha256")
79-
sha256 ||= formula.dig("bottle", "stable", "files", bottle_tag.to_sym, "sha256")
78+
sha256 = newest_bottle_sha256(formula, bottle_tag)
8079

8180
[name, [pkg_version.to_s, rebuild, sha256]]
8281
end
@@ -88,6 +87,35 @@ def run
8887
end
8988
end
9089

90+
sig {
91+
params(formula_json: T::Hash[String, T.untyped], bottle_tag: Utils::Bottles::Tag).returns(T.nilable(String))
92+
}
93+
def newest_bottle_sha256(formula_json, bottle_tag)
94+
available_tags = formula_json.dig("bottle", "stable", "files")&.keys&.map(&:to_sym)
95+
return unless available_tags
96+
97+
return formula_json.dig("bottle", "stable", "files", :all, "sha256") if available_tags.include? :all
98+
99+
if available_tags.include? bottle_tag.to_sym
100+
return formula_json.dig("bottle", "stable", "files", bottle_tag.to_sym, "sha256")
101+
end
102+
103+
return unless bottle_tag.macos?
104+
105+
# If the actual tag is not available, find the newest tag with matching arch that's older than the actual tag
106+
newest_viable_macos_tag = available_tags.filter_map do |tag_sym|
107+
tag = Utils::Bottles::Tag.from_symbol(tag_sym)
108+
next unless tag.macos?
109+
next if tag.arch != bottle_tag.arch
110+
next if tag.to_macos_version > bottle_tag.to_macos_version
111+
112+
tag
113+
end.max_by(&:to_macos_version)
114+
return unless newest_viable_macos_tag
115+
116+
formula_json.dig("bottle", "stable", "files", newest_viable_macos_tag.to_sym, "sha256")
117+
end
118+
91119
private
92120

93121
sig { params(title: String).returns(String) }

Library/Homebrew/test/dev-cmd/generate-formula-api_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,58 @@
55

66
RSpec.describe Homebrew::DevCmd::GenerateFormulaApi do
77
it_behaves_like "parseable arguments"
8+
9+
describe "#newest_bottle_sha256" do
10+
subject(:generate_formula_api) { described_class.new [] }
11+
12+
def make_json(bottles)
13+
json = {
14+
"bottle" => {
15+
"stable" => {
16+
"files" => {},
17+
},
18+
},
19+
}
20+
bottles.each do |tag, sha256|
21+
json["bottle"]["stable"]["files"][tag] = { "sha256" => sha256 }
22+
end
23+
json
24+
end
25+
26+
expected_sha256s = {
27+
arm64_sequoia: "abc123",
28+
arm64_sonoma: "abc123",
29+
arm64_ventura: "ghi789",
30+
arm64_monterey: nil,
31+
sequoia: "jkl012",
32+
sonoma: "jkl012",
33+
ventura: "mno345",
34+
monterey: "mno345",
35+
x86_64_linux: "pqr678",
36+
arm64_linux: nil,
37+
}.transform_keys do |tag|
38+
Utils::Bottles::Tag.from_symbol(tag)
39+
end
40+
41+
let(:all_json) { make_json all: "abc123" }
42+
let(:standard_json) do
43+
make_json arm64_sonoma: "abc123",
44+
arm64_ventura: "ghi789",
45+
sonoma: "jkl012",
46+
big_sur: "mno345",
47+
x86_64_linux: "pqr678"
48+
end
49+
50+
it "returns the sha256 for the :all tag on all systems" do
51+
expected_sha256s.each_key do |tag|
52+
expect(generate_formula_api.newest_bottle_sha256(all_json, tag)).to eq("abc123")
53+
end
54+
end
55+
56+
expected_sha256s.each_key do |tag|
57+
it "returns the corrent sha256 for #{tag}" do
58+
expect(generate_formula_api.newest_bottle_sha256(standard_json, tag)).to eq(expected_sha256s[tag])
59+
end
60+
end
61+
end
862
end

0 commit comments

Comments
 (0)