From 23b277bbd7902a10fffe37ecf83639215d34b6c5 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Sun, 1 Oct 2023 13:23:12 -0700 Subject: [PATCH] cli/named_args: better handle name conflicts in #to_paths Before we used to evaluate all named arguments as local paths first. This means that the following could be a name conflict. $ brew edit src If there was a local file or directory named src, it would default to that. Otherwise it would search for a formula/cask with the same name and return that. Now it will only default to the local path if the named argument starts or ends with a slash ('/') or includes a period ('.'). This means that in the event of a name clash with a normal package name it will default to the package instead of the local file. It also fixes an edge case where the following would be interpreted as a tap name. $ brew edit /src --- Library/Homebrew/cli/named_args.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cli/named_args.rb b/Library/Homebrew/cli/named_args.rb index bfa013001b..e26a8d15f0 100644 --- a/Library/Homebrew/cli/named_args.rb +++ b/Library/Homebrew/cli/named_args.rb @@ -221,6 +221,10 @@ module Homebrew to_formulae_to_casks(only: only, method: :resolve) end + LOCAL_PATH_REGEX = %r{^/|[.]|/$}.freeze + TAP_NAME_REGEX = %r{^[^./]+/[^./]+$}.freeze + private_constant :LOCAL_PATH_REGEX, :TAP_NAME_REGEX + # Keep existing paths and try to convert others to tap, formula or cask paths. # If a cask and formula with the same name exist, includes both their paths # unless `only` is specified. @@ -230,9 +234,9 @@ module Homebrew @to_paths[only] ||= Homebrew.with_no_api_env_if_needed(@without_api) do downcased_unique_named.flat_map do |name| path = Pathname(name).expand_path - if only.nil? && File.exist?(name) + if only.nil? && name.match?(LOCAL_PATH_REGEX) && path.exist? path - elsif name.count("/") == 1 && !name.start_with?("./", "/") + elsif name.match?(TAP_NAME_REGEX) tap = Tap.fetch(name) if recurse_tap