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>

Jess Austin committed Jan 6, 2015 at 20:22 UTC 0120b8c85c7a52b26ea7062fec349d178a4e0208
2 files changed +119
contrib/completion/git-prompt.sh
+13
@@ -84,6 +84,11 @@
84 # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
85 # the colored output of "git status -sb" and are available only when
86 # 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".
92
93 # check whether printf supports -v
94 __git_printf_supports_v=
@@ -369,6 +374,14 @@ __git_ps1 ()
374 local inside_gitdir="${repo_info##*$'\n'}"
375 local g="${repo_info%$'\n'*}"
376
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 +
385 local r=""
386 local b=""
387 local step=""
t/t9903-bash-prompt.sh
+106
@@ -35,6 +35,8 @@ test_expect_success 'setup for prompt tests' '
35 git commit -m "another b2" file &&
36 echo 000 >file &&
37 git commit -m "yet another b2" file &&
38 + mkdir ignored_dir &&
39 + echo "ignored_dir/" >>.gitignore &&
40 git checkout master
41 '
42
@@ -588,4 +590,108 @@ test_expect_success 'prompt - zsh color pc mode' '
590 test_cmp expected "$actual"
591 '
592
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 +
697 test_done