* [PATCH] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION
@ 2016-05-24 8:31 Ville Skyttä
2016-05-24 17:11 ` Junio C Hamano
2016-05-24 17:27 ` Junio C Hamano
0 siblings, 2 replies; 5+ messages in thread
From: Ville Skyttä @ 2016-05-24 8:31 UTC (permalink / raw)
To: git
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.
Signed-off-by: Ville Skyttä <ville.skytta@iki.fi>
---
contrib/completion/git-prompt.sh | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 64219e6..341cf6b 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 \
--
2.5.5
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION 2016-05-24 8:31 [PATCH] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION Ville Skyttä @ 2016-05-24 17:11 ` Junio C Hamano 2016-05-24 17:27 ` Junio C Hamano 1 sibling, 0 replies; 5+ messages in thread From: Junio C Hamano @ 2016-05-24 17:11 UTC (permalink / raw) To: Ville Skyttä; +Cc: git 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. > > Signed-off-by: Ville Skyttä <ville.skytta@iki.fi> > --- Makes sense from a cursory look. Thanks. > contrib/completion/git-prompt.sh | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh > index 64219e6..341cf6b 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 \ ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION 2016-05-24 8:31 [PATCH] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION Ville Skyttä 2016-05-24 17:11 ` Junio C Hamano @ 2016-05-24 17:27 ` Junio C Hamano 2016-05-24 19:53 ` Junio C Hamano 2016-06-06 16:31 ` Ville Skyttä 1 sibling, 2 replies; 5+ messages in thread From: Junio C Hamano @ 2016-05-24 17:27 UTC (permalink / raw) To: SZEDER Gábor; +Cc: git, Ville Skyttä 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. > 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 I wonder if we can teach t9903 to run (at least) some parts of the completion script under "set -u" to catch these automatically without a report from an end user. With the attached "just started to scratch the surface" patch to test-lib.sh and fix by Ville to the git-prompt.sh script, we find another one. ...contrib/completion/git-prompt.sh: line 481: short_sha: unbound variable Ville, I think you want to take the "git-prompt.sh" part from the following and replace it with your patch. Others, I am not sure how seriously we would want to support those who use "set -u"; the damage to test-lib.sh so far (see below) does not look _too_ bad, but the reason why I have "set -u" after including lib-bash.sh is because it will trigger a lot more errors while lib-bash and test-lib do their preparation. contrib/completion/git-prompt.sh | 5 +++-- t/t9903-bash-prompt.sh | 2 ++ t/test-lib.sh | 8 +++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index 64219e6..ed994a9 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 \ @@ -369,6 +369,7 @@ __git_ps1 () fi local short_sha + short_sha= if [ "$rev_parse_exit_code" = "0" ]; then short_sha="${repo_info##*$'\n'}" repo_info="${repo_info%$'\n'*}" diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh index 0db4469..96b5ff8 100755 --- a/t/t9903-bash-prompt.sh +++ b/t/t9903-bash-prompt.sh @@ -7,6 +7,8 @@ test_description='test git-specific bash prompt functions' . ./lib-bash.sh +set -u + . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh" actual="$TRASH_DIRECTORY/actual" diff --git a/t/test-lib.sh b/t/test-lib.sh index 0055ebb..36ca13e 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -186,6 +186,9 @@ test "x$TERM" != "xdumb" && ( ) && color=t +HARNESS_ACTIVE= quiet= verbose_only= run_list= trace= +: ${GIT_SKIP_TESTS=} + while test "$#" -ne 0 do case "$1" in @@ -585,7 +588,7 @@ test_eval_ () { test_run_ () { test_cleanup=: - expecting_failure=$2 + expecting_failure=${2-} if test "${GIT_TEST_CHAIN_LINT:-1}" != 0; then # turn off tracing for this test-eval, as it simply creates @@ -750,6 +753,8 @@ test_done () { esac } +GIT_VALGRIND= + if test -n "$valgrind" then make_symlink () { @@ -902,6 +907,7 @@ fi # in subprocesses like git equals our $PWD (for pathname comparisons). cd -P "$TRASH_DIRECTORY" || exit 1 +skip_all= this_test=${0##*/} this_test=${this_test%%-*} if match_pattern_list "$this_test" $GIT_SKIP_TESTS ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION 2016-05-24 17:27 ` Junio C Hamano @ 2016-05-24 19:53 ` Junio C Hamano 2016-06-06 16:31 ` Ville Skyttä 1 sibling, 0 replies; 5+ messages in thread From: Junio C Hamano @ 2016-05-24 19:53 UTC (permalink / raw) To: SZEDER Gábor; +Cc: git, Ville Skyttä Junio C Hamano <gitster@pobox.com> writes: > Others, I am not sure how seriously we would want to support those > who use "set -u"; the damage to test-lib.sh so far (see below) does > not look _too_ bad, but the reason why I have "set -u" after > including lib-bash.sh is because it will trigger a lot more errors > while lib-bash and test-lib do their preparation. Ehh, this didn't come out very well as I hoped, primarily because my thinking behind it was muddy X-<. I do want to see us keep supporting those who "set -u" in their interactive shell environment, and we should strive to make sure that the "prompt" and the "completion", which are part of their interactive shell experience, work well with "set -u". It does not mean "cd t && sh t$anything.sh" must be "set -u" clean at all (this is where my thinking was muddy); it is sufficient if we can successfully run t9903 (prompt) with "set -u" before the script includes "git-prompt". We do not have to be able to insert "set -u" at the beginning of test-lib.sh and cleanly run other tests. Having said that, some parts of "minimum" change shown in my "how about this" patch would probably be worth doing anyway. They make sure that: (1) variables the test framework uses will always be initialized properly, without getting affected a stray environment variable the user may have. E.g. "trace". (2) environment variables the test framework may accept to change the behaviour, e.g. GIT_SKIP_TESTS, are clearly documented by using the idiom "set to an empty string if not set". So in that sense, even though we may not add "set -u" at the beginning of test-lib.sh in the production code, doing so may be a good developer aid to catch other variables the attached patch did not catch and complete the above two points. > diff --git a/t/test-lib.sh b/t/test-lib.sh > index 0055ebb..36ca13e 100644 > --- a/t/test-lib.sh > +++ b/t/test-lib.sh > @@ -186,6 +186,9 @@ test "x$TERM" != "xdumb" && ( > ) && > color=t > > +HARNESS_ACTIVE= quiet= verbose_only= run_list= trace= > +: ${GIT_SKIP_TESTS=} > + > while test "$#" -ne 0 > do > case "$1" in > @@ -585,7 +588,7 @@ test_eval_ () { > > test_run_ () { > test_cleanup=: > - expecting_failure=$2 > + expecting_failure=${2-} > > if test "${GIT_TEST_CHAIN_LINT:-1}" != 0; then > # turn off tracing for this test-eval, as it simply creates > @@ -750,6 +753,8 @@ test_done () { > esac > } > > +GIT_VALGRIND= > + > if test -n "$valgrind" > then > make_symlink () { > @@ -902,6 +907,7 @@ fi > # in subprocesses like git equals our $PWD (for pathname comparisons). > cd -P "$TRASH_DIRECTORY" || exit 1 > > +skip_all= > this_test=${0##*/} > this_test=${this_test%%-*} > if match_pattern_list "$this_test" $GIT_SKIP_TESTS ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION 2016-05-24 17:27 ` Junio C Hamano 2016-05-24 19:53 ` Junio C Hamano @ 2016-06-06 16:31 ` Ville Skyttä 1 sibling, 0 replies; 5+ messages in thread From: Ville Skyttä @ 2016-06-06 16:31 UTC (permalink / raw) To: Junio C Hamano; +Cc: SZEDER Gábor, git On Tue, May 24, 2016 at 8:27 PM, Junio C Hamano <gitster@pobox.com> wrote: > 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. >> 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 > > I wonder if we can teach t9903 to run (at least) some parts of the > completion script under "set -u" to catch these automatically without > a report from an end user. > > With the attached "just started to scratch the surface" patch to > test-lib.sh and fix by Ville to the git-prompt.sh script, we find > another one. > > ...contrib/completion/git-prompt.sh: line 481: short_sha: unbound variable > > Ville, I think you want to take the "git-prompt.sh" part from the > following and replace it with your patch. Hm, I'm not entirely sure what I was expected to do, but I took the short_sha bit, added it to my patch (modifying to be in line with the style of the rest of the file), and submitted a superseding patch with the subject "[PATCH 1/2] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION, $short_sha" (and there's no part 2/2 coming, that was a user error). ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-06-06 16:32 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-24 8:31 [PATCH] git-prompt.sh: Don't error on null ${ZSH,BASH}_VERSION Ville Skyttä
2016-05-24 17:11 ` Junio C Hamano
2016-05-24 17:27 ` Junio C Hamano
2016-05-24 19:53 ` Junio C Hamano
2016-06-06 16:31 ` Ville Skyttä
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox