Skip to content

Commit 7cc1b16

Browse files
authored
Merge pull request #22352 from Homebrew/omit-alias-completions
Omit aliases from completions
2 parents 9531a0c + 25bfa00 commit 7cc1b16

10 files changed

Lines changed: 198 additions & 1164 deletions

File tree

Library/Homebrew/commands.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def self.find_commands(path)
190190
def self.rebuild_internal_commands_completion_list
191191
require "completions"
192192

193-
cmds = internal_commands + internal_developer_commands + internal_commands_aliases
193+
cmds = internal_commands + internal_developer_commands
194194
cmds.reject! { |cmd| Homebrew::Completions::COMPLETIONS_EXCLUSION_LIST.include? cmd }
195195

196196
file = HOMEBREW_REPOSITORY/"completions/internal_commands_list.txt"
@@ -204,7 +204,7 @@ def self.rebuild_commands_completion_list
204204
# Ensure that the cache exists so we can build the commands list
205205
HOMEBREW_CACHE.mkpath
206206

207-
cmds = commands(aliases: true) - Homebrew::Completions::COMPLETIONS_EXCLUSION_LIST
207+
cmds = commands - Homebrew::Completions::COMPLETIONS_EXCLUSION_LIST
208208

209209
all_commands_file = HOMEBREW_CACHE/"all_commands_list.txt"
210210
external_commands_file = HOMEBREW_CACHE/"external_commands_list.txt"

Library/Homebrew/completions.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def self.show_completions_message_if_needed
124124

125125
sig { void }
126126
def self.update_shell_completions!
127-
commands = Commands.commands(external: false, aliases: true).sort
127+
commands = Commands.commands(external: false).sort
128128

129129
puts "Writing completions to #{COMPLETIONS_DIR}"
130130

@@ -283,7 +283,12 @@ def self.generate_bash_subcommand_completion(command)
283283

