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>
This commit is contained in:
Jess Austin 2015-01-06 20:22:27 -05:00 committed by Junio C Hamano
parent 76b4309400
commit 0120b8c85c
2 changed files with 119 additions and 0 deletions

View File

@ -84,6 +84,11 @@
# GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
# the colored output of "git status -sb" and are available only when
# using __git_ps1 for PROMPT_COMMAND or precmd.
#
# If you would like __git_ps1 to do nothing in the case when the current
# directory is set up to be ignored by git, then set
# GIT_PS1_HIDE_IF_PWD_IGNORED to a nonempty value. Override this on the
# repository level by setting bash.hideIfPwdIgnored to "false".
# check whether printf supports -v
__git_printf_supports_v=
@ -369,6 +374,14 @@ __git_ps1 ()
local inside_gitdir="${repo_info##*$'\n'}"
local g="${repo_info%$'\n'*}"
if [ "true" = "$inside_worktree" ] &&
[ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED-}" ] &&
[ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] &&
git check-ignore -q .
then
return
fi
local r=""
local b=""
local step=""

View File

@ -35,6 +35,8 @@ test_expect_success 'setup for prompt tests' '
git commit -m "another b2" file &&
echo 000 >file &&
git commit -m "yet another b2" file &&
mkdir ignored_dir &&
echo "ignored_dir/" >>.gitignore &&
git checkout master
'
@ -588,4 +590,108 @@ test_expect_success 'prompt - zsh color pc mode' '
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - env var unset, config disabled' '
printf " (master)" >expected &&
test_config bash.hideIfPwdIgnored false &&
(
cd ignored_dir &&
__git_ps1 >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - env var unset, config disabled, pc mode' '
printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
test_config bash.hideIfPwdIgnored false &&
(
cd ignored_dir &&
__git_ps1 "BEFORE:" ":AFTER" &&
printf "%s" "$PS1" >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - env var unset, config unset' '
printf " (master)" >expected &&
(
cd ignored_dir &&
__git_ps1 >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - env var unset, config unset, pc mode' '
printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
(
cd ignored_dir &&
__git_ps1 "BEFORE:" ":AFTER" &&
printf "%s" "$PS1" >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - env var set, config disabled' '
printf " (master)" >expected &&
test_config bash.hideIfPwdIgnored false &&
(
cd ignored_dir &&
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
__git_ps1 >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - env var set, config disabled, pc mode' '
printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
test_config bash.hideIfPwdIgnored false &&
(
cd ignored_dir &&
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
__git_ps1 "BEFORE:" ":AFTER" &&
printf "%s" "$PS1" >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - env var set, config unset' '
printf "" >expected &&
(
cd ignored_dir &&
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
__git_ps1 >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - env var set, config unset, pc mode' '
printf "BEFORE::AFTER" >expected &&
(
cd ignored_dir &&
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
__git_ps1 "BEFORE:" ":AFTER" &&
printf "%s" "$PS1" >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stdout)' '
printf " (GIT_DIR!)" >expected &&
(
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
cd .git &&
__git_ps1 >"$actual" 2>/dev/null
) &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stderr)' '
printf "" >expected &&
(
GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
cd .git &&
__git_ps1 >/dev/null 2>"$actual"
) &&
test_cmp expected "$actual"
'
test_done