diff --git a/Library/Homebrew/exceptions.rb b/Library/Homebrew/exceptions.rb index 663236cba2..28a97bc82e 100644 --- a/Library/Homebrew/exceptions.rb +++ b/Library/Homebrew/exceptions.rb @@ -65,6 +65,25 @@ class TapFormulaUnavailableError < FormulaUnavailableError end end +class TapFormulaAmbiguityError < RuntimeError + attr_reader :name, :paths, :formulae + + def initialize name, paths + @name = name + @paths = paths + @formulae = paths.map do |path| + path.to_s =~ HOMEBREW_TAP_PATH_REGEX + "#{$1}/#{$2.sub("homebrew-", "")}/#{path.basename(".rb")}" + end + + super <<-EOS.undent + Formulae found in multiple taps: #{formulae.map { |f| "\n * #{f}" }.join} + + Please use the fully-qualified name e.g. #{formulae.first} to refer the formula. + EOS + end +end + class OperationInProgressError < RuntimeError def initialize name message = <<-EOS.undent diff --git a/Library/Homebrew/formulary.rb b/Library/Homebrew/formulary.rb index e59a25cc71..2a0d43f92b 100644 --- a/Library/Homebrew/formulary.rb +++ b/Library/Homebrew/formulary.rb @@ -232,6 +232,13 @@ class Formulary return AliasLoader.new(possible_alias) end + possible_tap_formulae = tap_paths(ref) + if possible_tap_formulae.size > 1 + raise TapFormulaAmbiguityError.new(ref, possible_tap_formulae) + elsif possible_tap_formulae.size == 1 + return FormulaLoader.new(ref, possible_tap_formulae.first) + end + possible_cached_formula = Pathname.new("#{HOMEBREW_CACHE_FORMULA}/#{ref}.rb") if possible_cached_formula.file? return FormulaLoader.new(ref, possible_cached_formula) @@ -243,4 +250,12 @@ class Formulary def self.core_path(name) Pathname.new("#{HOMEBREW_LIBRARY}/Formula/#{name.downcase}.rb") end + + def self.tap_paths(name) + name = name.downcase + Dir["#{HOMEBREW_LIBRARY}/Taps/*/*/"].map do |tap| + Pathname.glob(["#{tap}#{name}.rb", "#{tap}Formula/#{name}.rb", + "#{tap}HomebrewFormula/#{name}.rb"]) + end.flatten.select(&:file?) + end end