From aeaeccc3b191920cfd8759d477fb58e0970f5e34 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 23 Jan 2025 14:29:48 +0000 Subject: [PATCH] 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). --- Library/Homebrew/formula.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 0071393919..575c2fd4bd 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -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