Apply the same changes from #6066. Also apply the same logics from `brew upgrade` to preserve `installed_as_dependency`/`installed_on_request` settings when reinstalling formulae.
Library
This directory contains all the code run by the official brew and brew cask commands in Homebrew and all formulae (package descriptions) in taps (repositories containing formulae) in Taps subdirectories.