284284
sig { params(commands: T::Array[String]).returns(String) }
285285
def self.generate_bash_completion_file(commands)
286+
commands -= Commands.internal_commands_aliases
287+
286288
variables = Variables.new(
289+
aliases: Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.map do |alias_cmd, command|
290+
"#{alias_cmd}) echo \"#{command}\" ;;"
291+
end,
287292
completion_functions: commands.filter_map do |command|
288293
generate_bash_subcommand_completion command
289294
end,
@@ -449,6 +454,8 @@ def self.generate_zsh_option_exclusions(command, option)
449454

450455
sig { params(commands: T::Array[String]).returns(String) }
451456
def self.generate_zsh_completion_file(commands)
457+
commands -= Commands.internal_commands_aliases
458+
452459
variables = Variables.new(
453460
aliases: Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.filter_map do |alias_cmd, command|
454461
alias_cmd = "'#{alias_cmd}'" if alias_cmd.start_with? "-"
@@ -482,7 +489,8 @@ def self.generate_fish_subcommand_completion(command)
482489
return generate_fish_nested_subcommand_completion(command, subcommands) if subcommands.present?
483490

484491
command_description = format_description Commands.command_description(command, short: true).to_s, fish: true
485-
lines = if COMPLETIONS_EXCLUSION_LIST.include?(command)
492+
lines = if COMPLETIONS_EXCLUSION_LIST.include?(command) ||
493+
Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key?(command)
486494
[]
487495
else
488496
["__fish_brew_complete_cmd '#{command}' '#{command_description}'"]
@@ -571,7 +579,8 @@ def self.generate_fish_named_args(command, types, subcommand: nil)
571579
sig { params(command: String, subcommands: T::Array[Homebrew::CLI::Parser::Subcommand]).returns(String) }
572580
def self.generate_fish_nested_subcommand_completion(command, subcommands)
573581
command_description = format_description Commands.command_description(command, short: true).to_s, fish: true
574-
lines = if COMPLETIONS_EXCLUSION_LIST.include?(command)
582+
lines = if COMPLETIONS_EXCLUSION_LIST.include?(command) ||
583+
Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key?(command)
575584
[]
576585
else
577586
["__fish_brew_complete_cmd '#{command}' '#{command_description}'"]
@@ -615,7 +624,12 @@ def self.generate_fish_nested_subcommand_completion(command, subcommands)
615624

616625
sig { params(commands: T::Array[String]).returns(String) }
617626
def self.generate_fish_completion_file(commands)
627+
commands -= Commands.internal_commands_aliases
628+
618629
variables = Variables.new(
630+
aliases: Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.map do |alias_cmd, command|
631+
" case '#{alias_cmd}'\n echo '#{command}'"
632+
end,
619633
completion_functions: commands.filter_map do |command|
620634
generate_fish_subcommand_completion command
621635
end,

Library/Homebrew/completions/bash.erb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ __brew_complete_commands() {
113113

114114
local cur="${COMP_WORDS[COMP_CWORD]}"
115115
local cmds
116-
local -a cmd_aliases
117116

118117
if [[ -n ${__HOMEBREW_COMMANDS} ]]
119118
then
@@ -125,9 +124,11 @@ __brew_complete_commands() {
125124
then
126125
cmds="$(< "${HOMEBREW_REPOSITORY}/completions/internal_commands_list.txt")"
127126
fi
128-
while read -r alias; do cmd_aliases+=("${alias}"); done < <(compgen -W "$(__brew_list_aliases)")
129-
[[ -n ${cmd_aliases[*]+"${cmd_aliases[*]}"} ]] && cmds+=" ${cmd_aliases[*]} alias unalias"
130-
while read -r line; do COMPREPLY+=("${line}"); done < <(compgen -W "${cmds}" -- "${cur}")
127+
while read -r line
128+
do
129+
[[ $(__brew_internal_command_alias "${line}") == "${line}" ]] || continue
130+
COMPREPLY+=("${line}")
131+
done < <(compgen -W "${cmds}" -- "${cur}")
131132
export __HOMEBREW_COMMANDS=${cmds}
132133
}
133134

@@ -136,6 +137,13 @@ __brew_complete_files() {
136137
command -v compopt &> /dev/null && compopt -o default
137138
}
138139

140+
__brew_internal_command_alias() {
141+
case "$1" in
142+
<%= aliases.join("\n ") + "\n" %>
143+
*) echo "$1" ;;
144+
esac
145+
}
146+
139147
# https://github.com/Homebrew/homebrew-aliases
140148
__brew_list_aliases() {
141149
local aliases_dir="${HOME}/.brew-aliases"
@@ -186,6 +194,8 @@ _brew() {
186194
return
187195
fi
188196

197+
cmd="$(__brew_internal_command_alias "${cmd}")"
198+
189199
# subcommands have their own completion functions
190200
case "${cmd}" in
191201
<%= function_mappings.join("\n ").concat("\n") %>

Library/Homebrew/completions/fish.erb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ function __fish_brew_args -d "Returns a list of all arguments given to brew"
3838
end
3939
end
4040

41+
function __fish_brew_expand_alias -a cmd -d "Expands a Homebrew command alias"
42+
switch $cmd
43+
<%= aliases.join("\n") + "\n" %>
44+
case '*'
45+
echo $cmd
46+
end
47+
end
48+
4149
function __fish_brew_opts -d "Only arguments starting with a dash (options)"
4250
string match --all -- '-*' (__fish_brew_args)
4351
end
@@ -52,11 +60,12 @@ end
5260
function __fish_brew_command -d "Helps matching the first argument of brew"
5361
set args (__fish_brew_args)
5462
set -q args[1]; or return 1
63+
set -l cmd (__fish_brew_expand_alias $args[1])
5564

5665
if count $argv
57-
contains -- $args[1] $argv
66+
contains -- $cmd $argv
5867
else
59-
echo $args[1]
68+
echo $cmd
6069
end
6170
end
6271

@@ -147,11 +156,16 @@ function __fish_brew_suggest_taps_installed -d "List all available taps"
147156
| string replace (brew --repo)/Library/Taps/ ""
148157
end
149158

150-
function __fish_brew_suggest_commands -d "Lists all commands names, including aliases"
159+
function __fish_brew_suggest_commands -d "Lists all command names"
160+
set -l commands
151161
if test -f (brew --cache)/all_commands_list.txt
152-
cat (brew --cache)/all_commands_list.txt
162+
set commands (cat (brew --cache)/all_commands_list.txt)
153163
else
154-
cat (brew --repo)/completions/internal_commands_list.txt
164+
set commands (cat (brew --repo)/completions/internal_commands_list.txt)
165+
end
166+
for command in $commands
167+
set -l expanded_command (__fish_brew_expand_alias $command)
168+
test "$expanded_command" = "$command"; and echo $command
155169
end
156170
end
157171

Library/Homebrew/test/commands_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@
7777
end
7878
end
7979

80+
describe "::rebuild_internal_commands_completion_list" do
81+
it "omits internal command aliases" do
82+
mktmpdir do |repository|
83+
stub_const("HOMEBREW_REPOSITORY", repository)
84+
(repository/"completions").mkpath
85+
86+
described_class.rebuild_internal_commands_completion_list
87+
88+
commands = (repository/"completions/internal_commands_list.txt").read.lines(chomp: true)
89+
expect(commands & described_class.internal_commands_aliases).to be_empty
90+
end
91+
end
92+
end
93+
94+
describe "::rebuild_commands_completion_list" do
95+
it "omits internal command aliases from the cached command list" do
96+
mktmpdir do |cache|
97+
stub_const("HOMEBREW_CACHE", cache)
98+
allow(described_class).to receive(:external_commands).and_return(["external"])
99+
100+
described_class.rebuild_commands_completion_list
101+
102+
commands = (cache/"all_commands_list.txt").read.lines(chomp: true)
103+
expect(commands & described_class.internal_commands_aliases).to be_empty
104+
end
105+
end
106+
end
107+
80108
describe "::path" do
81109
specify "returns the path for an internal command" do
82110
expect(described_class.path("rbcmd")).to eq(Commands::HOMEBREW_CMD_PATH/"rbcmd.rb")

Library/Homebrew/test/completions_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,17 @@ def stub_nested_completion_command(command, subcommands)
396396
expect(file).to match(/^ {4}update\) _brew_update ;;/)
397397
expect(file).to match(/^complete -o bashdefault -o default -F _brew brew$/)
398398
end
399+
400+
it "doesn't add aliases to command completions" do
401+
file = described_class.generate_bash_completion_file(%w[install missing up update])
402+
expect(file).not_to include("cmd_aliases")
403+
expect(file).not_to match(/^_brew_up\(\) {$/)
404+
expect(file).not_to match(/^ {4}up\) _brew_up ;;/)
405+
expect(file).to include('[[ $(__brew_internal_command_alias "${line}") == "${line}" ]] || continue')
406+
expect(file).to include('cmd="$(__brew_internal_command_alias "${cmd}")"')
407+
expect(file).to match(/^ {4}up\) echo "update" ;;$/)
408+
expect(file).to match(/^ {4}update\) _brew_update ;;$/)
409+
end
399410
end
400411

401412
describe ".generate_zsh_subcommand_completion" do
@@ -461,6 +472,15 @@ def stub_nested_completion_command(command, subcommands)
461472
expect(completion).to include("info|i)")
462473
expect(completion).to include("*:service:__brew_services")
463474
end
475+
476+
it "doesn't generate alias completion functions" do
477+
file = described_class.generate_zsh_completion_file(%w[up update])
478+
expect(file).not_to match(/^# brew up$/)
479+
expect(file).not_to match(/^_brew_up\(\) {$/)
480+
expect(file).to match(/^ up update$/)
481+
expect(file).to include('command="${aliases[$command_or_alias]:-$command_or_alias}"')
482+
expect(file).to include('local completion_func="_brew_${command//-/_}"')
483+
end
464484
end
465485

466486
describe ".generate_zsh_completion_file" do
@@ -546,6 +566,16 @@ def stub_nested_completion_command(command, subcommands)
546566
expect(file).to match(/^__fish_brew_complete_cmd 'missing' 'Check the given formula kegs for .*'$/)
547567
expect(file).to match(/^__fish_brew_complete_cmd 'update' 'Fetch the newest version of Homebrew .*'$/)
548568
end
569+
570+
it "omits aliases from command completions" do
571+
file = described_class.generate_fish_completion_file(%w[up update])
572+
expect(file).not_to match(/^__fish_brew_complete_cmd 'up'/)
573+
expect(file).not_to match(/^__fish_brew_complete_arg 'up'/)
574+
expect(file).to match(/^ case 'up'$/)
575+
expect(file).to match(/^ echo 'update'$/)
576+
expect(file).to include("set -l cmd (__fish_brew_expand_alias $args[1])")
577+
expect(file).to match(/^__fish_brew_complete_cmd 'update'/)
578+
end
549579
end
550580
end
551581
end

0 commit comments

Comments
 (0)