Merge pull request #2540 from MikeMcQuaid/search-taps-code-search
search: use single HTTP call for tap searches.
This commit is contained in:
commit
b54f5c40a8
@ -16,15 +16,12 @@
|
|||||||
require "formula"
|
require "formula"
|
||||||
require "missing_formula"
|
require "missing_formula"
|
||||||
require "utils"
|
require "utils"
|
||||||
require "thread"
|
|
||||||
require "official_taps"
|
require "official_taps"
|
||||||
require "descriptions"
|
require "descriptions"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
SEARCH_ERROR_QUEUE = Queue.new
|
|
||||||
|
|
||||||
def search
|
def search
|
||||||
if ARGV.empty?
|
if ARGV.empty?
|
||||||
puts Formatter.columns(Formula.full_names)
|
puts Formatter.columns(Formula.full_names)
|
||||||
@ -61,8 +58,8 @@ module Homebrew
|
|||||||
regex = query_regexp(query)
|
regex = query_regexp(query)
|
||||||
local_results = search_formulae(regex)
|
local_results = search_formulae(regex)
|
||||||
puts Formatter.columns(local_results) unless local_results.empty?
|
puts Formatter.columns(local_results) unless local_results.empty?
|
||||||
tap_results = search_taps(regex)
|
tap_results = search_taps(query)
|
||||||
puts Formatter.columns(tap_results) unless tap_results.empty?
|
puts Formatter.columns(tap_results) if tap_results && !tap_results.empty?
|
||||||
|
|
||||||
if $stdout.tty?
|
if $stdout.tty?
|
||||||
count = local_results.length + tap_results.length
|
count = local_results.length + tap_results.length
|
||||||
@ -75,33 +72,24 @@ module Homebrew
|
|||||||
puts reason
|
puts reason
|
||||||
elsif count.zero?
|
elsif count.zero?
|
||||||
puts "No formula found for #{query.inspect}."
|
puts "No formula found for #{query.inspect}."
|
||||||
begin
|
|
||||||
GitHub.print_pull_requests_matching(query)
|
GitHub.print_pull_requests_matching(query)
|
||||||
rescue GitHub::Error => e
|
|
||||||
SEARCH_ERROR_QUEUE << e
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if $stdout.tty?
|
return unless $stdout.tty?
|
||||||
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?]
|
return if ARGV.empty?
|
||||||
bad_regex = metacharacters.any? do |char|
|
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze
|
||||||
|
return unless metacharacters.any? do |char|
|
||||||
ARGV.any? do |arg|
|
ARGV.any? do |arg|
|
||||||
arg.include?(char) && !arg.start_with?("/")
|
arg.include?(char) && !arg.start_with?("/")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if !ARGV.empty? && bad_regex
|
ohai <<-EOS.undent
|
||||||
ohai "Did you mean to perform a regular expression search?"
|
Did you mean to perform a regular expression search?
|
||||||
ohai "Surround your query with /slashes/ to search by regex."
|
Surround your query with /slashes/ to search locally by regex.
|
||||||
|
EOS
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
raise SEARCH_ERROR_QUEUE.pop unless SEARCH_ERROR_QUEUE.empty?
|
|
||||||
end
|
|
||||||
|
|
||||||
SEARCHABLE_TAPS = OFFICIAL_TAPS.map { |tap| ["Homebrew", tap] } +
|
|
||||||
OFFICIAL_CASK_TAPS.map { |tap| ["caskroom", tap] }
|
|
||||||
|
|
||||||
def query_regexp(query)
|
def query_regexp(query)
|
||||||
case query
|
case query
|
||||||
@ -112,55 +100,24 @@ module Homebrew
|
|||||||
odie "#{query} is not a valid regex"
|
odie "#{query} is not a valid regex"
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_taps(regex_or_string)
|
def search_taps(query)
|
||||||
SEARCHABLE_TAPS.map do |user, repo|
|
valid_dirnames = ["Formula", "HomebrewFormula", "Casks", ".", ""].freeze
|
||||||
Thread.new { search_tap(user, repo, regex_or_string) }
|
q = "user:Homebrew%20user:caskroom%20filename:#{query}"
|
||||||
end.inject([]) do |results, t|
|
GitHub.open "https://api.github.com/search/code?q=#{q}" do |json|
|
||||||
results.concat(t.value)
|
json["items"].map do |object|
|
||||||
|
dirname, filename = File.split(object["path"])
|
||||||
|
next unless valid_dirnames.include?(dirname)
|
||||||
|
user = object["repository"]["owner"]["login"]
|
||||||
|
user = user.downcase if user == "Homebrew"
|
||||||
|
repo = object["repository"]["name"].sub(/^homebrew-/, "")
|
||||||
|
tap = Tap.fetch user, repo
|
||||||
|
next if tap.installed?
|
||||||
|
basename = File.basename(filename, ".rb")
|
||||||
|
"#{user}/#{repo}/#{basename}"
|
||||||
|
end.compact
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_tap(user, repo, regex_or_string)
|
|
||||||
regex = regex_or_string.is_a?(String) ? /^#{Regexp.escape(regex_or_string)}$/ : regex_or_string
|
|
||||||
|
|
||||||
if (HOMEBREW_LIBRARY/"Taps/#{user.downcase}/homebrew-#{repo.downcase}").directory? && \
|
|
||||||
user != "caskroom"
|
|
||||||
return []
|
|
||||||
end
|
|
||||||
|
|
||||||
remote_tap_formulae = Hash.new do |cache, key|
|
|
||||||
user, repo = key.split("/", 2)
|
|
||||||
tree = {}
|
|
||||||
|
|
||||||
GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |json|
|
|
||||||
json["tree"].each do |object|
|
|
||||||
next unless object["type"] == "blob"
|
|
||||||
|
|
||||||
subtree, file = File.split(object["path"])
|
|
||||||
|
|
||||||
if File.extname(file) == ".rb"
|
|
||||||
tree[subtree] ||= []
|
|
||||||
tree[subtree] << file
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
paths = tree["Formula"] || tree["HomebrewFormula"] || tree["."] || []
|
|
||||||
paths += tree["Casks"] || []
|
|
||||||
cache[key] = paths.map { |path| File.basename(path, ".rb") }
|
|
||||||
end
|
|
||||||
|
|
||||||
names = remote_tap_formulae["#{user}/#{repo}"]
|
|
||||||
user = user.downcase if user == "Homebrew" # special handling for the Homebrew organization
|
|
||||||
names.select { |name| name =~ regex }.map { |name| "#{user}/#{repo}/#{name}" }
|
|
||||||
rescue GitHub::HTTPNotFoundError
|
|
||||||
opoo "Failed to search tap: #{user}/#{repo}. Please run `brew update`"
|
|
||||||
[]
|
|
||||||
rescue GitHub::Error => e
|
|
||||||
SEARCH_ERROR_QUEUE << e
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
|
|
||||||
def search_formulae(regex)
|
def search_formulae(regex)
|
||||||
aliases = Formula.alias_full_names
|
aliases = Formula.alias_full_names
|
||||||
results = (Formula.full_names+aliases).grep(regex).sort
|
results = (Formula.full_names+aliases).grep(regex).sort
|
||||||
|
@ -1,19 +1,24 @@
|
|||||||
require "cmd/search"
|
require "cmd/search"
|
||||||
|
|
||||||
describe Homebrew do
|
describe Homebrew do
|
||||||
specify "#search_tap" do
|
specify "#search_taps" do
|
||||||
json_response = {
|
json_response = {
|
||||||
"tree" => [
|
"items" => [
|
||||||
{
|
{
|
||||||
"path" => "Formula/not-a-formula.rb",
|
"path" => "Formula/some-formula.rb",
|
||||||
"type" => "blob",
|
"repository" => {
|
||||||
|
"name" => "homebrew-foo",
|
||||||
|
"owner" => {
|
||||||
|
"login" => "Homebrew",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
allow(GitHub).to receive(:open).and_yield(json_response)
|
allow(GitHub).to receive(:open).and_yield(json_response)
|
||||||
|
|
||||||
expect(described_class.search_tap("homebrew", "not-a-tap", "not-a-formula"))
|
expect(described_class.search_taps("some-formula"))
|
||||||
.to eq(["homebrew/not-a-tap/not-a-formula"])
|
.to match(["homebrew/foo/some-formula"])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user