brew bundle env: sort and filter output.

- Sort output by key so it's more readable.
- Skip exporting empty values because shell scripts treat them as unset.
- Skip exporting non-Homebrew things that were already set in the old
  environment to avoid massive duplication and higher chances of e.g.
  bad escapes breaking things.
This commit is contained in:
Mike McQuaid 2025-03-25 13:47:44 +00:00
parent fcd9b340a8
commit 4c97a795a9
No known key found for this signature in database

View File

@ -51,6 +51,10 @@ module Homebrew
# Cleanup Homebrew's global environment # Cleanup Homebrew's global environment
HOMEBREW_ENV_CLEANUP.each { |key| ENV.delete(key) } HOMEBREW_ENV_CLEANUP.each { |key| ENV.delete(key) }
# Store the old environment so we can check if things were already set
# before we start mutating it.
old_env = ENV.to_h
# Setup Homebrew's ENV extensions # Setup Homebrew's ENV extensions
ENV.activate_extensions! ENV.activate_extensions!
raise UsageError, "No command to execute was specified!" if args.blank? raise UsageError, "No command to execute was specified!" if args.blank?
@ -141,7 +145,13 @@ module Homebrew
raise "command was not found in your PATH: #{command}" if command.exclude?("/") && which(command).nil? raise "command was not found in your PATH: #{command}" if command.exclude?("/") && which(command).nil?
if subcommand == "env" if subcommand == "env"
ENV.each do |key, value| ENV.sort.each do |key, value|
# No need to export empty values.
next if value.blank?
# Skip exporting non-Homebrew things that were already set in the old environment.
next if !key.start_with?("HOMEBREW_") && old_env.key?(key) && old_env[key] == value
puts "export #{key}=\"#{Utils::Shell.sh_quote(value)}\"" puts "export #{key}=\"#{Utils::Shell.sh_quote(value)}\""
end end
return return