From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jess Austin Subject: [PATCH] git-prompt.sh: Option to hide prompt for ignored pwd Date: Tue, 14 Oct 2014 23:06:33 -0500 Message-ID: <1413345993-14393-1-git-send-email-jess.austin@gmail.com> References: <543D77CC.8060803@bbn.com> <1412795040-19267-1-git-send-email-jess.austin@gmail.com> <5435A8A7.2030008@bbn.com> <54361F30.8020603@bbn.com> <5437078B.6020307@bbn.com> <1413253924-8065-1-git-send-email-jess.austin@gmail.com> <543D6FD7.3090306@kdbg.org> Cc: Jess Austin , Johannes Sixt , git@vger.kernel.org To: Richard Hansen X-From: git-owner@vger.kernel.org Wed Oct 15 06:07:31 2014 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XeFs1-0000gV-9j for gcvg-git-2@plane.gmane.org; Wed, 15 Oct 2014 06:07:29 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750781AbaJOEHE (ORCPT ); Wed, 15 Oct 2014 00:07:04 -0400 Received: from mail-ie0-f172.google.com ([209.85.223.172]:40565 "EHLO mail-ie0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750721AbaJOEHD (ORCPT ); Wed, 15 Oct 2014 00:07:03 -0400 Received: by mail-ie0-f172.google.com with SMTP id rl12so467575iec.3 for ; Tue, 14 Oct 2014 21:07:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=cajAFhBJYhpu+6n0AeTZ32XhmRibCnuTCkBgbaTXeBQ=; b=B1ixdT3vUPV6T/OYnjbZAWzPG39VDTw7SN1om++N/n7qd3d9IRV6MqO5hoR2P7xRgW yZOFweWGeeWkA2DhotpIjnpMP64OmjKiDjKOTRvwaU9hO62dPaq1RK40TWJ+DViS45Hc 0/ocqsTQMTkB4MkepY/6Us4ULT/UF/2c2kamsrjCadnXNdt3CgHpzicnQ5/6uMyBbELb TrY/0JhZZ7EGPl+3r9csNkdGM/ua6+we8jRuAcQyhA51u9uIJsIGCDdxZZfvVKwrpM/w UKabxq20bzT9VTyTSCi0zl1feQY4rqk7ar1F1RP62yLjbu8Hn7nCxEioedDeuPInmcm5 3OcQ== X-Received: by 10.42.175.202 with SMTP id bb10mr8843552icb.45.1413346022185; Tue, 14 Oct 2014 21:07:02 -0700 (PDT) Received: from localhost.localdomain (66-128-122-45.static.stls.mo.charter.com. [66.128.122.45]) by mx.google.com with ESMTPSA id x9sm12549856igl.10.2014.10.14.21.07.00 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 14 Oct 2014 21:07:00 -0700 (PDT) X-Mailer: git-send-email 1.9.1 In-Reply-To: <543D77CC.8060803@bbn.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Optionally set __git_ps1 to display nothing when present working directory is ignored, triggered by the new environmental variable GIT_PS1_HIDE_IF_PWD_IGNORED. This environmental 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 --- On Tue, Oct 14, 2014 at 2:08 PM, Richard Hansen wrote: > On 2014-10-14 14:47, Johannes Sixt wrote: >> Ahem, no. Please do not punish users who are not interested in the new >> feature with two new processes every time __git_ps() is run. Think of >> Windows where fork() is really, *really* expensive. > Regardless, it would be nice if the behavior matched the other bash.* > variables (only check the bash.* variable if the corresponding > environment variable is set, and default to true). The following should > fix it: > > if [ -n "${GIT_PS1_HIDE_ON_IGNORED_PWD}" ] && > [ "$(git config --bool bash.hideOnIgnoredPwd)" != "false" ] && > [ "$(git check-ignore .)" ] > then Thanks for helping me understand this! I think I have it correct now. On Tue, Oct 14, 2014 at 2:21 PM, Richard Hansen wrote: > I do prefer the new names. They are long, but how often will someone > have to type it? In this case it's better to be descriptive than to be > short. (I wonder if adding two letters would improve readability > further: GIT_PS1_HIDE_WHEN_PWD_IGNORED and bash.hideWhenPwdIgnored.) We got those two letters back with GIT_PS1_HIDE_IF_PWD_IGNORED and bash.hideIfPwdIgnored. > To avoid scaring people who might not want this feature enabled, I > recommend changing the subject line to something like this: > > git-prompt.sh: Option to hide prompt for ignored pwd Good idea! contrib/completion/git-prompt.sh | 12 ++++++++++++ t/t9903-bash-prompt.sh | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index c5473dc..151218b 100644 --- a/contrib/completion/git-prompt.sh +++ b/contrib/completion/git-prompt.sh @@ -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= @@ -501,6 +506,13 @@ __git_ps1 () local f="$w$i$s$u" local gitstring="$c$b${f:+$z$f}$r$p" + if [ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED}" ] && + [ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] && + git check-ignore -q . + then + printf_format="" + fi + if [ $pcmode = yes ]; then if [ "${__git_printf_supports_v-}" != yes ]; then gitstring=$(printf -- "$printf_format" "$gitstring") diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh index 9150984..88a75cf 100755 --- a/t/t9903-bash-prompt.sh +++ b/t/t9903-bash-prompt.sh @@ -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,44 @@ test_expect_success 'prompt - zsh color pc mode' ' test_cmp expected "$actual" ' +test_expect_success 'prompt - hide if pwd ignored - shell variable unset with 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 - shell variable unset with config unset' ' + printf " (master)" >expected && + ( + cd ignored_dir && + __git_ps1 >"$actual" + ) && + test_cmp expected "$actual" +' + +test_expect_success 'prompt - hide if pwd ignored - shell variable set with 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 - shell variable set with config unset' ' + printf "" >expected && + ( + cd ignored_dir && + GIT_PS1_HIDE_IF_PWD_IGNORED=y && + __git_ps1 >"$actual" + ) && + test_cmp expected "$actual" +' + test_done -- 1.9.1