mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
search: remove remote searching
This commit is contained in:
parent
80372686fa
commit
3697825784
@ -315,8 +315,8 @@ module Homebrew
|
||||
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)
|
||||
|
@ -33,12 +33,11 @@ module Homebrew
|
||||
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>."
|
||||
@ -84,7 +83,7 @@ module Homebrew
|
||||
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
|
||||
|
||||
|
@ -42,51 +42,6 @@ module Homebrew
|
||||
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)
|
||||
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX)
|
||||
return begin
|
||||
@ -129,12 +84,11 @@ module Homebrew
|
||||
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)
|
||||
@ -150,19 +104,17 @@ module Homebrew
|
||||
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
|
||||
|
@ -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$/)
|
||||
|
@ -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")
|
||||
|
@ -58,10 +58,6 @@ module GitHub
|
||||
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
|
||||
|
||||
|
@ -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_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'
|
||||
@ -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 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'
|
||||
@ -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_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'
|
||||
@ -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 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'
|
||||
|
@ -360,7 +360,7 @@ _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]' \
|
||||
@ -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]' \
|
||||
'(--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]' \
|
||||
@ -1634,7 +1634,7 @@ _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]' \
|
||||
@ -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]' \
|
||||
'(--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]' \
|
||||
|
@ -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`:
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user