search: remove remote searching

This commit is contained in:
Bo Anderson 2023-04-12 00:00:48 +01:00
parent 80372686fa
commit 3697825784
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
10 changed files with 25 additions and 132 deletions

View File

@ -315,8 +315,8 @@ module Homebrew
ohai "Searching for similarly named #{package_types.join(" and ")}..." ohai "Searching for similarly named #{package_types.join(" and ")}..."
# Don't treat formula/cask name as a regex # Don't treat formula/cask name as a regex
query = string_or_regex = name string_or_regex = name
all_formulae, all_casks = Search.search_names(query, string_or_regex, args) all_formulae, all_casks = Search.search_names(string_or_regex, args)
if all_formulae.any? if all_formulae.any?
ohai "Formulae", Formatter.columns(all_formulae) ohai "Formulae", Formatter.columns(all_formulae)

View File

@ -33,12 +33,11 @@ module Homebrew
description <<~EOS description <<~EOS
Perform a substring search of cask tokens and formula names for <text>. If <text> Perform a substring search of cask tokens and formula names for <text>. If <text>
is flanked by slashes, it is interpreted as a regular expression. is flanked by slashes, it is interpreted as a regular expression.
The search for <text> is extended online to `homebrew/core` and `homebrew/cask`.
EOS EOS
switch "--formula", "--formulae", switch "--formula", "--formulae",
description: "Search online and locally for formulae." description: "Search for formulae."
switch "--cask", "--casks", switch "--cask", "--casks",
description: "Search online and locally for casks." description: "Search for casks."
switch "--desc", switch "--desc",
description: "Search for formulae with a description matching <text> and casks with " \ description: "Search for formulae with a description matching <text> and casks with " \
"a name or description matching <text>." "a name or description matching <text>."
@ -84,7 +83,7 @@ module Homebrew
elsif args.pull_request? elsif args.pull_request?
search_pull_requests(query, args) search_pull_requests(query, args)
else else
formulae, casks = Search.search_names(query, string_or_regex, args) formulae, casks = Search.search_names(string_or_regex, args)
print_results(formulae, casks, query) print_results(formulae, casks, query)
end end

View File

@ -42,51 +42,6 @@ module Homebrew
end end
end end
def search_taps(query, silent: false)
if query.match?(Regexp.union(HOMEBREW_TAP_FORMULA_REGEX, HOMEBREW_TAP_CASK_REGEX))
_, _, query = query.split("/", 3)
end
results = { formulae: [], casks: [] }
return results if Homebrew::EnvConfig.no_github_api?
unless silent
# Use stderr to avoid breaking parsed output
$stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue)
end
matches = begin
GitHub.search_code(
user: "Homebrew",
path: ["Formula", "Casks", "."],
filename: query,
extension: "rb",
)
rescue GitHub::API::Error => e
opoo "Error searching on GitHub: #{e}\n"
nil
end
return results if matches.blank?
matches.each do |match|
name = File.basename(match["path"], ".rb")
tap = Tap.fetch(match["repository"]["full_name"])
full_name = "#{tap.name}/#{name}"
next if tap.installed?
if match["path"].start_with?("Casks/")
results[:casks] = [*results[:casks], full_name].sort
else
results[:formulae] = [*results[:formulae], full_name].sort
end
end
results
end
def search_formulae(string_or_regex) def search_formulae(string_or_regex)
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX) if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX)
return begin return begin
@ -129,12 +84,11 @@ module Homebrew
end end
cask_tokens = Tap.flat_map(&:cask_tokens).map do |c| cask_tokens = Tap.flat_map(&:cask_tokens).map do |c|
c.sub(%r{^homebrew/cask.*/}, "") next if c.start_with?("homebrew/cask/") && !Homebrew::EnvConfig.no_install_from_api?
end
if !Tap.fetch("homebrew/cask").installed? && !Homebrew::EnvConfig.no_install_from_api? c.sub(%r{^homebrew/cask.*/}, "")
cask_tokens += Homebrew::API::Cask.all_casks.keys end.compact
end cask_tokens |= Homebrew::API::Cask.all_casks.keys unless Homebrew::EnvConfig.no_install_from_api?
results = search(cask_tokens, string_or_regex) results = search(cask_tokens, string_or_regex)
results += DidYouMean::SpellChecker.new(dictionary: cask_tokens) results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
@ -150,19 +104,17 @@ module Homebrew
end.uniq end.uniq
end end
def search_names(query, string_or_regex, args) def search_names(string_or_regex, args)
both = !args.formula? && !args.cask? both = !args.formula? && !args.cask?
remote_results = search_taps(query, silent: true)
all_formulae = if args.formula? || both all_formulae = if args.formula? || both
search_formulae(string_or_regex) + remote_results[:formulae] search_formulae(string_or_regex)
else else
[] []
end end
all_casks = if args.cask? || both all_casks = if args.cask? || both
search_casks(string_or_regex) + remote_results[:casks] search_casks(string_or_regex)
else else
[] []
end end

