Skip to content

Commit e1ef7d1

Browse files
committed
Merge branch 'rh/hide-prompt-in-ignored-directory'
* rh/hide-prompt-in-ignored-directory: git-prompt.sh: allow to hide prompt for ignored pwd git-prompt.sh: if pc mode, immediately set PS1 to a plain prompt
2 parents 1e7ef5d + 0120b8c commit e1ef7d1

File tree

2 files changed

+123
-7
lines changed

2 files changed

+123
-7
lines changed

contrib/completion/git-prompt.sh

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
# GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
8585
# the colored output of "git status -sb" and are available only when
8686
# using __git_ps1 for PROMPT_COMMAND or precmd.
87+
#
88+
# If you would like __git_ps1 to do nothing in the case when the current
89+
# directory is set up to be ignored by git, then set
90+
# GIT_PS1_HIDE_IF_PWD_IGNORED to a nonempty value. Override this on the
91+
# repository level by setting bash.hideIfPwdIgnored to "false".
8792

8893
# check whether printf supports -v
8994
__git_printf_supports_v=
@@ -300,6 +305,10 @@ __git_ps1 ()
300305
ps1pc_start="$1"
301306
ps1pc_end="$2"
302307
printf_format="${3:-$printf_format}"
308+
# set PS1 to a plain prompt so that we can
309+
# simply return early if the prompt should not
310+
# be decorated
311+
PS1="$ps1pc_start$ps1pc_end"
303312
;;
304313
0|1) printf_format="${1:-$printf_format}"
305314
;;
@@ -351,10 +360,6 @@ __git_ps1 ()
351360
rev_parse_exit_code="$?"
352361

353362
if [ -z "$repo_info" ]; then
354-
if [ $pcmode = yes ]; then
355-
#In PC mode PS1 always needs to be set
356-
PS1="$ps1pc_start$ps1pc_end"
357-
fi
358363
return
359364
fi
360365

@@ -370,6 +375,14 @@ __git_ps1 ()
370375
local inside_gitdir="${repo_info##*$'\n'}"
371376
local g="${repo_info%$'\n'*}"
372377

378+
if [ "true" = "$inside_worktree" ] &&
379+
[ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED-}" ] &&
380+
[ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] &&
381+
git check-ignore -q .
382+
then
383+
return
384+
fi
385+
373386
local r=""
374387
local b=""
375388
local step=""
@@ -413,9 +426,6 @@ __git_ps1 ()
413426
else
414427
local head=""
415428
if ! __git_eread "$g/HEAD" head; then
416-
if [ $pcmode = yes ]; then
417-
PS1="$ps1pc_start$ps1pc_end"
418-
fi
419429
return
420430
fi
421431
# is it a symbolic ref?

t/t9903-bash-prompt.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ test_expect_success 'setup for prompt tests' '
3535
git commit -m "another b2" file &&
3636
echo 000 >file &&
3737
git commit -m "yet another b2" file &&
38+
mkdir ignored_dir &&
39+
echo "ignored_dir/" >>.gitignore &&
3840
git checkout master
3941
'
4042

@@ -588,4 +590,108 @@ test_expect_success 'prompt - zsh color pc mode' '
588590
test_cmp expected "$actual"
589591
'
590592

593+
test_expect_success 'prompt - hide if pwd ignored - env var unset, config disabled' '
594+
printf " (master)" >expected &&
595+
test_config bash.hideIfPwdIgnored false &&
596+
(
597+
cd ignored_dir &&
598+
__git_ps1 >"$actual"
599+
) &&
600+
test_cmp expected "$actual"
601+
'
602+
603+
test_expect_success 'prompt - hide if pwd ignored - env var unset, config disabled, pc mode' '
604+
printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
605+
test_config bash.hideIfPwdIgnored false &&
606+
(
607+
cd ignored_dir &&
608+
__git_ps1 "BEFORE:" ":AFTER" &&
609+
printf "%s" "$PS1" >"$actual"
610+
) &&
611+
test_cmp expected "$actual"
612+
'
613+
614+
test_expect_success 'prompt - hide if pwd ignored - env var unset, config unset' '
615+
printf " (master)" >expected &&
616+
(
617+
cd ignored_dir &&
618+
__git_ps1 >"$actual"
619+
) &&
620+
test_cmp expected "$actual"
621+
'
622+
623+
test_expect_success 'prompt - hide if pwd ignored - env var unset, config unset, pc mode' '
624+
printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
625+
(
626+
cd ignored_dir &&
627+
__git_ps1 "BEFORE:" ":AFTER" &&
628+
printf "%s" "$PS1" >"$actual"
629+
) &&
630+
test_cmp expected "$actual"
631+
'
632+
633+
test_expect_success 'prompt - hide if pwd ignored - env var set, config disabled' '
634+
printf " (master)" >expected &&
635+
test_config bash.hideIfPwdIgnored false &&
636+
(
637+
cd ignored_dir &&
638+
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
639+
__git_ps1 >"$actual"
640+
) &&
641+
test_cmp expected "$actual"
642+
'
643+
644+
test_expect_success 'prompt - hide if pwd ignored - env var set, config disabled, pc mode' '
645+
printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
646+
test_config bash.hideIfPwdIgnored false &&
647+
(
648+
cd ignored_dir &&
649+
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
650+
__git_ps1 "BEFORE:" ":AFTER" &&
651+
printf "%s" "$PS1" >"$actual"
652+
) &&
653+
test_cmp expected "$actual"
654+
'
655+
656+
test_expect_success 'prompt - hide if pwd ignored - env var set, config unset' '
657+
printf "" >expected &&
658+
(
659+
cd ignored_dir &&
660+
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
661+
__git_ps1 >"$actual"
662+
) &&
663+
test_cmp expected "$actual"
664+
'
665+
666+
test_expect_success 'prompt - hide if pwd ignored - env var set, config unset, pc mode' '
667+
printf "BEFORE::AFTER" >expected &&
668+
(
669+
cd ignored_dir &&
670+
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
671+
__git_ps1 "BEFORE:" ":AFTER" &&
672+
printf "%s" "$PS1" >"$actual"
673+
) &&
674+
test_cmp expected "$actual"
675+
'
676+
677+
test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stdout)' '
678+
printf " (GIT_DIR!)" >expected &&
679+
(
680+
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
681+
cd .git &&
682+
__git_ps1 >"$actual" 2>/dev/null
683+
) &&
684+
test_cmp expected "$actual"
685+
'
686+
687+
test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stderr)' '
688+
printf "" >expected &&
689+
(
690+
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
691+
cd .git &&
692+
__git_ps1 >/dev/null 2>"$actual"
693+
) &&
694+
test_cmp expected "$actual"
695+
'
696+
591697
test_done

0 commit comments

Comments
 (0)