From d1262868a9073304a244cdf7f582ffd27514c47c Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Thu, 12 Oct 2017 00:11:11 -0300 Subject: [PATCH 01/13] Fixes help querying - If a non-existent command with a flag is queried to Cask, the latter is printed instead of the command - If the help from a not-yet-tapped cask's command is queried, it prints brew tap's help infinitely Fixes caskroom/homebrew-cask#28977 --- Library/Homebrew/brew.rb | 5 ++++- Library/Homebrew/cask/lib/hbc/cli.rb | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 41955e60e0..3c561e8573 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -32,7 +32,7 @@ begin empty_argv = ARGV.empty? help_flag_list = %w[-h --help --usage -?] - help_flag = !ENV["HOMEBREW_HELP"].nil? + help_flag = false internal_cmd = true cmd = nil @@ -44,6 +44,9 @@ begin help_flag = true elsif !cmd && !help_flag_list.include?(arg) cmd = ARGV.delete_at(i) + elsif help_flag_list.include?(arg) & cmd + # cmd determined, and it needs help + help_flag = true end end diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index d260be4e37..bd50e41a62 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -231,7 +231,7 @@ module Hbc return if @command == "help" && @args.empty? unknown_command = @args.empty? ? @command : @args.first - raise ArgumentError, "Unknown command: #{unknown_command}" + raise ArgumentError, "Unknown command: #{@command} #{@args.join(" ")}" end def purpose From cc8e45591171ca09c2632dcb3aaa9398b2af5fb7 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Thu, 12 Oct 2017 07:39:53 -0300 Subject: [PATCH 02/13] Remove unknown_command variable --- Library/Homebrew/cask/lib/hbc/cli.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index bd50e41a62..437b0d72c6 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -230,7 +230,6 @@ module Hbc return if @command == "help" && @args.empty? - unknown_command = @args.empty? ? @command : @args.first raise ArgumentError, "Unknown command: #{@command} #{@args.join(" ")}" end From 0998e16e81dfc27f878b972e5b0c30c89723b399 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Thu, 12 Oct 2017 11:20:04 -0300 Subject: [PATCH 03/13] Fix mistaken & I meant to verify that both a cmd and a help flag were received from the shell --- Library/Homebrew/brew.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 3c561e8573..e74e65978b 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -44,7 +44,7 @@ begin help_flag = true elsif !cmd && !help_flag_list.include?(arg) cmd = ARGV.delete_at(i) - elsif help_flag_list.include?(arg) & cmd + elsif help_flag_list.include?(arg) && cmd # cmd determined, and it needs help help_flag = true end From fd2b0d21c4e1a6ebe7c4d59dd19cfc45a093fdc9 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Wed, 18 Oct 2017 08:30:26 -0300 Subject: [PATCH 04/13] Only unset HOMEBREW_HELP before issuing the tap --- Library/Homebrew/brew.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index e74e65978b..f73ca46ed8 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -32,7 +32,7 @@ begin empty_argv = ARGV.empty? help_flag_list = %w[-h --help --usage -?] - help_flag = false + help_flag = !ENV["HOMEBREW_HELP"].nil? internal_cmd = true cmd = nil @@ -119,8 +119,16 @@ begin if Process.uid.zero? && !brew_uid.zero? tap_commands += %W[/usr/bin/sudo -u ##{brew_uid}] end + if help_flag + # Unset HOMEBREW_HELP to avoid confusing the tap + ENV["HOMEBREW_HELP"] = nil + end tap_commands += %W[#{HOMEBREW_BREW_FILE} tap #{possible_tap}] safe_system(*tap_commands) + if help_flag + # Restore HOMEBREW_HELP after the tap + ENV["HOMEBREW_HELP"] = 1 + end exec HOMEBREW_BREW_FILE, cmd, *ARGV end rescue UsageError => e From 098305e6798cf64bf0eaf8f7b92e5f1fce23b301 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Wed, 18 Oct 2017 08:37:24 -0300 Subject: [PATCH 05/13] Correct setting and deleting HOMEBREW_HELP (and also delete the extra help_flag case, it's no longer needed) --- Library/Homebrew/brew.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index f73ca46ed8..ef8b28aa9c 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -44,9 +44,6 @@ begin help_flag = true elsif !cmd && !help_flag_list.include?(arg) cmd = ARGV.delete_at(i) - elsif help_flag_list.include?(arg) && cmd - # cmd determined, and it needs help - help_flag = true end end @@ -121,13 +118,13 @@ begin end if help_flag # Unset HOMEBREW_HELP to avoid confusing the tap - ENV["HOMEBREW_HELP"] = nil + ENV.delete("HOMEBREW_HELP") end tap_commands += %W[#{HOMEBREW_BREW_FILE} tap #{possible_tap}] safe_system(*tap_commands) if help_flag # Restore HOMEBREW_HELP after the tap - ENV["HOMEBREW_HELP"] = 1 + ENV["HOMEBREW_HELP"] = "1" end exec HOMEBREW_BREW_FILE, cmd, *ARGV end From 16faf6fca94625988ebcd8e1419acda478b2ef52 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Wed, 18 Oct 2017 08:47:52 -0300 Subject: [PATCH 06/13] Shorten HOMEBREW_HELP set/unset --- Library/Homebrew/brew.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index ef8b28aa9c..6fd9c75c1f 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -116,16 +116,11 @@ begin if Process.uid.zero? && !brew_uid.zero? tap_commands += %W[/usr/bin/sudo -u ##{brew_uid}] end - if help_flag - # Unset HOMEBREW_HELP to avoid confusing the tap - ENV.delete("HOMEBREW_HELP") - end + # Unset HOMEBREW_HELP to avoid confusing the tap + ENV.delete("HOMEBREW_HELP") if help_flag tap_commands += %W[#{HOMEBREW_BREW_FILE} tap #{possible_tap}] safe_system(*tap_commands) - if help_flag - # Restore HOMEBREW_HELP after the tap - ENV["HOMEBREW_HELP"] = "1" - end + ENV["HOMEBREW_HELP"] = "1" if help_flag exec HOMEBREW_BREW_FILE, cmd, *ARGV end rescue UsageError => e From ec92cf6498ffeabb32827e18408f155c92d4e3ca Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Fri, 20 Oct 2017 10:35:13 -0300 Subject: [PATCH 07/13] Print only the offending command instead of the whole Cask command --- Library/Homebrew/cask/lib/hbc/cli.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index 437b0d72c6..b2883334c6 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -230,7 +230,8 @@ module Hbc return if @command == "help" && @args.empty? - raise ArgumentError, "Unknown command: #{@command} #{@args.join(" ")}" + unknown_command = @command == "help" ? @args.first : @command + raise ArgumentError, "Unknown command: #{unknown_command}" end def purpose From 95a3d53125a4a93f31ddec46d9f56b6131f2b749 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Fri, 20 Oct 2017 13:54:58 -0300 Subject: [PATCH 08/13] Fix ternary condition style --- Library/Homebrew/cask/lib/hbc/cli.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index b2883334c6..a77feff29e 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -230,7 +230,7 @@ module Hbc return if @command == "help" && @args.empty? - unknown_command = @command == "help" ? @args.first : @command + unknown_command = (@command == "help") ? @args.first : @command raise ArgumentError, "Unknown command: #{unknown_command}" end From 75de4db003637d871a16a73d4c7c5bc1daa88728 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Fri, 20 Oct 2017 16:51:26 -0300 Subject: [PATCH 09/13] Let Cask handle the -h flag --- Library/Homebrew/brew.rb | 8 +++++++- Library/Homebrew/cask/lib/hbc/cli.rb | 5 ++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 6fd9c75c1f..8d738637b4 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -80,7 +80,13 @@ begin # - no arguments are passed if empty_argv || help_flag require "cmd/help" - Homebrew.help cmd, empty_argv: empty_argv + if cmd == "cask" + # Let Cask handle the help command + Homebrew.send cmd.to_s.tr("-", "_").downcase + exit 0 + else + Homebrew.help cmd, empty_argv: empty_argv + end # `Homebrew.help` never returns, except for external/unknown commands. end diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index a77feff29e..6ec3bf3b41 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -154,7 +154,7 @@ module Hbc def run command_name, *args = detect_command_and_arguments(*@args) command = if help? - args.unshift(command_name) + args.unshift(command_name) if !command_name.nil? "help" else self.class.lookup_command(command_name) @@ -230,8 +230,7 @@ module Hbc return if @command == "help" && @args.empty? - unknown_command = (@command == "help") ? @args.first : @command - raise ArgumentError, "Unknown command: #{unknown_command}" + raise ArgumentError, "help does not take arguments" end def purpose From 10cbc77af9cd4b6c9f69f28ccf87f24359a771fc Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Fri, 20 Oct 2017 20:21:38 -0300 Subject: [PATCH 10/13] Fix brew style errors --- Library/Homebrew/cask/lib/hbc/cli.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index 6ec3bf3b41..7f59954b58 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -154,7 +154,7 @@ module Hbc def run command_name, *args = detect_command_and_arguments(*@args) command = if help? - args.unshift(command_name) if !command_name.nil? + args.unshift(command_name) unless command_name.nil "help" else self.class.lookup_command(command_name) @@ -230,7 +230,7 @@ module Hbc return if @command == "help" && @args.empty? - raise ArgumentError, "help does not take arguments" + raise ArgumentError, "help does not take arguments." end def purpose From a8ee54a35fb976ad142b1a21a718d43351ea6268 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Sat, 21 Oct 2017 15:43:20 -0300 Subject: [PATCH 11/13] Actually check that the command name was set before adding it to args --- Library/Homebrew/cask/lib/hbc/cli.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index 7f59954b58..92f0333ad8 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -154,7 +154,7 @@ module Hbc def run command_name, *args = detect_command_and_arguments(*@args) command = if help? - args.unshift(command_name) unless command_name.nil + args.unshift(command_name) unless command_name.nil? "help" else self.class.lookup_command(command_name) From 2bfc7904fb82651bbb08765054f1c916784c29bf Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Tue, 24 Oct 2017 08:04:49 -0300 Subject: [PATCH 12/13] Simplify cask help check --- Library/Homebrew/brew.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 8d738637b4..39467ce9e1 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -78,15 +78,10 @@ begin # - a help flag is passed AND a command is matched # - a help flag is passed AND there is no command specified # - no arguments are passed - if empty_argv || help_flag + # - if cmd is Cask, let Cask handle the help command instead + if (empty_argv || help_flag ) && cmd != "cask" require "cmd/help" - if cmd == "cask" - # Let Cask handle the help command - Homebrew.send cmd.to_s.tr("-", "_").downcase - exit 0 - else - Homebrew.help cmd, empty_argv: empty_argv - end + Homebrew.help cmd, empty_argv: empty_argv # `Homebrew.help` never returns, except for external/unknown commands. end From 09e26b1152cb66f0f1914f09a629c27a9e112b19 Mon Sep 17 00:00:00 2001 From: "L. E. Segovia" Date: Tue, 24 Oct 2017 10:17:11 -0300 Subject: [PATCH 13/13] brew style fixes --- Library/Homebrew/brew.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 39467ce9e1..7a004240e9 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -78,8 +78,8 @@ begin # - a help flag is passed AND a command is matched # - a help flag is passed AND there is no command specified # - no arguments are passed - # - if cmd is Cask, let Cask handle the help command instead - if (empty_argv || help_flag ) && cmd != "cask" + # - if cmd is Cask, let Cask handle the help command instead + if (empty_argv || help_flag) && cmd != "cask" require "cmd/help" Homebrew.help cmd, empty_argv: empty_argv # `Homebrew.help` never returns, except for external/unknown commands.