View File

@ -4,48 +4,6 @@
require "search" require "search"
describe Homebrew::Search do describe Homebrew::Search do
describe "#search_taps" do
before do
ENV.delete("HOMEBREW_NO_GITHUB_API")
end
it "does not raise if `HOMEBREW_NO_GITHUB_API` is set" do
ENV["HOMEBREW_NO_GITHUB_API"] = "1"
expect(described_class.search_taps("some-formula")).to match(formulae: [], casks: [])
end
it "does not raise if the network fails" do
allow(GitHub::API).to receive(:open_rest).and_raise(GitHub::API::Error)
expect(described_class.search_taps("some-formula"))
.to match(formulae: [], casks: [])
end
it "returns Formulae and Casks separately" do
json_response = {
"items" => [
{
"path" => "Formula/some-formula.rb",
"repository" => {
"full_name" => "Homebrew/homebrew-foo",
},
},
{
"path" => "Casks/some-cask.rb",
"repository" => {
"full_name" => "Homebrew/homebrew-bar",
},
},
],
}
allow(GitHub::API).to receive(:open_rest).and_return(json_response)
expect(described_class.search_taps("some-formula"))
.to match(formulae: ["homebrew/foo/some-formula"], casks: ["homebrew/bar/some-cask"])
end
end
describe "#query_regexp" do describe "#query_regexp" do
it "correctly parses a regex query" do it "correctly parses a regex query" do
expect(described_class.query_regexp("/^query$/")).to eq(/^query$/) expect(described_class.query_regexp("/^query$/")).to eq(/^query$/)

View File

@ -4,17 +4,6 @@
require "utils/github" require "utils/github"
describe GitHub do describe GitHub do
describe "::search_code", :needs_network do
it "queries GitHub code with the passed parameters" do
results = described_class.search_code(repo: "Homebrew/brew", path: "/",
filename: "readme", extension: "md")
expect(results.count).to eq(1)
expect(results.first["name"]).to eq("README.md")
expect(results.first["path"]).to eq("README.md")
end
end
describe "::search_query_string" do describe "::search_query_string" do
it "builds a query with the given hash parameters formatted as key:value" do it "builds a query with the given hash parameters formatted as key:value" do
query = described_class.search_query_string(user: "Homebrew", repo: "brew") query = described_class.search_query_string(user: "Homebrew", repo: "brew")

View File

@ -58,10 +58,6 @@ module GitHub
API.open_rest(url_to("repos", user, repo)) API.open_rest(url_to("repos", user, repo))
end end
def self.search_code(repo: nil, user: "Homebrew", path: ["Formula", "Casks", "."], filename: nil, extension: "rb")
search_results_items("code", user: user, path: path, filename: filename, extension: extension, repo: repo)
end
def self.issues_for_formula(name, tap: CoreTap.instance, tap_remote_repo: tap&.full_name, state: nil) def self.issues_for_formula(name, tap: CoreTap.instance, tap_remote_repo: tap&.full_name, state: nil)
return [] unless tap_remote_repo return [] unless tap_remote_repo

View File

