Discussion:
[PATCH] git-prompt.sh: Omit prompt for ignored directories
Jess Austin
2014-10-08 19:04:00 UTC
Permalink
Introduce a new environmental variable, GIT_PS1_OMITIGNORED, which
tells __git_ps1 to display nothing when the current directory is
set (e.g. via .gitignore) to be ignored by git. In the absence of
GIT_PS1_OMITIGNORED 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 <***@gmail.com>
---
contrib/completion/git-prompt.sh | 9 +++++++++
t/t9903-bash-prompt.sh | 21 +++++++++++++++++++++
2 files changed, 30 insertions(+)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index c5473dc..6a26cb4 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -84,6 +84,10 @@
# 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_OMITIGNORED
+# to a nonempty value.

# check whether printf supports -v
__git_printf_supports_v=
@@ -501,6 +505,11 @@ __git_ps1 ()
local f="$w$i$s$u"
local gitstring="$c$b${f:+$z$f}$r$p"

+ if [ -n "$(git check-ignore .)" ] && [ -n "${GIT_PS1_OMITIGNORED}" ]
+ 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..55bcb6b 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,23 @@ test_expect_success 'prompt - zsh color pc mode' '
test_cmp expected "$actual"
'

+test_expect_success 'prompt - prompt omitted in ignored directory' '
+ printf "" >expected &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_OMITIGNORED=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - prompt not omitted without GIT_PS1_OMITIGNORED' '
+ printf " (master)" >expected &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
test_done
--
1.9.1
Richard Hansen
2014-10-08 21:12:07 UTC
Permalink
Post by Jess Austin
Introduce a new environmental variable, GIT_PS1_OMITIGNORED, which
tells __git_ps1 to display nothing when the current directory is
set (e.g. via .gitignore) to be ignored by git. In the absence of
GIT_PS1_OMITIGNORED 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.
Interesting idea, though I would prefer this to be configurable on a
per-repository basis. (I wouldn't want to hide the prompt in any
repository besides my home repository.)

I'm not a big fan of the name "OMITIGNORED" (it's not immediately
obvious what this means), but I can't think of anything better off the
top of my head...

-Richard
Post by Jess Austin
---
contrib/completion/git-prompt.sh | 9 +++++++++
t/t9903-bash-prompt.sh | 21 +++++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index c5473dc..6a26cb4 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -84,6 +84,10 @@
# 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_OMITIGNORED
+# to a nonempty value.
# check whether printf supports -v
__git_printf_supports_v=
@@ -501,6 +505,11 @@ __git_ps1 ()
local f="$w$i$s$u"
local gitstring="$c$b${f:+$z$f}$r$p"
+ if [ -n "$(git check-ignore .)" ] && [ -n "${GIT_PS1_OMITIGNORED}" ]
+ 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..55bcb6b 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,23 @@ test_expect_success 'prompt - zsh color pc mode' '
test_cmp expected "$actual"
'
+test_expect_success 'prompt - prompt omitted in ignored directory' '
+ printf "" >expected &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_OMITIGNORED=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - prompt not omitted without GIT_PS1_OMITIGNORED' '
+ printf " (master)" >expected &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
test_done
Jess Austin
2014-10-08 21:37:23 UTC
Permalink
Post by Richard Hansen
Post by Jess Austin
Introduce a new environmental variable, GIT_PS1_OMITIGNORED, which
tells __git_ps1 to display nothing when the current directory is
set (e.g. via .gitignore) to be ignored by git. In the absence of
GIT_PS1_OMITIGNORED 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.
Interesting idea, though I would prefer this to be configurable on a
per-repository basis. (I wouldn't want to hide the prompt in any
repository besides my home repository.)
Sorry my description was unclear. Let's say you have a repo in "~",
and another in "~/projects/foo". Also, the file "~/.gitignore" has the line
"projects/" in it. In this case, you'd see repo info in your prompt while
in "~" or in "~/projects/foo", but not if you were in "~/projects". In that
sense, the prompt is not distracting you with the status of the top-level
repo when you're not looking at anything in that repo.
Post by Richard Hansen
I'm not a big fan of the name "OMITIGNORED" (it's not immediately
obvious what this means), but I can't think of anything better off the
top of my head...
I'm definitely open to suggestions.

cheers,
Jess

