formula: add .keepme files to prevent cleanup

Allow the use of `.keepme` files inside a keg to prevent the cleanup.

Rather than having a binary state of "never cleanup if `.keepme` is
present" and "can cleanup without", instead a `.keepme` file is
essentially providing reference counting.

It can contain one or more lines which reference files on disk. If the
file exists, the `.keepme` file will prevent cleanup. If/when it does
not: `brew cleanup` will happily cleanup this keg (providing all other
conditions apply).
This commit is contained in:
Mike McQuaid 2025-01-23 14:29:48 +00:00
parent 0a422daf68
commit aeaeccc3b1
No known key found for this signature in database

View File

@ -3174,6 +3174,7 @@ class Formula
end
end
sig { params(quiet: T::Boolean).returns(T::Array[Keg]) }
def eligible_kegs_for_cleanup(quiet: false)
eligible_for_cleanup = []
if latest_version_installed?
@ -3201,6 +3202,9 @@ class Formula
opoo "Skipping (old) #{keg} due to it being linked" unless quiet
elsif pinned? && keg == Keg.new(@pin.path.resolved_path)
opoo "Skipping (old) #{keg} due to it being pinned" unless quiet
elsif (keepme = keg/".keepme") && keepme.exist? && keepme.readable? &&
(keepme_refs = keepme.readlines.map(&:strip).select { |ref| Pathname(ref).exist? }.presence)
opoo "Skipping #{keg} as it needed by #{keepme_refs.join(", ")}" unless quiet
else
eligible_for_cleanup << keg
end