Skip to content

Commit

Permalink
Document Tab.for_keg and use Keg#tab where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Apr 28, 2024
1 parent 5e027bf commit 2df9869
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def self.autoremove(dry_run: false)

require "uninstall"

kegs_by_rack = removable_formulae.map(&:any_installed_keg).group_by(&:rack)
kegs_by_rack = removable_formulae.filter_map(&:any_installed_keg).group_by(&:rack)
Uninstall.uninstall_kegs(kegs_by_rack)

# The installed formula cache will be invalid after uninstalling.
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cli/named_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,11 @@ def resolve_latest_keg(name)
# Return keg if it is the only installed keg
return kegs if kegs.length == 1

stable_kegs = kegs.reject { |k| k.version.head? }
stable_kegs = kegs.reject { |keg| keg.version.head? }

if stable_kegs.blank?
return kegs.max_by do |keg|
[Tab.for_keg(keg).source_modified_time, keg.version.revision]
[keg.tab.source_modified_time, keg.version.revision]
end
end

Expand Down
6 changes: 3 additions & 3 deletions Library/Homebrew/cmd/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ def info_formula(formula)
end

kegs = formula.installed_kegs
heads, versioned = kegs.partition { |k| k.version.head? }
heads, versioned = kegs.partition { |keg| keg.version.head? }

Check warning on line 296 in Library/Homebrew/cmd/info.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L296

Added line #L296 was not covered by tests
kegs = [
*heads.sort_by { |k| -Tab.for_keg(k).time.to_i },
*heads.sort_by { |keg| -keg.tab.time.to_i },

Check warning on line 298 in Library/Homebrew/cmd/info.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L298

Added line #L298 was not covered by tests
*versioned.sort_by(&:scheme_and_version),
]
if kegs.empty?
Expand All @@ -304,7 +304,7 @@ def info_formula(formula)
puts "Installed"
kegs.each do |keg|
puts "#{keg} (#{keg.abv})#{" *" if keg.linked?}"
tab = Tab.for_keg(keg).to_s
tab = keg.tab.to_s

Check warning on line 307 in Library/Homebrew/cmd/info.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/info.rb#L307

Added line #L307 was not covered by tests
puts " #{tab}" unless tab.empty?
end
end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cmd/leaves.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def run
private

def installed_on_request?(formula)
Tab.for_keg(formula.any_installed_keg).installed_on_request
formula.any_installed_keg.tab.installed_on_request

Check warning on line 43 in Library/Homebrew/cmd/leaves.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/leaves.rb#L43

Added line #L43 was not covered by tests
end

def installed_as_dependency?(formula)
Tab.for_keg(formula.any_installed_keg).installed_as_dependency
formula.any_installed_keg.installed_as_dependency

Check warning on line 47 in Library/Homebrew/cmd/leaves.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/leaves.rb#L47

Added line #L47 was not covered by tests
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/cmd/update-report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def output_update_report
next unless formula.any_version_installed?

keg = formula.installed_kegs.last
tab = Tab.for_keg(keg)
tab = keg.tab

Check warning on line 165 in Library/Homebrew/cmd/update-report.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/update-report.rb#L165

Added line #L165 was not covered by tests
# force a `brew upgrade` from the linuxbrew-core version to the homebrew-core version (even if lower)
tab.source["versions"]["version_scheme"] = -1
tab.write
Expand Down Expand Up @@ -631,7 +631,7 @@ def migrate_tap_migration

next unless (dir = HOMEBREW_CELLAR/name).exist? # skip if formula is not installed.

tabs = dir.subdirs.map { |d| Tab.for_keg(Keg.new(d)) }
tabs = dir.subdirs.map { |d| Keg.new(d).tab }

Check warning on line 634 in Library/Homebrew/cmd/update-report.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cmd/update-report.rb#L634

Added line #L634 was not covered by tests
next if tabs.first.tap != tap # skip if installed formula is not from this tap.