ps. sorry for the html before
Post by Richard Hansen
Post by Jess Austin
---
contrib/completion/git-prompt.sh | 9 +++++++++
t/t9903-bash-prompt.sh | 21 +++++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index c5473dc..6a26cb4 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -84,6 +84,10 @@
# 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_OMITIGNORED
+# to a nonempty value.
# check whether printf supports -v
__git_printf_supports_v=
@@ -501,6 +505,11 @@ __git_ps1 ()
local f="$w$i$s$u"
local gitstring="$c$b${f:+$z$f}$r$p"
+ if [ -n "$(git check-ignore .)" ] && [ -n "${GIT_PS1_OMITIGNORED}" ]
+ 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..55bcb6b 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,23 @@ test_expect_success 'prompt - zsh color pc mode' '
test_cmp expected "$actual"
'
+test_expect_success 'prompt - prompt omitted in ignored directory' '
+ printf "" >expected &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_OMITIGNORED=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - prompt not omitted without GIT_PS1_OMITIGNORED' '
+ printf " (master)" >expected &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
test_done
Richard Hansen
2014-10-09 05:37:52 UTC
Permalink
Post by Jess Austin
Post by Richard Hansen
Post by Jess Austin
Introduce a new environmental variable, GIT_PS1_OMITIGNORED, which
tells __git_ps1 to display nothing when the current directory is
set (e.g. via .gitignore) to be ignored by git. In the absence of
GIT_PS1_OMITIGNORED 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.
Interesting idea, though I would prefer this to be configurable on a
per-repository basis. (I wouldn't want to hide the prompt in any
repository besides my home repository.)
Sorry my description was unclear. Let's say you have a repo in "~",
and another in "~/projects/foo". Also, the file "~/.gitignore" has the line
"projects/" in it. In this case, you'd see repo info in your prompt while
in "~" or in "~/projects/foo", but not if you were in "~/projects". In that
sense, the prompt is not distracting you with the status of the top-level
repo when you're not looking at anything in that repo.
I understand; I was concerned about this case:

$ PS1='\n\w$(__git_ps1 " (%s)")\n\$ '

/home/rhansen/projects (dotfiles)
$ GIT_PS1_OMITIGNORED=y

/home/rhansen/projects <-- Git prompt goes away as desired
$ cd foo

/home/rhansen/projects/foo (master) <-- Git prompt back as expected
$ echo ignored/ >>.gitignore && mkdir -p ignored && cd ignored

/home/rhansen/projects/foo/ignored <-- I want the Git prompt here
$

In other words: If I were to use this feature, I'd want to be able to
hide the prompt when I'm in an ignored directory in my dotfiles work
tree, but show the prompt when I'm in an ignored directory in any other
work tree.

-Richard
Jess Austin
2014-10-09 10:27:16 UTC
Permalink
Post by Richard Hansen
Post by Jess Austin
Post by Richard Hansen
Post by Jess Austin
Introduce a new environmental variable, GIT_PS1_OMITIGNORED, which
tells __git_ps1 to display nothing when the current directory is
set (e.g. via .gitignore) to be ignored by git. In the absence of
GIT_PS1_OMITIGNORED 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.
Interesting idea, though I would prefer this to be configurable on a
per-repository basis. (I wouldn't want to hide the prompt in any
repository besides my home repository.)
Sorry my description was unclear. Let's say you have a repo in "~",
and another in "~/projects/foo". Also, the file "~/.gitignore" has the line
"projects/" in it. In this case, you'd see repo info in your prompt while
in "~" or in "~/projects/foo", but not if you were in "~/projects". In that
sense, the prompt is not distracting you with the status of the top-level
repo when you're not looking at anything in that repo.
$ PS1='\n\w$(__git_ps1 " (%s)")\n\$ '
/home/rhansen/projects (dotfiles)
$ GIT_PS1_OMITIGNORED=y
/home/rhansen/projects <-- Git prompt goes away as desired
$ cd foo
/home/rhansen/projects/foo (master) <-- Git prompt back as expected
$ echo ignored/ >>.gitignore && mkdir -p ignored && cd ignored
/home/rhansen/projects/foo/ignored <-- I want the Git prompt here
$
In other words: If I were to use this feature, I'd want to be able to
hide the prompt when I'm in an ignored directory in my dotfiles work
tree, but show the prompt when I'm in an ignored directory in any other
work tree.
Would you want this configured in each repo (i.e. via a line in ".git/config"),
or would you prefer something global so that it only need be set in one
place? I'm not sure how the latter technique would work, so if that seems
better please advise on how to go about that.

