git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Sibo Dong via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Sibo Dong <sibo.dong@outlook.com>
Subject: Re: [PATCH] git-prompt.sh: make `option` a local variable
Date: Sat, 31 Oct 2020 15:45:37 -0700	[thread overview]
Message-ID: <xmqqmu02xbe6.fsf@gitster.c.googlers.com> (raw)
In-Reply-To: <pull.775.git.1604182187195.gitgitgadget@gmail.com> (Sibo Dong via GitGitGadget's message of "Sat, 31 Oct 2020 22:09:46 +0000")

"Sibo Dong via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Sibo Dong <sibo.dong@outlook.com>
>
> Other variables like `key` and `value` are local to this completion
> script, so I think the same should be done with `option`.
>
> Signed-off-by: Sibo Dong <sibo.dong@outlook.com>
> ---
>     git-prompt.sh: make option a local variable
>     
>     This is very trivial, but variables like key and value are local to 
>     git-prompt.sh, so I think the same should be done with option.

That explains why we might consider localizing option; another thing
to consider is if it is safe to just blindly localize it.

Here is how I would explain and justify the change, if I were
writing it:

    git-prompt.sh: localize `option` in __git_ps1_show_upstream

    The variable 'option' is used in __git_ps1_show_upstream()
    without being localized.

    This clobbers the variable the user may be using for other
    purposes, which is bad.  Luckily, $option is not used to carry
    information around in the script as a global variable.  The use
    of it in this script has very limited scope (namely, only inside
    this function), so just declare that it is "local".


following the usual pattern:

 - start with the observation on the codebase before your change,
 - explain what the problem is with the current state,
 - outline the solution and explain why it is the right thing to do, and
 - order the codebase to "be like so".


> diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
> index 16260bab73..5116016d39 100644
> --- a/contrib/completion/git-prompt.sh
> +++ b/contrib/completion/git-prompt.sh
> @@ -137,6 +137,7 @@ __git_ps1_show_upstream ()
>  	done <<< "$output"
>  
>  	# parse configuration values
> +	local option
>  	for option in ${GIT_PS1_SHOWUPSTREAM}; do
>  		case "$option" in
>  		git|svn) upstream="$option" ;;

Looks OK to me.

  reply	other threads:[~2020-10-31 22:47 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-31 22:09 [PATCH] git-prompt.sh: make `option` a local variable Sibo Dong via GitGitGadget
2020-10-31 22:45 ` Junio C Hamano [this message]
2020-11-01  8:04   ` Sibo Dong
2020-11-02 20:39     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqmu02xbe6.fsf@gitster.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=sibo.dong@outlook.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).