new_tap = Tap.fetch(new_tap_name)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/bottle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def bottle_formula(formula)
Tab.clear_cache
Dependency.clear_cache
Requirement.clear_cache
tab = Tab.for_keg(keg)
tab = keg.tab
original_tab = tab.dup
tab.poured_from_bottle = false
tab.time = nil
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/diagnostic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ def check_deleted_formula
kegs = Keg.all

deleted_formulae = kegs.filter_map do |keg|
tap = Tab.for_keg(keg).tap
tap = keg.tab.tap

Check warning on line 835 in Library/Homebrew/diagnostic.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/diagnostic.rb#L835

Added line #L835 was not covered by tests
tap_keg_name = tap ? "#{tap}/#{keg.name}" : keg.name

loadable = [
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/extend/os/linux/cmd/update-report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def migrate_gcc_dependents_if_needed
next unless recursive_runtime_dependencies.map(&:name).include? "gcc"

keg = formula.installed_kegs.last
tab = Tab.for_keg(keg)
tab = keg.tab

Check warning on line 26 in Library/Homebrew/extend/os/linux/cmd/update-report.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/linux/cmd/update-report.rb#L26

Added line #L26 was not covered by tests
# Force reinstallation upon `brew upgrade` to fix the bottle RPATH.
tab.source["versions"]["version_scheme"] = -1
tab.write
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/extend/os/mac/formula_cellar_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def check_linkage
#{checker.display_test_output}
EOS

tab = Tab.for_keg(keg)
tab = keg.tab

Check warning on line 83 in Library/Homebrew/extend/os/mac/formula_cellar_checks.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/mac/formula_cellar_checks.rb#L83

Added line #L83 was not covered by tests
if tab.poured_from_bottle
output += <<~EOS
Rebuild this from source with:
Expand Down
28 changes: 17 additions & 11 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ def latest_head_version
end

head_versions.max_by do |pn_pkgversion|
[Tab.for_keg(prefix(pn_pkgversion)).source_modified_time, pn_pkgversion.revision]
[Keg.new(prefix(pn_pkgversion)).tab.source_modified_time, pn_pkgversion.revision]
end
end

Expand Down Expand Up @@ -751,6 +751,7 @@ def installed_prefixes
end

# All currently installed kegs.
sig { returns(T::Array[Keg]) }
def installed_kegs
installed_prefixes.map { |dir| Keg.new(dir) }
end
Expand Down Expand Up @@ -1408,7 +1409,7 @@ def link_overwrite?(path)
rescue NotAKegError, Errno::ENOENT
# file doesn't belong to any keg.
else
tab_tap = Tab.for_keg(keg).tap
tab_tap = keg.tab.tap
# this keg doesn't below to any core/tap formula, most likely coming from a DIY install.
return false if tab_tap.nil?

Expand Down Expand Up @@ -1850,12 +1851,15 @@ def time
# universal binaries in a {Formula}'s {Keg}.
sig { params(targets: T.nilable(T.any(Pathname, String))).void }
def deuniversalize_machos(*targets)
targets = nil if targets.blank?
targets ||= any_installed_keg.mach_o_files.select do |file|
file.arch == :universal && file.archs.include?(Hardware::CPU.arch)
if targets.none?
targets = any_installed_keg&.mach_o_files&.select do |file|
file.arch == :universal && file.archs.include?(Hardware::CPU.arch)

Check warning on line 1856 in Library/Homebrew/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L1856

Added line #L1856 was not covered by tests
end
end

targets.each { |t| extract_macho_slice_from(Pathname.new(t), Hardware::CPU.arch) }
targets&.each do |target|
extract_macho_slice_from(Pathname(target), Hardware::CPU.arch)

Check warning on line 1861 in Library/Homebrew/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L1861

Added line #L1861 was not covered by tests
end
end

sig { params(file: Pathname, arch: T.nilable(Symbol)).void }
Expand Down Expand Up @@ -2147,6 +2151,7 @@ def recursive_requirements(&block)

# Returns a Keg for the opt_prefix or installed_prefix if they exist.
# If not, return `nil`.
sig { returns(T.nilable(Keg)) }
def any_installed_keg
Formula.cache[:any_installed_keg] ||= {}
Formula.cache[:any_installed_keg][full_name] ||= if (installed_prefix = any_installed_prefix)
Expand Down Expand Up @@ -2328,7 +2333,7 @@ def to_hash
hsh.merge!(dependencies_hash)

hsh["installed"] = installed_kegs.sort_by(&:scheme_and_version).map do |keg|
tab = Tab.for_keg keg
tab = keg.tab

Check warning on line 2336 in Library/Homebrew/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula.rb#L2336

Added line #L2336 was not covered by tests
{
"version" => keg.version.to_s,
"used_options" => tab.used_options.as_flags,
Expand Down Expand Up @@ -2882,12 +2887,13 @@ def eligible_kegs_for_cleanup(quiet: false)
eligible_for_cleanup = []
if latest_version_installed?
eligible_kegs = if head? && (head_prefix = latest_head_prefix)
head, stable = installed_kegs.partition { |k| k.version.head? }
# Remove newest head and stable kegs
head - [Keg.new(head_prefix)] + stable.sort_by(&:scheme_and_version).slice(0...-1)
head, stable = installed_kegs.partition { |keg| keg.version.head? }

# Remove newest head and stable kegs.
head - [Keg.new(head_prefix)] + T.must(stable.sort_by(&:scheme_and_version).slice(0...-1))
else
installed_kegs.select do |keg|
tab = Tab.for_keg(keg)
tab = keg.tab
if version_scheme > tab.version_scheme
true
elsif version_scheme == tab.version_scheme
Expand Down
8 changes: 4 additions & 4 deletions Library/Homebrew/formula_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def install
Pathname(brew_prefix/"#{formula.name}.rb").atomic_write(s)

keg = Keg.new(formula.prefix)
tab = Tab.for_keg(keg)
tab = keg.tab
tab.installed_as_dependency = installed_as_dependency?
tab.installed_on_request = installed_on_request?
tab.write
Expand Down Expand Up @@ -707,15 +707,15 @@ def install_dependency(dep, inherited_options)

if df.linked_keg.directory?
linked_keg = Keg.new(df.linked_keg.resolved_path)
tab = Tab.for_keg(linked_keg)
tab = linked_keg.tab

Check warning on line 710 in Library/Homebrew/formula_installer.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula_installer.rb#L710

Added line #L710 was not covered by tests
keg_had_linked_keg = true
keg_was_linked = linked_keg.linked?
linked_keg.unlink
end

if df.latest_version_installed?
installed_keg = Keg.new(df.prefix)
tab ||= Tab.for_keg(installed_keg)
tab ||= installed_keg.tab

Check warning on line 718 in Library/Homebrew/formula_installer.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula_installer.rb#L718

Added line #L718 was not covered by tests
tmp_keg = Pathname.new("#{installed_keg}.tmp")
installed_keg.rename(tmp_keg)
end
Expand Down Expand Up @@ -822,7 +822,7 @@ def finish
end

# Update tab with actual runtime dependencies
tab = Tab.for_keg(keg)
tab = keg.tab
Tab.clear_cache
f_runtime_deps = formula.runtime_dependencies(read_from_tab: false)
tab.runtime_dependencies = Tab.runtime_deps_hash(formula, f_runtime_deps)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ def self.from_keg(
force_bottle: T.unsafe(nil),
flags: T.unsafe(nil)
)
tab = Tab.for_keg(keg)
tab = keg.tab
tap = tab.tap
spec ||= tab.spec

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def install_formula?(
return false unless formula.opt_prefix.directory?

keg = Keg.new(formula.opt_prefix.resolved_path)
tab = Tab.for_keg(keg)
tab = keg.tab

Check warning on line 214 in Library/Homebrew/install.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/install.rb#L214

Added line #L214 was not covered by tests
unless tab.installed_on_request
tab.installed_on_request = true
tab.write
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/keg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def self.all
:to_path, :hash, :abv, :disk_usage, :file_count, :directory?, :exist?, :/,
:join, :rename, :find

sig { params(path: Pathname).void }
def initialize(path)
path = path.resolved_path if path.to_s.start_with?("#{HOMEBREW_PREFIX}/opt/")
raise "#{path} is not a valid keg" if path.parent.parent.realpath != HOMEBREW_CELLAR.realpath
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/linkage_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def check_dylibs(rebuild_cache:)
end

begin
owner = Keg.for Pathname.new(dylib)
owner = Keg.for(Pathname(dylib))
rescue NotAKegError
@system_dylibs << dylib
rescue Errno::ENOENT
Expand All @@ -159,7 +159,7 @@ def check_dylibs(rebuild_cache:)
@broken_dylibs << dylib
end
else
tap = Tab.for_keg(owner).tap
tap = owner.tab.tap

Check warning on line 162 in Library/Homebrew/linkage_checker.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/linkage_checker.rb#L162

Added line #L162 was not covered by tests
f = if tap.nil? || tap.core_tap?
owner.name
else
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def initialize(formula, oldname, force: false)
@old_cellar = HOMEBREW_CELLAR/oldname
raise MigratorNoOldpathError, oldname unless old_cellar.exist?

@old_tabs = old_cellar.subdirs.map { |d| Tab.for_keg(Keg.new(d)) }
@old_tabs = old_cellar.subdirs.map { |d| Keg.new(d).tab }
@old_tap = old_tabs.first.tap

raise MigratorDifferentTapsError.new(formula, oldname, old_tap) if !force && !from_same_tap_user?
Expand Down Expand Up @@ -373,7 +373,7 @@ def link_oldname_opt
# After migration every `INSTALL_RECEIPT.json` has the wrong path to the formula
# so we must update `INSTALL_RECEIPT`s.
def update_tabs
new_tabs = new_cellar.subdirs.map { |d| Tab.for_keg(Keg.new(d)) }
new_tabs = new_cellar.subdirs.map { |d| Keg.new(d).tab }
new_tabs.each do |tab|
tab.source["path"] = formula.path.to_s if tab.source["path"]
tab.write
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/reinstall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def reinstall_formula(
)
if formula.opt_prefix.directory?
keg = Keg.new(formula.opt_prefix.resolved_path)
tab = Tab.for_keg(keg)
tab = keg.tab
keg_had_linked_opt = true
keg_was_linked = keg.linked?
backup keg
Expand Down
4 changes: 4 additions & 0 deletions Library/Homebrew/tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ def self.from_file_content(content, path)
new(attributes)
end

# Get the {Tab} for the given {Keg},
# or a fake one if the formula is not installed.
#
# @api internal
sig { params(keg: T.any(Keg, Pathname)).returns(T.attached_class) }
def self.for_keg(keg)
path = keg/FILENAME

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/formula_installer_bottle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def temporarily_install_bottle(formula)
expect(formula).to be_latest_version_installed

begin
expect(Tab.for_keg(keg)).to be_poured_from_bottle
expect(keg.tab).to be_poured_from_bottle

yield formula
ensure
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/formula_installer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def temporary_install(formula, **options)

begin
Tab.clear_cache
expect(Tab.for_keg(keg)).not_to be_poured_from_bottle
expect(keg.tab).not_to be_poured_from_bottle

yield formula if block_given?
ensure
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/test/installed_dependents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def setup_test_keg(name, version, &block)
end

def alter_tab(keg)
tab = Tab.for_keg(keg)
tab = keg.tab
yield tab
tab.write
end
Expand Down
Loading

0 comments on commit 2df9869

Please sign in to comment.