@ -277,7 +277,7 @@ __fish_brew_complete_arg '--repository' -a '(__fish_brew_suggest_taps_installed)
__fish_brew_complete_cmd '-S' 'Perform a substring search of cask tokens and formula names for text' __fish_brew_complete_cmd '-S' 'Perform a substring search of cask tokens and formula names for text'
__fish_brew_complete_arg '-S' -l archlinux -d 'Search for text in the given database' __fish_brew_complete_arg '-S' -l archlinux -d 'Search for text in the given database'
__fish_brew_complete_arg '-S' -l cask -d 'Search online and locally for casks' __fish_brew_complete_arg '-S' -l cask -d 'Search for casks'
__fish_brew_complete_arg '-S' -l closed -d 'Search for only closed GitHub pull requests' __fish_brew_complete_arg '-S' -l closed -d 'Search for only closed GitHub pull requests'
__fish_brew_complete_arg '-S' -l debian -d 'Search for text in the given database' __fish_brew_complete_arg '-S' -l debian -d 'Search for text in the given database'
__fish_brew_complete_arg '-S' -l debug -d 'Display any debugging information' __fish_brew_complete_arg '-S' -l debug -d 'Display any debugging information'
@ -285,7 +285,7 @@ __fish_brew_complete_arg '-S' -l desc -d 'Search for formulae with a description
__fish_brew_complete_arg '-S' -l eval-all -d 'Evaluate all available formulae and casks, whether installed or not, to search their descriptions. Implied if `HOMEBREW_EVAL_ALL` is set' __fish_brew_complete_arg '-S' -l eval-all -d 'Evaluate all available formulae and casks, whether installed or not, to search their descriptions. Implied if `HOMEBREW_EVAL_ALL` is set'
__fish_brew_complete_arg '-S' -l fedora -d 'Search for text in the given database' __fish_brew_complete_arg '-S' -l fedora -d 'Search for text in the given database'
__fish_brew_complete_arg '-S' -l fink -d 'Search for text in the given database' __fish_brew_complete_arg '-S' -l fink -d 'Search for text in the given database'
__fish_brew_complete_arg '-S' -l formula -d 'Search online and locally for formulae' __fish_brew_complete_arg '-S' -l formula -d 'Search for formulae'
__fish_brew_complete_arg '-S' -l help -d 'Show this message' __fish_brew_complete_arg '-S' -l help -d 'Show this message'
__fish_brew_complete_arg '-S' -l macports -d 'Search for text in the given database' __fish_brew_complete_arg '-S' -l macports -d 'Search for text in the given database'
__fish_brew_complete_arg '-S' -l open -d 'Search for only open GitHub pull requests' __fish_brew_complete_arg '-S' -l open -d 'Search for only open GitHub pull requests'
@ -1336,7 +1336,7 @@ __fish_brew_complete_arg 'ruby' -l r -d 'Load a library using `require`'
__fish_brew_complete_cmd 'search' 'Perform a substring search of cask tokens and formula names for text' __fish_brew_complete_cmd 'search' 'Perform a substring search of cask tokens and formula names for text'
__fish_brew_complete_arg 'search' -l archlinux -d 'Search for text in the given database' __fish_brew_complete_arg 'search' -l archlinux -d 'Search for text in the given database'
__fish_brew_complete_arg 'search' -l cask -d 'Search online and locally for casks' __fish_brew_complete_arg 'search' -l cask -d 'Search for casks'
__fish_brew_complete_arg 'search' -l closed -d 'Search for only closed GitHub pull requests' __fish_brew_complete_arg 'search' -l closed -d 'Search for only closed GitHub pull requests'
__fish_brew_complete_arg 'search' -l debian -d 'Search for text in the given database' __fish_brew_complete_arg 'search' -l debian -d 'Search for text in the given database'
__fish_brew_complete_arg 'search' -l debug -d 'Display any debugging information' __fish_brew_complete_arg 'search' -l debug -d 'Display any debugging information'
@ -1344,7 +1344,7 @@ __fish_brew_complete_arg 'search' -l desc -d 'Search for formulae with a descrip
__fish_brew_complete_arg 'search' -l eval-all -d 'Evaluate all available formulae and casks, whether installed or not, to search their descriptions. Implied if `HOMEBREW_EVAL_ALL` is set' __fish_brew_complete_arg 'search' -l eval-all -d 'Evaluate all available formulae and casks, whether installed or not, to search their descriptions. Implied if `HOMEBREW_EVAL_ALL` is set'
__fish_brew_complete_arg 'search' -l fedora -d 'Search for text in the given database' __fish_brew_complete_arg 'search' -l fedora -d 'Search for text in the given database'
__fish_brew_complete_arg 'search' -l fink -d 'Search for text in the given database' __fish_brew_complete_arg 'search' -l fink -d 'Search for text in the given database'
__fish_brew_complete_arg 'search' -l formula -d 'Search online and locally for formulae' __fish_brew_complete_arg 'search' -l formula -d 'Search for formulae'
__fish_brew_complete_arg 'search' -l help -d 'Show this message' __fish_brew_complete_arg 'search' -l help -d 'Show this message'
__fish_brew_complete_arg 'search' -l macports -d 'Search for text in the given database' __fish_brew_complete_arg 'search' -l macports -d 'Search for text in the given database'
__fish_brew_complete_arg 'search' -l open -d 'Search for only open GitHub pull requests' __fish_brew_complete_arg 'search' -l open -d 'Search for only open GitHub pull requests'

