Skip to content

Commit 0120b8c

Browse files
jessaustingitster
authored andcommitted
git-prompt.sh: allow to hide prompt for ignored pwd
Optionally set __git_ps1 to display nothing when present working directory is ignored, triggered by the new environment variable GIT_PS1_HIDE_IF_PWD_IGNORED. This environment variable may be overridden on any repository by setting bash.hideIfPwdIgnored to "false". In the absence of GIT_PS1_HIDE_IF_PWD_IGNORED this change has no effect. Many people manage e.g. dotfiles in their home directory with git. This causes the prompt generated by __git_ps1 to refer to that "top level" repo while working in any descendant directory. That can be distracting, so this patch helps one shut off that noise. Signed-off-by: Jess Austin <jess.austin@gmail.com> Signed-off-by: Richard Hansen <rhansen@bbn.com> Reviewed-by: Richard Hansen <rhansen@bbn.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 76b4309 commit 0120b8c

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

contrib/completion/git-prompt.sh

Lines changed: 13 additions & 0 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=
@@ -369,6 +374,14 @@ __git_ps1 ()
369374
local inside_gitdir="${repo_info##*$'\n'}"
370375
local g="${repo_info%$'\n'*}"
371376

377+
if [ "true" = "$inside_worktree" ] &&
378+
[ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED-}" ] &&
379+
[ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] &&
380+
git check-ignore -q .
381+
then
382+
return
383+
fi
384+
372385
local r=""
373386
local b=""
374387
local step=""

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)