threads / patch / 42540

patch, 2 partsgit-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION, $short_sha

Subject: [PATCH 1/2] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION, $short_sha

## tl;dr

3 messages between Jun 6, 2016 and Jun 7, 2016. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Ville Skyttä· Jun 6, 2016, 16:29 UTC · lore

When the shell is in "nounset" or "set -u" mode, referencing unset or null variables results in an error. Protect $ZSH_VERSION and $BASH_VERSION against that, and initialize $short_sha before use.

Signed-off-by: Ville Skyttä <ville.skytta@iki.fi>
---
 contrib/completion/git-prompt.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Show changes to contrib/completion/git-prompt.sh +3 −3
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 64219e6..97eacd7 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -355,8 +355,8 @@ __git_ps1 ()
 	# incorrect.)
 	#
 	local ps1_expanded=yes
-	[ -z "$ZSH_VERSION" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no
-	[ -z "$BASH_VERSION" ] || shopt -q promptvars || ps1_expanded=no
+	[ -z "${ZSH_VERSION-}" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no
+	[ -z "${BASH_VERSION-}" ] || shopt -q promptvars || ps1_expanded=no
 
 	local repo_info rev_parse_exit_code
 	repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
@@ -368,7 +368,7 @@ __git_ps1 ()
 		return $exit
 	fi
 
-	local short_sha
+	local short_sha=""
 	if [ "$rev_parse_exit_code" = "0" ]; then
 		short_sha="${repo_info##*$'\n'}"
 		repo_info="${repo_info%$'\n'*}"
-- 
2.5.5
Junio C Hamano· Jun 6, 2016, 20:08 UTC · re: Ville Skyttä · lore

Re: [PATCH 1/2] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION, $short_sha

Ville Skyttä <ville.skytta@iki.fi> writes:
Show 6 quoted lines
> When the shell is in "nounset" or "set -u" mode, referencing unset or
> null variables results in an error. Protect $ZSH_VERSION and
> $BASH_VERSION against that, and initialize $short_sha before use.
>
> Signed-off-by: Ville Skyttä <ville.skytta@iki.fi>
> ---

Thanks for following up. I do not run my interactive shells with "set -u", so I never noticed these, but apparently you do (or you know who does)--does this patch cover everything that is not "-u" safe?

Will queue.
Show 27 quoted lines
>  contrib/completion/git-prompt.sh | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
> index 64219e6..97eacd7 100644
> --- a/contrib/completion/git-prompt.sh
> +++ b/contrib/completion/git-prompt.sh
> @@ -355,8 +355,8 @@ __git_ps1 ()
>  	# incorrect.)
>  	#
>  	local ps1_expanded=yes
> -	[ -z "$ZSH_VERSION" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no
> -	[ -z "$BASH_VERSION" ] || shopt -q promptvars || ps1_expanded=no
> +	[ -z "${ZSH_VERSION-}" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no
> +	[ -z "${BASH_VERSION-}" ] || shopt -q promptvars || ps1_expanded=no
>  
>  	local repo_info rev_parse_exit_code
>  	repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
> @@ -368,7 +368,7 @@ __git_ps1 ()
>  		return $exit
>  	fi
>  
> -	local short_sha
> +	local short_sha=""
>  	if [ "$rev_parse_exit_code" = "0" ]; then
>  		short_sha="${repo_info##*$'\n'}"
>  		repo_info="${repo_info%$'\n'*}"
Ville Skyttä· Jun 7, 2016, 05:22 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION, $short_sha

On Mon, Jun 6, 2016 at 11:08 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 12 quoted lines
> Ville Skyttä <ville.skytta@iki.fi> writes:
>
>> When the shell is in "nounset" or "set -u" mode, referencing unset or
>> null variables results in an error. Protect $ZSH_VERSION and
>> $BASH_VERSION against that, and initialize $short_sha before use.
>>
>> Signed-off-by: Ville Skyttä <ville.skytta@iki.fi>
>> ---
>
> Thanks for following up.  I do not run my interactive shells with
> "set -u", so I never noticed these, but apparently you do (or you
> know who does)

I don't personally, but some do. The issue pops up every now and then with bash-completion, e.g. https://github.com/scop/bash-completion/issues/44

> does this patch cover everything that is not "-u" safe?

I don't know. These were the low hanging fruits I've come across. Null $ZSH_VERSION in git-prompt.sh was the thing I ran into immediately when enabling "nounset" mode to start looking into the above bash-completion issue. Quick testing with zsh revealed the same issue with null $BASH_VERSION, and you found the $short_sha one.

← back to recent threads