cheers,
Jess
Richard Hansen
2014-10-09 22:09:15 UTC
Permalink
Post by Jess Austin
Post by Richard Hansen
Post by Jess Austin
Introduce a new environmental variable, GIT_PS1_OMITIGNORED, which
tells __git_ps1 to display nothing when the current directory is
set (e.g. via .gitignore) to be ignored by git. In the absence of
GIT_PS1_OMITIGNORED 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.
...
Post by Jess Austin
Post by Richard Hansen
$ PS1='\n\w$(__git_ps1 " (%s)")\n\$ '
/home/rhansen/projects (dotfiles)
$ GIT_PS1_OMITIGNORED=y
/home/rhansen/projects <-- Git prompt goes away as desired
$ cd foo
/home/rhansen/projects/foo (master) <-- Git prompt back as expected
$ echo ignored/ >>.gitignore && mkdir -p ignored && cd ignored
/home/rhansen/projects/foo/ignored <-- I want the Git prompt here
$
In other words: If I were to use this feature, I'd want to be able to
hide the prompt when I'm in an ignored directory in my dotfiles work
tree, but show the prompt when I'm in an ignored directory in any other
work tree.
Would you want this configured in each repo (i.e. via a line in ".git/config"),
or would you prefer something global so that it only need be set in one
place? I'm not sure how the latter technique would work, so if that seems
better please advise on how to go about that.
A 'git config' variable is fine. The bash.showDirtyState,
bash.showUntrackedFiles, and bash.showUpstream config variables seem
like good examples to follow.

-Richard
Jess Austin
2014-10-14 02:32:04 UTC
Permalink
Set __git_ps1 to display nothing when present working directory is
ignored, triggered by either the new environmental variable
GIT_PS1_HIDE_ON_IGNORED_PWD or the new repository configuration
variable bash.hideOnIgnoredPwd (or both). In the absence of these
settings 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 <***@gmail.com>
---
Post by Richard Hansen
Post by Jess Austin
Would you want this configured in each repo (i.e. via a line in ".git/config"),
or would you prefer something global so that it only need be set in one
place? I'm not sure how the latter technique would work, so if that seems
better please advise on how to go about that.
A 'git config' variable is fine. The bash.showDirtyState,
bash.showUntrackedFiles, and bash.showUpstream config variables seem
like good examples to follow.
I think this is what you meant. I changed the name of the envvar. Now the
variables are GIT_PS1_HIDE_ON_IGNORED_PWD and bash.hideOnIgnoredPwd. I
admit these are still kind of unwieldy, but maybe now they're more descriptive?

Please advise!

cheers,
Jess

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..d7559ff 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_ON_IGNORED_PWD to a nonempty value, or set
+# bash.hideOnIgnoredPwd to true in the repository configuration.

# 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 check-ignore .)" ] &&
+ ( [ -n "${GIT_PS1_HIDE_ON_IGNORED_PWD}" ] ||
+ [ "$(git config --bool bash.hideOnIgnoredPwd)" = "true" ] )
+ 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..a8ef8a3 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 on ignored pwd - shell variable unset with config disabled' '
+ printf " (master)" >expected &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide on ignored pwd - shell variable unset with config enabled' '
+ printf "" >expected &&
+ test_config bash.hideOnIgnoredPwd true &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide on ignored pwd - shell variable set with config disabled' '
+ printf "" >expected &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_HIDE_ON_IGNORED_PWD=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide on ignored pwd - shell variable set with config enabled' '
+ printf "" >expected &&
+ test_config bash.hideOnIgnoredPwd true &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_HIDE_ON_IGNORED_PWD=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
test_done
--
1.9.1
Johannes Sixt
2014-10-14 18:47:51 UTC
Permalink
Post by Jess Austin
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index c5473dc..d7559ff 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_ON_IGNORED_PWD to a nonempty value, or set
+# bash.hideOnIgnoredPwd to true in the repository configuration.
# 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 check-ignore .)" ] &&
+ ( [ -n "${GIT_PS1_HIDE_ON_IGNORED_PWD}" ] ||
+ [ "$(git config --bool bash.hideOnIgnoredPwd)" = "true" ] )
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.

BTW, you can write '{ foo || bar; }' to bracket a || chain without a
sub-process.
Post by Jess Austin
+ then
+ printf_format=""
+ fi
+
if [ $pcmode = yes ]; then
if [ "${__git_printf_supports_v-}" != yes ]; then
gitstring=$(printf -- "$printf_format" "$gitstring")
-- Hannes
Richard Hansen
2014-10-14 19:08:07 UTC
Permalink
Post by Johannes Sixt
Post by Jess Austin
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index c5473dc..d7559ff 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_ON_IGNORED_PWD to a nonempty value, or set
+# bash.hideOnIgnoredPwd to true in the repository configuration.
# 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 check-ignore .)" ] &&
+ ( [ -n "${GIT_PS1_HIDE_ON_IGNORED_PWD}" ] ||
+ [ "$(git config --bool bash.hideOnIgnoredPwd)" = "true" ] )
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.
Is this why bash.showDirtyState and friends aren't checked unless the
corresponding environment variable is set to a non-empty value?

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
...

