Skip to content

Commit

Permalink
formula: add .keepme files to prevent cleanup
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
MikeMcQuaid committed Jan 23, 2025
1 parent 0a422da commit d6b19ac
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3174,6 +3174,7 @@ def system(cmd, *args)
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?
Expand Down Expand Up @@ -3201,6 +3202,12 @@ def eligible_kegs_for_cleanup(quiet: false)
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)
.filter_map {|line| line if Pathname(line).exist? }
.presence)
opoo "Skipping #{keg} as it needed by #{keepme_refs.join(", ")}" unless quiet
else
eligible_for_cleanup << keg
end
Expand Down

0 comments on commit d6b19ac

Please sign in to comment.