git-prompt: add quiet upstream indicator option
cover git-prompt: add quiet upstream indicator option
This patch series allows a cleaner git-prompt when the upstream indicator is not actionable.
In particular, the '=' to the upstream can be redundant noise if the user is always on a remote-tracking branch and only needs to be aware of any divergence.
Since SHOWUPSTREAM already supports various options, I propose a new 'quiet' option to limit it to actionable output (omit '=')
* Default: branch =
* Verbose: branch|u=
* Verbose name: branch|u= remote/branch
* Quiet: branch
* Verbose quiet: branch|u
* Verbose quiet name: branch|u remote/branch
Ahead/behind output is unaffected (default <> or verbose +N-M).
Kiril Ivanov (3):
t9903: shorten setup using test_commit
t9903: add upstream indicator tests
git-prompt: add quiet upstream indicator option
contrib/completion/git-prompt.sh | 14 ++++-- t/t9903-bash-prompt.sh | 86 ++++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 20 deletions(-)
base-commit: fd372d9b1a69a01a676398882bbe3840bf51fe72
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2101%2Fivanovkirilg%2Fquiet-option-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2101/ivanovkirilg/quiet-option-v1
Pull-Request: https://github.com/git/git/pull/2101
Signature · 1 line
gitgitgadget
PATCH 1/3 t9903: shorten setup using test_commit
From: Kiril Ivanov <[email protected]>
--- t/t9903-bash-prompt.sh | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-)
t/t9903-bash-prompt.sh +11 -16
@@ -18,26 +18,21 @@ c_green='\001\e[32m\002' c_lblue='\001\e[1;34m\002' c_clear='\001\e[0m\002' +# (main) (b1) +# initial (t1) - second-b1 - third-b1 (t2) +# \ +# second-b2 - another-b2 - yet-another-b2 +# (b2) test_expect_success 'setup for prompt tests' ' git init otherrepo && - echo 1 >file && - git add file && - test_tick && - git commit -m initial && - git tag -a -m msg1 t1 && + test_commit --annotate initial file contents1 t1 && git checkout -b b1 && - echo 2 >file && - git commit -m "second b1" file && - echo 3 >file && - git commit -m "third b1" file && - git tag -a -m msg2 t2 && + test_commit --no-tag second-b1 file && + test_commit --annotate third-b1 file contents2 t2 && git checkout -b b2 main && - echo 0 >file && - git commit -m "second b2" file && - echo 00 >file && - git commit -m "another b2" file && - echo 000 >file && - git commit -m "yet another b2" file && + test_commit --no-tag second-b2 file && + test_commit --no-tag another-b2 file && + test_commit --no-tag yet-another-b2 file && mkdir ignored_dir && echo "ignored_dir/" >>.gitignore && git checkout main
Signature · 1 line
gitgitgadget
PATCH 2/3 t9903: add upstream indicator tests
In reply to Kiril Ivanov via GitGitGadgetFrom: Kiril Ivanov <[email protected]>
The upstream indicator currently has no test coverage.
Since GIT_PS1_SHOWUPSTREAM supports various options, make these tests parameterized with a matrix for expected results (ahead/behind/etc).
--- t/t9903-bash-prompt.sh | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
t/t9903-bash-prompt.sh +59 -0
@@ -25,6 +25,8 @@ c_clear='\001\e[0m\002' # (b2) test_expect_success 'setup for prompt tests' ' git init otherrepo && + git init --bare origin.git && + git remote add origin origin.git && test_commit --annotate initial file contents1 t1 && git checkout -b b1 && test_commit --no-tag second-b1 file &&
@@ -35,6 +37,7 @@ test_expect_success 'setup for prompt tests' ' test_commit --no-tag yet-another-b2 file && mkdir ignored_dir && echo "ignored_dir/" >>.gitignore && + git push --set-upstream origin main b1 b2 && git checkout main '
@@ -515,6 +518,62 @@ test_expect_success 'prompt - untracked files status indicator - not shown insid test_cmp expected "$actual" ' +################################################################ +# Show Upstream +options=( short verbose 'verbose name' ) +expected_equal=( ' =' '|u=' '|u= origin/main' ) +expected_ahead=( ' >' '|u+1' '|u+1 origin/main') +expected_behind=( ' <' '|u-1' '|u-1 origin/b1' ) +expected_diverged=(' <>' '|u+1-1' '|u+1-1 origin/b1') + +for i in ${!options[@]} +do + option=${options[$i]} + + test_expect_success "prompt - upstream status indicator - $option - equal" ' + printf " (main${expected_equal[$i]})" >expected && + GIT_PS1_SHOWUPSTREAM=$option && + test_when_finished "sane_unset GIT_PS1_SHOWUPSTREAM" && + __git_ps1 >"$actual" && + test_cmp expected "$actual" + ' + + test_expect_success "prompt - upstream status indicator - $option - ahead" ' + printf " (main${expected_ahead[$i]})" >expected && + GIT_PS1_SHOWUPSTREAM=$option && + test_when_finished "sane_unset GIT_PS1_SHOWUPSTREAM" && + test_commit --no-tag ahead && + test_when_finished "git reset HEAD^" && + __git_ps1 >"$actual" && + test_cmp expected "$actual" + ' + + test_expect_success "prompt - upstream status indicator - $option - behind" ' + printf " (b1${expected_behind[$i]})" >expected && + GIT_PS1_SHOWUPSTREAM=$option && + test_when_finished "sane_unset GIT_PS1_SHOWUPSTREAM" && + git checkout b1 && + test_when_finished "git checkout main" && + git reset HEAD^ && + test_when_finished "git reset @{u}" && + __git_ps1 >"$actual" && + test_cmp expected "$actual" + ' + + test_expect_success "prompt - upstream status indicator - $option - diverged" ' + printf " (b1${expected_diverged[$i]})" >expected && + GIT_PS1_SHOWUPSTREAM=$option && + test_when_finished "sane_unset GIT_PS1_SHOWUPSTREAM" && + git checkout b1 && + test_when_finished "git checkout main" && + git reset HEAD^ && + test_when_finished "git reset @{u}" && + test_commit --no-tag ahead && + __git_ps1 >"$actual" && + test_cmp expected "$actual" + ' +done + test_expect_success 'prompt - format string starting with dash' ' printf -- "-main" >expected && __git_ps1 "-%s" >"$actual" &&
Signature · 1 line
gitgitgadget
PATCH 3/3 git-prompt: add quiet upstream indicator option
In reply to Kiril Ivanov via GitGitGadgetFrom: Kiril Ivanov <[email protected]>
Other single-char state indicators (e.g. stash, dirty) as well as pipe-delimited indicators (e.g. '|CONFLICT') are only shown when they are actionable.
OTOH, the upstream '=' (up-to-date with the upstream) is simply a neutral state, which git-prompt is usually quiet about. Therefore, allow users to hide it.
The 'verbose quiet' combination sounds weird but it may be what the user wants, and does not conflict with anything else, so why not.
--- contrib/completion/git-prompt.sh | 14 ++++++++++---- t/t9903-bash-prompt.sh | 10 +++++----- 2 files changed, 15 insertions(+), 9 deletions(-)
contrib/completion/git-prompt.sh +10 -4
@@ -55,13 +55,14 @@ # enabled. # # If you would like to see the difference between HEAD and its upstream, -# set GIT_PS1_SHOWUPSTREAM="auto". A "<" indicates you are behind, ">" -# indicates you are ahead, "<>" indicates you have diverged and "=" -# indicates that there is no difference. You can further control +# set GIT_PS1_SHOWUPSTREAM to a nonempty value. A "<" indicates you are +# behind, ">" indicates you are ahead, "<>" indicates you have diverged +# and "=" indicates that there is no difference. You can further control # behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated list # of values: # # verbose show number of commits ahead/behind (+/-) upstream +# quiet omit '=' when there is no difference to the upstream # name if verbose, then also show the upstream abbrev name # legacy don't use the '--count' option available in recent # versions of git-rev-list
@@ -157,7 +158,7 @@ __git_ps1_show_upstream () { local key value local svn_remotes="" svn_url_pattern="" count n - local upstream_type=git legacy="" verbose="" name="" + local upstream_type=git legacy="" verbose="" quiet="" name="" local LF="$__git_LF" # get some config options from git-config
@@ -187,6 +188,7 @@ __git_ps1_show_upstream () case "$option" in git|svn) upstream_type="$option" ;; verbose) verbose=1 ;; + quiet) quiet=1 ;; legacy) legacy=1 ;; name) name=1 ;; esac
@@ -262,6 +264,8 @@ __git_ps1_show_upstream () "") # no upstream p="" ;; "0 0") # equal to upstream + [ "$quiet" ] && + p="" || p="=" ;; "0 "*) # ahead of upstream p=">" ;;
@@ -275,6 +279,8 @@ __git_ps1_show_upstream () "") # no upstream upstream="" ;; "0 0") # equal to upstream + [ "$quiet" ] && + upstream="|u" || upstream="|u=" ;; "0 "*) # ahead of upstream upstream="|u+${count#0 }" ;;
t/t9903-bash-prompt.sh +5 -5
@@ -520,11 +520,11 @@ test_expect_success 'prompt - untracked files status indicator - not shown insid ################################################################ # Show Upstream -options=( short verbose 'verbose name' ) -expected_equal=( ' =' '|u=' '|u= origin/main' ) -expected_ahead=( ' >' '|u+1' '|u+1 origin/main') -expected_behind=( ' <' '|u-1' '|u-1 origin/b1' ) -expected_diverged=(' <>' '|u+1-1' '|u+1-1 origin/b1') +options=( short verbose 'verbose name' quiet 'verbose quiet' 'verbose name quiet') +expected_equal=( ' =' '|u=' '|u= origin/main' '' '|u' '|u origin/main' ) +expected_ahead=( ' >' '|u+1' '|u+1 origin/main' ' >' '|u+1' '|u+1 origin/main' ) +expected_behind=( ' <' '|u-1' '|u-1 origin/b1' ' <' '|u-1' '|u-1 origin/b1' ) +expected_diverged=(' <>' '|u+1-1' '|u+1-1 origin/b1' ' <>' '|u+1-1' '|u+1-1 origin/b1' ) for i in ${!options[@]} do
Signature · 1 line
gitgitgadget
Quoted 14 lines · Kiril Ivanov via GitGitGadget
contrib/completion/git-prompt.sh +2
-0@@ -262,6 +264,8 @@ __git_ps1_show_upstream () "") # no upstream p="" ;; "0 0") # equal to upstream + [ "$quiet" ] && + p="" || p="=" ;; "0 "*) # ahead of upstream p=">" ;;
contrib/completion/git-prompt.sh __git_ps1_show_upstream () · line 286
Quoted 8 lines
contrib/completion/git-prompt.sh +2
-0@@ -275,6 +279,8 @@ __git_ps1_show_upstream () "") # no upstream upstream="" ;; "0 0") # equal to upstream + [ "$quiet" ] && + upstream="|u" || upstream="|u=" ;; "0 "*) # ahead of upstream upstream="|u+${count#0 }" ;;
I generally advise avoiding "X && Y || Z" as a replacement for "if X; then Y; else Z; fi" because if X succeeds and Y fails, Z runs in the former but not the latter.
Here, Y is an infallible (?) assignment, so we should not have any issues. OTOH, such subtleties may teach novice readers the wrong lesson ("oh, handy shortcut!" vs "I see how this works in restricted cases"). Using "if" is a bit more invasive wrt to the patch, but would be clearer to me.
Our coding style also prefers "test" to "[".
Signature · 1 line
D. Ben Knoble
Quoted 12 lines · D. Ben Knoble
contrib/completion/git-prompt.sh __git_ps1_show_upstream () · line 284
Quoted 8 lines · Kiril Ivanov via GitGitGadget
contrib/completion/git-prompt.sh +2
-0@@ -275,6 +279,8 @@ __git_ps1_show_upstream () "0 0") # equal to upstream + [ "$quiet" ] && + upstream="|u" || upstream="|u=" ;; "0 "*) # ahead of upstream upstream="|u+${count#0 }" ;;Our coding style also prefers "test" to "[".
In this bash-specific script, the normal project coding style is relaxed and [ is used regularly, far more so than test, so use of [ here is appropriate and consistent with surrounding code.
Quoted 15 lines · Eric Sunshine
Quoted 12 lines · D. Ben Knoble
contrib/completion/git-prompt.sh __git_ps1_show_upstream () · line 284
Quoted 8 lines · Kiril Ivanov via GitGitGadget
contrib/completion/git-prompt.sh +2
-0@@ -275,6 +279,8 @@ __git_ps1_show_upstream () "0 0") # equal to upstream + [ "$quiet" ] && + upstream="|u" || upstream="|u=" ;; "0 "*) # ahead of upstream upstream="|u+${count#0 }" ;;Our coding style also prefers "test" to "[".
Quoted 3 lines
In this bash-specific script, the normal project coding style is relaxed and
[is used regularly, far more so thantest, so use of[here is appropriate and consistent with surrounding code.
Good to know, thanks! That'll teach me to respond without looking at surrounding context ;)
Signature · 1 line
D. Ben Knoble
Quoted 7 lines · Kiril Ivanov via GitGitGadget
Kiril Ivanov (3):
t9903: shorten setup usingtest_commit
t9903: add upstream indicator tests
git-prompt: add quiet upstream indicator optioncontrib/completion/git-prompt.sh | 14 ++++-- t/t9903-bash-prompt.sh | 86 ++++++++++++++++++++++++++------
Not about the patch, but a slightly related tangent, but I wonder if we want to update t/t9903-bash-prompt.sh script so that it starts with "#!/bin/bash" not "#!/bin/sh" (this is a reaction to seeing some bash-isms in these patches. No complaints here, as the test script is all about the command line prompt feature to be used with bash and nothing else).
Quoted 7 lines
2 files changed, 80 insertions(+), 20 deletions(-)base-commit:
fd372d9b1a69a01a676398882bbe3840bf51fe72
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2101%2Fivanovkirilg%2Fquiet-option-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2101/ivanovkirilg/quiet-option-v1
Pull-Request: https://github.com/git/git/pull/2101
Quoted 4 lines · Junio C Hamano
... (this is a reaction to seeing
some bash-isms in these patches. No complaints here, as the test script is all about the command line prompt feature to be used with bash and nothing else).
Actually, this got me reconsidering. The prompt feature claims at least to support zsh in addition to bash, and it has had modifications to make it more compliant:
Quoted 3 lines
After this patchset, the following shells work: bash, zsh, dash (since at least 0.5.8), free/net bsd sh, busybox-ash, mksh, openbsd sh, pdksh(!), Schily extended Bourne sh (bosh), yash.
And the test seems to run successfully under 'sh' in master with:
-. ./lib-bash.sh
+. ./test-lib.sh
(Passes normally, and fails when the 'expected' strings are clobbered.)
... Which is all to say that the bash-isms may be undesirable even here.
Unfortunately, I have been struggling (for an embarrassing couple of hours) to replace them with something standard that still keeps the matrix-like form of the test parameters. If anyone more experienced in scripting has an itch to help, it's welcome.
Since I originally intended this to be a two-line change, I'd rather wait to see if it is even a welcome addition before I spend more time on it.
Thanks Junio for the feedback!
Quoted 13 lines · Kiril Ivanov
Quoted 4 lines · Junio C Hamano
... (this is a reaction to seeing
some bash-isms in these patches. No complaints here, as the test script is all about the command line prompt feature to be used with bash and nothing else).Actually, this got me reconsidering. The prompt feature claims at least to support zsh in addition to bash, and it has had modifications to make it more compliant:
Quoted 8 lines
Quoted 3 lines
After this patchset, the following shells work: bash, zsh, dash (since at least 0.5.8), free/net bsd sh, busybox-ash, mksh, openbsd sh, pdksh(!), Schily extended Bourne sh (bosh), yash.
And the test seems to run successfully under 'sh' in master with:
-. ./lib-bash.sh
+. ./test-lib.sh
(Passes normally, and fails when the 'expected' strings are clobbered.)
Ah, good finding. So the inclusion of lib-bash.sh there is wrong, and we shouldn't expect that we'd always run under bash.
Quoted 1 line
... Which is all to say that the bash-isms may be undesirable even here.
You're right. Thanks for correcting me.