View File

@ -360,7 +360,7 @@ _brew___repository() {
_brew__s() { _brew__s() {
_arguments \ _arguments \
'(--repology --macports --fink --opensuse --fedora --debian --ubuntu)--archlinux[Search for text in the given database]' \ '(--repology --macports --fink --opensuse --fedora --debian --ubuntu)--archlinux[Search for text in the given database]' \
'--cask[Search online and locally for casks]' \ '--cask[Search for casks]' \
'(--open)--closed[Search for only closed GitHub pull requests]' \ '(--open)--closed[Search for only closed GitHub pull requests]' \
'(--repology --macports --fink --opensuse --fedora --archlinux --ubuntu)--debian[Search for text in the given database]' \ '(--repology --macports --fink --opensuse --fedora --archlinux --ubuntu)--debian[Search for text in the given database]' \
'--debug[Display any debugging information]' \ '--debug[Display any debugging information]' \
@ -368,7 +368,7 @@ _brew__s() {
'--eval-all[Evaluate all available formulae and casks, whether installed or not, to search their descriptions. Implied if `HOMEBREW_EVAL_ALL` is set]' \ '--eval-all[Evaluate all available formulae and casks, whether installed or not, to search their descriptions. Implied if `HOMEBREW_EVAL_ALL` is set]' \
'(--repology --macports --fink --opensuse --archlinux --debian --ubuntu)--fedora[Search for text in the given database]' \ '(--repology --macports --fink --opensuse --archlinux --debian --ubuntu)--fedora[Search for text in the given database]' \
'(--repology --macports --opensuse --fedora --archlinux --debian --ubuntu)--fink[Search for text in the given database]' \ '(--repology --macports --opensuse --fedora --archlinux --debian --ubuntu)--fink[Search for text in the given database]' \
'--formula[Search online and locally for formulae]' \ '--formula[Search for formulae]' \
'--help[Show this message]' \ '--help[Show this message]' \
'(--repology --fink --opensuse --fedora --archlinux --debian --ubuntu)--macports[Search for text in the given database]' \ '(--repology --fink --opensuse --fedora --archlinux --debian --ubuntu)--macports[Search for text in the given database]' \
'(--closed)--open[Search for only open GitHub pull requests]' \ '(--closed)--open[Search for only open GitHub pull requests]' \
@ -1634,7 +1634,7 @@ _brew_ruby() {
_brew_search() { _brew_search() {
_arguments \ _arguments \
'(--repology --macports --fink --opensuse --fedora --debian --ubuntu)--archlinux[Search for text in the given database]' \ '(--repology --macports --fink --opensuse --fedora --debian --ubuntu)--archlinux[Search for text in the given database]' \
'--cask[Search online and locally for casks]' \ '--cask[Search for casks]' \
'(--open)--closed[Search for only closed GitHub pull requests]' \ '(--open)--closed[Search for only closed GitHub pull requests]' \
'(--repology --macports --fink --opensuse --fedora --archlinux --ubuntu)--debian[Search for text in the given database]' \ '(--repology --macports --fink --opensuse --fedora --archlinux --ubuntu)--debian[Search for text in the given database]' \
'--debug[Display any debugging information]' \ '--debug[Display any debugging information]' \
@ -1642,7 +1642,7 @@ _brew_search() {
'--eval-all[Evaluate all available formulae and casks, whether installed or not, to search their descriptions. Implied if `HOMEBREW_EVAL_ALL` is set]' \ '--eval-all[Evaluate all available formulae and casks, whether installed or not, to search their descriptions. Implied if `HOMEBREW_EVAL_ALL` is set]' \
'(--repology --macports --fink --opensuse --archlinux --debian --ubuntu)--fedora[Search for text in the given database]' \ '(--repology --macports --fink --opensuse --archlinux --debian --ubuntu)--fedora[Search for text in the given database]' \
'(--repology --macports --opensuse --fedora --archlinux --debian --ubuntu)--fink[Search for text in the given database]' \ '(--repology --macports --opensuse --fedora --archlinux --debian --ubuntu)--fink[Search for text in the given database]' \
'--formula[Search online and locally for formulae]' \ '--formula[Search for formulae]' \
'--help[Show this message]' \ '--help[Show this message]' \
'(--repology --fink --opensuse --fedora --archlinux --debian --ubuntu)--macports[Search for text in the given database]' \ '(--repology --fink --opensuse --fedora --archlinux --debian --ubuntu)--macports[Search for text in the given database]' \
'(--closed)--open[Search for only open GitHub pull requests]' \ '(--closed)--open[Search for only open GitHub pull requests]' \

View File

@ -602,12 +602,11 @@ reinstalled formulae or, every 30 days, for all formulae.
Perform a substring search of cask tokens and formula names for *`text`*. If *`text`* Perform a substring search of cask tokens and formula names for *`text`*. If *`text`*
is flanked by slashes, it is interpreted as a regular expression. is flanked by slashes, it is interpreted as a regular expression.
The search for *`text`* is extended online to `homebrew/core` and `homebrew/cask`.
* `--formula`: * `--formula`:
Search online and locally for formulae. Search for formulae.
* `--cask`: * `--cask`:
Search online and locally for casks. Search for casks.
* `--desc`: * `--desc`:
Search for formulae with a description matching *`text`* and casks with a name or description matching *`text`*. Search for formulae with a description matching *`text`* and casks with a name or description matching *`text`*.
* `--eval-all`: * `--eval-all`:

View File

@ -836,15 +836,15 @@ Skip installing cask dependencies\.
For use with \fBbrew reinstall \-\-cask\fR\. Remove all files associated with a cask\. \fIMay remove files which are shared between applications\.\fR For use with \fBbrew reinstall \-\-cask\fR\. Remove all files associated with a cask\. \fIMay remove files which are shared between applications\.\fR
. .
.SS "\fBsearch\fR, \fB\-S\fR [\fIoptions\fR] \fItext\fR|\fB/\fR\fIregex\fR\fB/\fR [\.\.\.]" .SS "\fBsearch\fR, \fB\-S\fR [\fIoptions\fR] \fItext\fR|\fB/\fR\fIregex\fR\fB/\fR [\.\.\.]"
Perform a substring search of cask tokens and formula names for \fItext\fR\. If \fItext\fR is flanked by slashes, it is interpreted as a regular expression\. The search for \fItext\fR is extended online to \fBhomebrew/core\fR and \fBhomebrew/cask\fR\. Perform a substring search of cask tokens and formula names for \fItext\fR\. If \fItext\fR is flanked by slashes, it is interpreted as a regular expression\.
. .
.TP .TP
\fB\-\-formula\fR \fB\-\-formula\fR
Search online and locally for formulae\. Search for formulae\.
. .
.TP .TP
\fB\-\-cask\fR \fB\-\-cask\fR
Search online and locally for casks\. Search for casks\.
. .
.TP .TP
\fB\-\-desc\fR \fB\-\-desc\fR