-Richard
Post by Johannes Sixt
BTW, you can write '{ foo || bar; }' to bracket a || chain without a
sub-process.
Post by Jess Austin
+ then
+ printf_format=""
+ fi
+
if [ $pcmode = yes ]; then
if [ "${__git_printf_supports_v-}" != yes ]; then
gitstring=$(printf -- "$printf_format" "$gitstring")
-- Hannes
Jess Austin
2014-10-15 04:06:33 UTC
Permalink
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 <***@gmail.com>
---
Post by Richard Hansen
Post by Johannes Sixt
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
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.
Post by Richard Hansen
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.
Post by Richard Hansen
To avoid scaring people who might not want this feature enabled, I
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
Richard Hansen
2014-10-15 20:28:54 UTC
Permalink
Post by Jess Austin
@@ -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")
This is broken in pcmode due to a Bash bug. The command:
printf -v foo "" asdf
is a no-op in Bash. The variable foo is never changed in any way --
it is neither unset nor set to the empty string.

Also, I noticed that I get an error message if I cd into .git:
fatal: This operation must be run in a work tree

I think the following change will fix the above issues, and it has the
advantage of avoiding unnecessary work if the directory is ignored:

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 6a4ce53..68ac82a 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -374,6 +374,17 @@ __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
+ if [ $pcmode = yes ]; then
+ PS1="$ps1pc_start$ps1pc_end"
+ fi
+ return
+ fi
+
local r=""
local b=""
local step=""
@@ -506,13 +517,6 @@ __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")

It would be good to add additional test cases for pcmode (two or three
arguments to __git_ps1) and 'cd .git' so that the above issues don't
reappear.

Thanks,
Richard

Richard Hansen
2014-10-14 19:21:48 UTC
Permalink
Post by Jess Austin
Set __git_ps1 to display nothing when present working directory is
ignored, triggered by either the new environmental variable
GIT_PS1_HIDE_ON_IGNORED_PWD or the new repository configuration
variable bash.hideOnIgnoredPwd (or both). In the absence of these
settings 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.
---
Post by Richard Hansen
Post by Jess Austin
Would you want this configured in each repo (i.e. via a line in ".git/config"),
or would you prefer something global so that it only need be set in one
place? I'm not sure how the latter technique would work, so if that seems
better please advise on how to go about that.
A 'git config' variable is fine. The bash.showDirtyState,
bash.showUntrackedFiles, and bash.showUpstream config variables seem
like good examples to follow.
I think this is what you meant. I changed the name of the envvar. Now the
variables are GIT_PS1_HIDE_ON_IGNORED_PWD and bash.hideOnIgnoredPwd. I
admit these are still kind of unwieldy, but maybe now they're more descriptive?
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.)

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
Post by Jess Austin
Please advise!
cheers,
Jess
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..d7559ff 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_ON_IGNORED_PWD to a nonempty value, or set
+# bash.hideOnIgnoredPwd to true in the repository configuration.
As mentioned in my previous email, I would prefer the code to follow the
behavior of the other config variables (the environment variable has to
be set *and* the config variable has to be non-false).
Post by Jess Austin
# 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 check-ignore .)" ] &&
Rather than:

[ -n "$(git check-ignore .)" ]

I would prefer:

git check-ignore -q .

For example:

if [ -n "${GIT_PS1_HIDE_ON_IGNORED_PWD}" ] &&
[ "$(git config --bool bash.hideOnIgnoredPwd)" != "false" ] &&
git check-ignore -q .
then
...

-Richard
Post by Jess Austin
+ ( [ -n "${GIT_PS1_HIDE_ON_IGNORED_PWD}" ] ||
+ [ "$(git config --bool bash.hideOnIgnoredPwd)" = "true" ] )
+ 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..a8ef8a3 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 on ignored pwd - shell variable unset with config disabled' '
+ printf " (master)" >expected &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide on ignored pwd - shell variable unset with config enabled' '
+ printf "" >expected &&
+ test_config bash.hideOnIgnoredPwd true &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide on ignored pwd - shell variable set with config disabled' '
+ printf "" >expected &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_HIDE_ON_IGNORED_PWD=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide on ignored pwd - shell variable set with config enabled' '
+ printf "" >expected &&
+ test_config bash.hideOnIgnoredPwd true &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_HIDE_ON_IGNORED_PWD=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
test_done
Loading...