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

brew.sh: don't autoupdate if --help passed #1190

Merged
merged 2 commits into from Oct 3, 2016

Conversation

vladshablinsky
Copy link
Contributor

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew tests with your changes locally?

Seems to fix #1188.

@BrewTestBot BrewTestBot added the in progress Maintainers are working on this label Sep 29, 2016
do
if [[ $arg = "--help" ]]
then
export HOMEBREW_COMMAND_HELP="1"
Copy link
Contributor

Choose a reason for hiding this comment

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

Since it only persists for the duration of the subshell, should we just set HOMEBREW_NO_AUTO_UPDATE instead of adding another variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 amending.

# Check if user passes --help command which should prevent auto-update.
for arg in "$@"
do
if [[ $arg = "--help" ]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Same goes for "-h".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like even more than that. https://github.com/Homebrew/brew/blob/master/Library/Homebrew/brew.rb#L34

 help_flag_list = %w[-h --help --usage -?]

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah. -? — didn't know that. Weird one.

@reitermarkus
Copy link
Member

reitermarkus commented Sep 29, 2016

Can't we set HOMEBREW_NO_AUTO_UPDATE in brew.rb by using the already existing help_flag_list instead of duplicating the flags in brew.sh, or is it already too late by then? Otherwise I would suggest removing the help_flag_list and instead exporting HOMEBREW_COMMAND_HELP like in the first diff.

@zmwangx
Copy link
Contributor

zmwangx commented Sep 29, 2016

By the time you get to brew.rb the battle is already lost. update-preinstall is in brew.sh.

@reitermarkus
Copy link
Member

Ah, ok, let's get rid of help_flag_list then, as it is used only once anyways: https://github.com/Homebrew/brew/blob/master/Library/Homebrew/brew.rb#L42

@zmwangx
Copy link
Contributor

zmwangx commented Sep 29, 2016

Getting rid of help_flag_list and using an in place list literal is good, but I doubt we want to set another env var.

@vladshablinsky
Copy link
Contributor Author

Getting rid of help_flag_list and using an in place list literal is good

With help_flag_list it looks more readable since variable's name explains variable's purpose.

@reitermarkus
Copy link
Member

reitermarkus commented Sep 29, 2016

We could completely remove the help_flag_list by always calling the help command instead of also parsing the help flags in brew.rb.

# check for help flags and call `help` instead
arg_n=0
for arg in "$@"
do
  ((arg_n++))

  if [[ $arg = "--help" || $arg = "-h" || $arg = "--usage" || $arg = "-?" ]]
  then
    set -- "${@:1:arg_n-1}" "${@:arg_n+1}" && ((arg_n--))

    if [ $HOMEBREW_COMMAND != "help" ]
    then
      set -- "$HOMEBREW_COMMAND" "$@" && ((arg_n++))
      HOMEBREW_COMMAND="help"
    fi
  fi
done

@MikeMcQuaid
Copy link
Member

I doubt we want to set another env var.

It's fine to set another environment variable to pass state between Bash and Ruby code. Personally I'd rather see us do that than overload HOMEBREW_NO_AUTO_UPDATE.

@vladshablinsky
Copy link
Contributor Author

vladshablinsky commented Sep 30, 2016

@MikeMcQuaid @reitermarkus @zmwangx @dunn

What if we transform any command like brew (--help|--usage|-?|-h|<some_command>)+ into
brew help <some_command> e.g.

brew --help install --usage -> brew help install
brew install -h -> brew help install

Basically we just remove all the help flags and unshift help to $@.

arg_n=0
for arg in "$@"
do
  ((arg_n++))

  if [[ $arg = "--help" || $arg = "-h" || $arg = "--usage" || $arg = "-?" ]]
  then
    set -- "${@:1:arg_n-1}" "${@:arg_n+1}" && ((arg_n--))

    if [[ $1 != "help" ]]
    then
      set -- "help" "$@" && ((arg_n++))
    fi
  fi
done

@MikeMcQuaid
Copy link
Member

What if we transform any command like brew (--help|--usage|-?|-h|<some_command>)+ into
brew help <some_command> e.g.

Seems like a good idea (even if the Bash code currently makes me want to cry a bit). It may be cleaner to consider just setting HOMEBREW_HELP=1 rather than modifying the argument list.

@vladshablinsky
Copy link
Contributor Author

It may be cleaner to consider just setting HOMEBREW_HELP=1 rather than modifying the argument list.

The idea was to prevent parsing --help-like flags twice: in sh and then in rb. Sorry for such a Bash code, I'm quite new to writing my own Bash code.

Assuming what you have said, can this be a solution?

diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh
index 22ebb87..8ecaadd 100644
--- a/Library/Homebrew/brew.sh
+++ b/Library/Homebrew/brew.sh
@@ -179,6 +179,15 @@ then
   set -- "$@" -v
 fi

+for arg in "$@"
+do
+  if [[ $arg = "--help" || $arg = "-h" || $arg = "--usage" || $arg = "-?" ]]
+  then
+    HOMEBREW_HELP="1"
+    break
+  fi
+done
+
 HOMEBREW_ARG_COUNT="$#"
 HOMEBREW_COMMAND="$1"
 shift
@@ -268,6 +277,7 @@ setup-analytics
 report-analytics-screenview-command

 update-preinstall() {
+  [[ "$HOMEBREW_HELP" != "1" ]] || return
   [[ -z "$HOMEBREW_NO_AUTO_UPDATE" ]] || return
   [[ -z "$HOMEBREW_UPDATE_PREINSTALL" ]] || return

@MikeMcQuaid
Copy link
Member

MikeMcQuaid commented Oct 1, 2016

The idea was to prevent parsing --help-like flags twice: in sh and then in rb. Sorry for such a Bash code, I'm quite new to writing my own Bash code.

Not your bad, this stuff is just far messier to do in Bash/

Assuming what you have said, can this be a solution?

Yeh, that looks good. You could also export it and then delete the --help handling code from brew.rb in favour of checking if the environment variable is set.

@@ -32,20 +32,17 @@ def require?(path)

empty_argv = ARGV.empty?
help_flag_list = %w[-h --help --usage -?]
help_flag = false
help_flag = ENV["HOMEBREW_HELP"]
Copy link
Member

Choose a reason for hiding this comment

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

!ENV["HOMEBREW_HELP"].nil?, maybe, so we get a nice boolean.

@MikeMcQuaid MikeMcQuaid merged commit 881fdcd into Homebrew:master Oct 3, 2016
@MikeMcQuaid
Copy link
Member

Thanks @vladshablinsky!

@MikeMcQuaid MikeMcQuaid removed the in progress Maintainers are working on this label Oct 3, 2016
@vladshablinsky vladshablinsky deleted the no-autoupdate-on-help branch October 3, 2016 21:53
@Homebrew Homebrew locked and limited conversation to collaborators May 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

brew autoupdate runs on brew install --help
6 participants