aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew
diff options
context:
space:
mode:
Diffstat (limited to 'Library/Homebrew')
-rw-r--r--Library/Homebrew/cmd/search.rb26
-rw-r--r--Library/Homebrew/test/cmd/search_remote_tap_spec.rb5
-rw-r--r--Library/Homebrew/utils/github.rb14
3 files changed, 21 insertions, 24 deletions
diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb
index 110e1559e..f71a14ba1 100644
--- a/Library/Homebrew/cmd/search.rb
+++ b/Library/Homebrew/cmd/search.rb
@@ -59,7 +59,7 @@ module Homebrew
local_results = search_formulae(regex)
puts Formatter.columns(local_results) unless local_results.empty?
tap_results = search_taps(query)
- puts Formatter.columns(tap_results) if tap_results && !tap_results.empty?
+ puts Formatter.columns(tap_results) unless tap_results.empty?
if $stdout.tty?
count = local_results.length + tap_results.length
@@ -101,21 +101,15 @@ module Homebrew
end
def search_taps(query)
- valid_dirnames = ["Formula", "HomebrewFormula", "Casks", ".", ""].freeze
- q = "user:Homebrew%20user:caskroom%20filename:#{query}"
- GitHub.open "https://api.github.com/search/code?q=#{q}" do |json|
- 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
+ valid_dirnames = ["Formula", "HomebrewFormula", "Casks", "."].freeze
+ matches = GitHub.search_code("user:Homebrew", "user:caskroom", "filename:#{query}", "extension:rb")
+ [*matches].map do |match|
+ dirname, filename = File.split(match["path"])
+ next unless valid_dirnames.include?(dirname)
+ tap = Tap.fetch(match["repository"]["full_name"])
+ next if tap.installed?
+ "#{tap.name}/#{File.basename(filename, ".rb")}"
+ end.compact
end
def search_formulae(regex)
diff --git a/Library/Homebrew/test/cmd/search_remote_tap_spec.rb b/Library/Homebrew/test/cmd/search_remote_tap_spec.rb
index be7c20865..b0beb122c 100644
--- a/Library/Homebrew/test/cmd/search_remote_tap_spec.rb
+++ b/Library/Homebrew/test/cmd/search_remote_tap_spec.rb
@@ -7,10 +7,7 @@ describe Homebrew do
{
"path" => "Formula/some-formula.rb",
"repository" => {
- "name" => "homebrew-foo",
- "owner" => {
- "login" => "Homebrew",
- },
+ "full_name" => "Homebrew/homebrew-foo",
},
},
],
diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb
index a5ed5394a..2daa23982 100644
--- a/Library/Homebrew/utils/github.rb
+++ b/Library/Homebrew/utils/github.rb
@@ -4,7 +4,7 @@ require "tempfile"
module GitHub
module_function
- ISSUES_URI = URI.parse("https://api.github.com/search/issues")
+ API_URL = "https://api.github.com".freeze
CREATE_GIST_SCOPES = ["gist"].freeze
CREATE_ISSUE_SCOPES = ["public_repo"].freeze
@@ -228,13 +228,19 @@ module GitHub
end
def issues_matching(query, qualifiers = {})
- uri = ISSUES_URI.dup
+ uri = URI.parse("#{API_URL}/search/issues")
uri.query = build_query_string(query, qualifiers)
open(uri) { |json| json["items"] }
end
def repository(user, repo)
- open(URI.parse("https://api.github.com/repos/#{user}/#{repo}")) { |j| j }
+ open(URI.parse("#{API_URL}/repos/#{user}/#{repo}")) { |j| j }
+ end
+
+ def search_code(*params)
+ uri = URI.parse("#{API_URL}/search/code")
+ uri.query = "q=#{uri_escape(params.join(" "))}"
+ open(uri) { |json| json["items"] }
end
def build_query_string(query, qualifiers)
@@ -286,7 +292,7 @@ module GitHub
end
def private_repo?(user, repo)
- uri = URI.parse("https://api.github.com/repos/#{user}/#{repo}")
+ uri = URI.parse("#{API_URL}/repos/#{user}/#{repo}")
open(uri) { |json| json["private"] }
end
end