Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search: remove remote searching #15209

Merged
merged 1 commit into from Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Library/Homebrew/cmd/install.rb
Expand Up @@ -315,8 +315,8 @@ def install
ohai "Searching for similarly named #{package_types.join(" and ")}..."

# Don't treat formula/cask name as a regex
query = string_or_regex = name
all_formulae, all_casks = Search.search_names(query, string_or_regex, args)
string_or_regex = name
all_formulae, all_casks = Search.search_names(string_or_regex, args)

if all_formulae.any?
ohai "Formulae", Formatter.columns(all_formulae)
Expand Down
7 changes: 3 additions & 4 deletions Library/Homebrew/cmd/search.rb
Expand Up @@ -33,12 +33,11 @@ def search_args
description <<~EOS
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.
The search for <text> is extended online to `homebrew/core` and `homebrew/cask`.
EOS
switch "--formula", "--formulae",
description: "Search online and locally for formulae."
description: "Search for formulae."
switch "--cask", "--casks",
description: "Search online and locally for casks."
description: "Search for casks."
switch "--desc",
description: "Search for formulae with a description matching <text> and casks with " \
"a name or description matching <text>."
Expand Down Expand Up @@ -84,7 +83,7 @@ def search
elsif args.pull_request?
search_pull_requests(query, args)
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)
end

Expand Down
62 changes: 7 additions & 55 deletions Library/Homebrew/search.rb
Expand Up @@ -42,51 +42,6 @@ def search_descriptions(string_or_regex, args, search_type: :desc)
end
end

def search_taps(query, silent: false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

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)
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX)
return begin
Expand Down Expand Up @@ -129,12 +84,11 @@ def search_casks(string_or_regex)
end

cask_tokens = Tap.flat_map(&:cask_tokens).map do |c|
c.sub(%r{^homebrew/cask.*/}, "")
end
next if c.start_with?("homebrew/cask/") && !Homebrew::EnvConfig.no_install_from_api?

if !Tap.fetch("homebrew/cask").installed? && !Homebrew::EnvConfig.no_install_from_api?
cask_tokens += Homebrew::API::Cask.all_casks.keys
end
c.sub(%r{^homebrew/cask.*/}, "")
end.compact
cask_tokens |= Homebrew::API::Cask.all_casks.keys unless Homebrew::EnvConfig.no_install_from_api?

results = search(cask_tokens, string_or_regex)
results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
Expand All @@ -150,19 +104,17 @@ def search_casks(string_or_regex)
end.uniq
end

def search_names(query, string_or_regex, args)
def search_names(string_or_regex, args)
both = !args.formula? && !args.cask?

remote_results = search_taps(query, silent: true)

all_formulae = if args.formula? || both
search_formulae(string_or_regex) + remote_results[:formulae]
search_formulae(string_or_regex)
else
[]
end

all_casks = if args.cask? || both
search_casks(string_or_regex) + remote_results[:casks]
search_casks(string_or_regex)
else
[]
end
Expand Down
42 changes: 0 additions & 42 deletions Library/Homebrew/test/search_spec.rb
Expand Up @@ -4,48 +4,6 @@
require "search"

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
it "correctly parses a regex query" do
expect(described_class.query_regexp("/^query$/")).to eq(/^query$/)
Expand Down
11 changes: 0 additions & 11 deletions Library/Homebrew/test/utils/github_spec.rb
Expand Up @@ -4,17 +4,6 @@
require "utils/github"

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
it "builds a query with the given hash parameters formatted as key:value" do
query = described_class.search_query_string(user: "Homebrew", repo: "brew")
Expand Down
4 changes: 0 additions & 4 deletions Library/Homebrew/utils/github.rb
Expand Up @@ -58,10 +58,6 @@ def self.repository(user, repo)
API.open_rest(url_to("repos", user, repo))
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)
return [] unless tap_remote_repo

Expand Down
8 changes: 4 additions & 4 deletions completions/fish/brew.fish
Expand Up @@ -277,15 +277,15 @@ __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_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 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 desc -d 'Search for formulae with a description matching text and casks with a name or description matching text'
__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 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 macports -d 'Search for text in the given database'
__fish_brew_complete_arg '-S' -l open -d 'Search for only open GitHub pull requests'
Expand Down Expand Up @@ -1336,15 +1336,15 @@ __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_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 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 desc -d 'Search for formulae with a description matching text and casks with a name or description matching text'
__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 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 macports -d 'Search for text in the given database'
__fish_brew_complete_arg 'search' -l open -d 'Search for only open GitHub pull requests'
Expand Down
8 changes: 4 additions & 4 deletions completions/zsh/_brew
Expand Up @@ -360,15 +360,15 @@ _brew___repository() {
_brew__s() {
_arguments \
'(--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]' \
'(--repology --macports --fink --opensuse --fedora --archlinux --ubuntu)--debian[Search for text in the given database]' \
'--debug[Display any debugging information]' \
'(--pull-request)--desc[Search for formulae with a description matching text and casks with a name or description matching text]' \
'--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 --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]' \
'(--repology --fink --opensuse --fedora --archlinux --debian --ubuntu)--macports[Search for text in the given database]' \
'(--closed)--open[Search for only open GitHub pull requests]' \
Expand Down Expand Up @@ -1634,15 +1634,15 @@ _brew_ruby() {
_brew_search() {
_arguments \
'(--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]' \
'(--repology --macports --fink --opensuse --fedora --archlinux --ubuntu)--debian[Search for text in the given database]' \
'--debug[Display any debugging information]' \
'(--pull-request)--desc[Search for formulae with a description matching text and casks with a name or description matching text]' \
'--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 --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]' \
'(--repology --fink --opensuse --fedora --archlinux --debian --ubuntu)--macports[Search for text in the given database]' \
'(--closed)--open[Search for only open GitHub pull requests]' \
Expand Down
5 changes: 2 additions & 3 deletions docs/Manpage.md
Expand Up @@ -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`*
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`:
Search online and locally for formulae.
Search for formulae.
* `--cask`:
Search online and locally for casks.
Search for casks.
* `--desc`:
Search for formulae with a description matching *`text`* and casks with a name or description matching *`text`*.
* `--eval-all`:
Expand Down
6 changes: 3 additions & 3 deletions manpages/brew.1
Expand Up @@ -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
.
.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
\fB\-\-formula\fR
Search online and locally for formulae\.
Search for formulae\.
.
.TP
\fB\-\-cask\fR
Search online and locally for casks\.
Search for casks\.
.
.TP
\fB\-\-desc\fR
Expand Down