Skip to content

Commit

Permalink
Merge branch 'rh/hide-prompt-in-ignored-directory'
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Junio C Hamano committed Jan 14, 2015
2 parents 1e7ef5d + 0120b8c commit e1ef7d1
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 7 deletions.
24 changes: 17 additions & 7 deletions contrib/completion/git-prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -300,6 +305,10 @@ __git_ps1 ()
ps1pc_start="$1"
ps1pc_end="$2"
printf_format="${3:-$printf_format}"
# set PS1 to a plain prompt so that we can
# simply return early if the prompt should not
# be decorated
PS1="$ps1pc_start$ps1pc_end"
;;
0|1) printf_format="${1:-$printf_format}"
;;
Expand Down Expand Up @@ -351,10 +360,6 @@ __git_ps1 ()
rev_parse_exit_code="$?"

if [ -z "$repo_info" ]; then
if [ $pcmode = yes ]; then
#In PC mode PS1 always needs to be set
PS1="$ps1pc_start$ps1pc_end"
fi
return
fi

Expand All @@ -370,6 +375,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=""
Expand Down Expand Up @@ -413,9 +426,6 @@ __git_ps1 ()
else
local head=""
if ! __git_eread "$g/HEAD" head; then
if [ $pcmode = yes ]; then
PS1="$ps1pc_start$ps1pc_end"
fi
return
fi
# is it a symbolic ref?
Expand Down
106 changes: 106 additions & 0 deletions t/t9903-bash-prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
'

Expand Down Expand Up @@ -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

0 comments on commit e1ef7d1

Please sign in to comment.