Skip to content

Commit

Permalink
Merge pull request #19137 from Homebrew/cleanup_improve_pinned
Browse files Browse the repository at this point in the history
cleanup: improve cleanup version detection.
  • Loading branch information
MikeMcQuaid authored Jan 23, 2025
2 parents 9f319bc + fa8ada3 commit d0304b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Library/Homebrew/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ def stale_api_source?(pathname, scrub)
package.tap_git_head != git_head
end

sig { params(formula: Formula).returns(T::Set[String]) }
def excluded_versions_from_cleanup(formula)
@excluded_versions_from_cleanup ||= {}
@excluded_versions_from_cleanup[formula.name] ||= begin
eligible_kegs_for_cleanup = formula.eligible_kegs_for_cleanup(quiet: true)
Set.new((formula.installed_kegs - eligible_kegs_for_cleanup).map { |keg| keg.version.to_s })
end
end

sig { params(pathname: Pathname, scrub: T::Boolean).returns(T::Boolean) }
def stale_formula?(pathname, scrub)
return false unless HOMEBREW_CELLAR.directory?
Expand Down Expand Up @@ -131,6 +140,7 @@ def stale_formula?(pathname, scrub)
nil
end

formula_excluded_versions_from_cleanup = nil
if formula.blank? && formula_name.delete_suffix!("_bottle_manifest")
formula = begin
Formulary.from_rack(HOMEBREW_CELLAR/formula_name)
Expand All @@ -140,6 +150,9 @@ def stale_formula?(pathname, scrub)

return false if formula.blank?

formula_excluded_versions_from_cleanup = excluded_versions_from_cleanup(formula)
return false if formula_excluded_versions_from_cleanup.include?(version.to_s)

# We can't determine an installed rebuild and parsing manifest version cannot be reliably done.
return false unless formula.latest_version_installed?

Expand All @@ -158,6 +171,9 @@ def stale_formula?(pathname, scrub)
return true unless patch_hashes&.include?(Checksum.new(version.to_s))
elsif resource_name && stable && (resource_version = stable.resources[resource_name]&.version)
return true if resource_version != version
elsif (formula_excluded_versions_from_cleanup ||= excluded_versions_from_cleanup(formula).presence) &&
formula_excluded_versions_from_cleanup.include?(version.to_s)
return false
elsif (formula.latest_version_installed? && formula.pkg_version.to_s != version) ||
formula.pkg_version.to_s > version
return true
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/test/cleanup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@
FileUtils.touch testball
FileUtils.touch testball_resource
(HOMEBREW_CELLAR/"testball"/"0.0.1").mkpath
# Create the latest version of testball so the older version is eligible for cleanup.
(HOMEBREW_CELLAR/"testball"/"0.1/bin").mkpath
FileUtils.touch(CoreTap.instance.new_formula_path("testball"))
end

Expand Down

0 comments on commit d0304b4

Please # to comment.