Add flag --overwrite to brew install to govern the keg-linking step

Allows you to avoid the `Keg::ConflictError` recommending that you invoke `brew link --overwrite` in scenarios when you know that that's how you'd proceed anyway.
This commit is contained in:
Bob Lail 2022-01-10 10:05:44 -06:00
parent 33d765150a
commit 9b678c365b
3 changed files with 13 additions and 3 deletions

View File

@ -22,6 +22,7 @@ module Homebrew
sig { returns(CLI::Parser) } sig { returns(CLI::Parser) }
def install_args def install_args
# rubocop:disable Metrics/BlockLength
Homebrew::CLI::Parser.new do Homebrew::CLI::Parser.new do
description <<~EOS description <<~EOS
Install a <formula> or <cask>. Additional options specific to a <formula> may be Install a <formula> or <cask>. Additional options specific to a <formula> may be
@ -112,6 +113,9 @@ module Homebrew
[:switch, "-g", "--git", { [:switch, "-g", "--git", {
description: "Create a Git repository, useful for creating patches to the software.", description: "Create a Git repository, useful for creating patches to the software.",
}], }],
[:switch, "--overwrite", {
description: "Delete files that already exist in the prefix while linking.",
}],
].each do |*args, **options| ].each do |*args, **options|
send(*args, **options) send(*args, **options)
conflicts "--cask", args.last conflicts "--cask", args.last
@ -132,6 +136,7 @@ module Homebrew
named_args [:formula, :cask], min: 1 named_args [:formula, :cask], min: 1
end end
# rubocop:enable Metrics/BlockLength
end end
def install def install
@ -226,6 +231,7 @@ module Homebrew
interactive: args.interactive?, interactive: args.interactive?,
keep_tmp: args.keep_tmp?, keep_tmp: args.keep_tmp?,
force: args.force?, force: args.force?,
overwrite: args.overwrite?,
debug: args.debug?, debug: args.debug?,
quiet: args.quiet?, quiet: args.quiet?,
verbose: args.verbose?, verbose: args.verbose?,

View File

@ -38,7 +38,7 @@ class FormulaInstaller
attr_predicate :installed_as_dependency?, :installed_on_request? attr_predicate :installed_as_dependency?, :installed_on_request?
attr_predicate :show_summary_heading?, :show_header? attr_predicate :show_summary_heading?, :show_header?
attr_predicate :force_bottle?, :ignore_deps?, :only_deps?, :interactive?, :git?, :force?, :keep_tmp? attr_predicate :force_bottle?, :ignore_deps?, :only_deps?, :interactive?, :git?, :force?, :overwrite?, :keep_tmp?
attr_predicate :verbose?, :debug?, :quiet? attr_predicate :verbose?, :debug?, :quiet?
# TODO: Remove when removed from `test-bot`. # TODO: Remove when removed from `test-bot`.
@ -64,6 +64,7 @@ class FormulaInstaller
cc: nil, cc: nil,
options: Options.new, options: Options.new,
force: false, force: false,
overwrite: false,
debug: false, debug: false,
quiet: false, quiet: false,
verbose: false verbose: false
@ -71,6 +72,7 @@ class FormulaInstaller
@formula = formula @formula = formula
@env = env @env = env
@force = force @force = force
@overwrite = overwrite
@keep_tmp = keep_tmp @keep_tmp = keep_tmp
@link_keg = !formula.keg_only? || link_keg @link_keg = !formula.keg_only? || link_keg
@show_header = show_header @show_header = show_header
@ -951,7 +953,7 @@ class FormulaInstaller
unless link_keg unless link_keg
begin begin
keg.optlink(verbose: verbose?) keg.optlink(verbose: verbose?, overwrite: overwrite?)
rescue Keg::LinkError => e rescue Keg::LinkError => e
ofail "Failed to create #{formula.opt_prefix}" ofail "Failed to create #{formula.opt_prefix}"
puts "Things that depend on #{formula.full_name} will probably not build." puts "Things that depend on #{formula.full_name} will probably not build."
@ -982,7 +984,7 @@ class FormulaInstaller
backup_dir = HOMEBREW_CACHE/"Backup" backup_dir = HOMEBREW_CACHE/"Backup"
begin begin
keg.link(verbose: verbose?) keg.link(verbose: verbose?, overwrite: overwrite?)
rescue Keg::ConflictError => e rescue Keg::ConflictError => e
conflict_file = e.dst conflict_file = e.dst
if formula.link_overwrite?(conflict_file) && !link_overwrite_backup.key?(conflict_file) if formula.link_overwrite?(conflict_file) && !link_overwrite_backup.key?(conflict_file)

View File

@ -268,6 +268,7 @@ module Homebrew
interactive: false, interactive: false,
keep_tmp: false, keep_tmp: false,
force: false, force: false,
overwrite: false,
debug: false, debug: false,
quiet: false, quiet: false,
verbose: false verbose: false
@ -291,6 +292,7 @@ module Homebrew
interactive: interactive, interactive: interactive,
keep_tmp: keep_tmp, keep_tmp: keep_tmp,
force: force, force: force,
overwrite: overwrite,
debug: debug, debug: debug,
quiet: quiet, quiet: quiet,
verbose: verbose, verbose: verbose,