threads / patch / 37693

patchgit-prompt.sh: Omit prompt for ignored directories

Subject: [PATCH] git-prompt.sh: Omit prompt for ignored directories

## tl;dr

18 messages between Oct 8, 2014 and Jan 14, 2015. Diffs are folded; open one to read it.

replies: 17people: 5as markdown or json

Jess Austin· Oct 8, 2014, 19:04 UTC · lore

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 <jess.austin@gmail.com>
---
 contrib/completion/git-prompt.sh |  9 +++++++++
 t/t9903-bash-prompt.sh           | 21 +++++++++++++++++++++
 2 files changed, 30 insertions(+)
Show changes to 2 files +30 −0

contrib/completion/git-prompt.sh, t/t9903-bash-prompt.sh

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· Oct 8, 2014, 21:12 UTC · re: Jess Austin · lore

Re: [PATCH] git-prompt.sh: Omit prompt for ignored directories

On 2014-10-08 15:04, Jess Austin wrote:
Show 9 quoted lines
> 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
Show 72 quoted lines
> 
> Signed-off-by: Jess Austin <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
> 
Jess Austin· Oct 8, 2014, 21:37 UTC · lore

Fwd: [PATCH] git-prompt.sh: Omit prompt for ignored directories

On Wed, Oct 8, 2014 at 4:12 PM, Richard Hansen <rhansen@bbn.com> wrote:
Show 15 quoted lines
>
> On 2014-10-08 15:04, Jess Austin wrote:
> > 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'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
Show 72 quoted lines
> > Signed-off-by: Jess Austin <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
> >
>
Richard Hansen· Oct 9, 2014, 05:37 UTC · re: Jess Austin · lore

Re: [PATCH] git-prompt.sh: Omit prompt for ignored directories

On 2014-10-08 17:37, Jess Austin wrote:
Show 22 quoted lines
> On Wed, Oct 8, 2014 at 4:12 PM, Richard Hansen <rhansen@bbn.com> wrote:
>> On 2014-10-08 15:04, Jess Austin wrote:
>>> 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· Oct 9, 2014, 10:27 UTC · re: Richard Hansen · lore

Re: [PATCH] git-prompt.sh: Omit prompt for ignored directories

On Thu, Oct 9, 2014 at 12:37 AM, Richard Hansen <rhansen@bbn.com> wrote:
Show 44 quoted lines
> On 2014-10-08 17:37, Jess Austin wrote:
>> On Wed, Oct 8, 2014 at 4:12 PM, Richard Hansen <rhansen@bbn.com> wrote:
>>> On 2014-10-08 15:04, Jess Austin wrote:
>>>> 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.

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· Oct 9, 2014, 22:09 UTC · re: Jess Austin · lore

Re: [PATCH] git-prompt.sh: Omit prompt for ignored directories

On 2014-10-09 06:27, Jess Austin wrote:
Show 13 quoted lines
> On Thu, Oct 9, 2014 at 12:37 AM, Richard Hansen <rhansen@bbn.com> wrote:
>> On 2014-10-08 17:37, Jess Austin wrote:
>>> On Wed, Oct 8, 2014 at 4:12 PM, Richard Hansen <rhansen@bbn.com> wrote:
>>>> On 2014-10-08 15:04, Jess Austin wrote:
>>>>> 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.
...
Show 24 quoted lines
>>
>> $ 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· Oct 14, 2014, 02:32 UTC · re: Richard Hansen · lore

[PATCH] git-prompt.sh: Hide prompt for ignored pwd

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 <jess.austin@gmail.com>
---
On Thu, Oct 9, 2014 at 5:09 PM, Richard Hansen <rhansen@bbn.com> wrote:
Show 9 quoted lines
> On 2014-10-09 06:27, Jess Austin wrote:
>> 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(+)
Show changes to 2 files +54 −0

contrib/completion/git-prompt.sh, t/t9903-bash-prompt.sh

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· Oct 14, 2014, 18:47 UTC · re: Jess Austin · lore

Re: [PATCH] git-prompt.sh: Hide prompt for ignored pwd

Am 14.10.2014 um 04:32 schrieb Jess Austin:
Show 23 quoted lines
> 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.

Show 7 quoted lines
> +	then
> +		printf_format=""
> +	fi
> +
>  	if [ $pcmode = yes ]; then
>  		if [ "${__git_printf_supports_v-}" != yes ]; then
>  			gitstring=$(printf -- "$printf_format" "$gitstring")
-- Hannes
Richard Hansen· Oct 14, 2014, 19:08 UTC · re: Johannes Sixt · lore

Re: [PATCH] git-prompt.sh: Hide prompt for ignored pwd

On 2014-10-14 14:47, Johannes Sixt wrote:
Show 28 quoted lines
> Am 14.10.2014 um 04:32 schrieb 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
Show 14 quoted lines
> 
> BTW, you can write '{ foo || bar; }' to bracket a || chain without a
> sub-process.
> 
>> +	then
>> +		printf_format=""
>> +	fi
>> +
>>  	if [ $pcmode = yes ]; then
>>  		if [ "${__git_printf_supports_v-}" != yes ]; then
>>  			gitstring=$(printf -- "$printf_format" "$gitstring")
> 
> -- Hannes
> 
Richard Hansen· Oct 14, 2014, 19:21 UTC · re: Jess Austin · lore

Re: [PATCH] git-prompt.sh: Hide prompt for ignored pwd

On 2014-10-13 22:32, Jess Austin wrote:
Show 27 quoted lines
> 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 <jess.austin@gmail.com>
> ---
> On Thu, Oct 9, 2014 at 5:09 PM, Richard Hansen <rhansen@bbn.com> wrote:
>> On 2014-10-09 06:27, Jess Austin wrote:
>>> 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
Show 23 quoted lines
> 
> 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).

Show 8 quoted lines
>  
>  # 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
Show 68 quoted lines
> +	   ( [ -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
> 
Jess Austin· Oct 15, 2014, 04:06 UTC · re: Richard Hansen · lore

[PATCH] git-prompt.sh: Option to hide prompt for ignored pwd

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 <jess.austin@gmail.com>
---
On Tue, Oct 14, 2014 at 2:08 PM, Richard Hansen <rhansen@bbn.com> wrote:
Show 13 quoted lines
> 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 <rhansen@bbn.com> 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(+)
Show changes to 2 files +54 −0

contrib/completion/git-prompt.sh, t/t9903-bash-prompt.sh

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· Oct 15, 2014, 20:28 UTC · re: Jess Austin · lore

Re: [PATCH] git-prompt.sh: Option to hide prompt for ignored pwd

On 2014-10-15 00:06, Jess Austin wrote:
Show 14 quoted lines
> @@ -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:

Show changes to contrib/completion/git-prompt.sh +11 −7
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· Jan 5, 2015, 07:03 UTC · re: Richard Hansen · lore

[PATCH v4] git-prompt.sh: Option to hide prompt for ignored pwd

From: Jess Austin <jess.austin@gmail.com>

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 <jess.austin@gmail.com>
Signed-off-by: Richard Hansen <rhansen@bbn.com>
Reviewed-by: Richard Hansen <rhansen@bbn.com>
---
This is the patch from:
  http://article.gmane.org/gmane.comp.version-control.git/258313
modified to include the changes I suggested in:
  http://article.gmane.org/gmane.comp.version-control.git/258355

I never heard back regarding my suggested changes. The feature was so close to ready and I thought it would be a shame for the feature to silently die, so I'm submitting a re-roll with my suggested changes on behalf of the original author.

-Richard
 contrib/completion/git-prompt.sh |  16 ++++++
 t/t9903-bash-prompt.sh           | 106 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+)
Show changes to 2 files +122 −0

contrib/completion/git-prompt.sh, t/t9903-bash-prompt.sh

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 729f769..cb78c79 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=
@@ -369,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=""
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 9150984..37953c8 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,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
-- 
2.2.1
Junio C Hamano· Jan 6, 2015, 23:31 UTC · re: Richard Hansen · lore

Re: [PATCH v4] git-prompt.sh: Option to hide prompt for ignored pwd

Richard Hansen <rhansen@bbn.com> writes:
Show 12 quoted lines
> This is the patch from:
>
>   http://article.gmane.org/gmane.comp.version-control.git/258313
>
> modified to include the changes I suggested in:
>
>   http://article.gmane.org/gmane.comp.version-control.git/258355
>
> I never heard back regarding my suggested changes.  The feature was so
> close to ready and I thought it would be a shame for the feature to
> silently die, so I'm submitting a re-roll with my suggested changes on
> behalf of the original author.
> +# 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".

Perhaps nobody had much interest in the original or the update. Occassionally resending with improvements like this is a good way to show it to more people who may have missed it the last time to solicit comments and supports.

I am personally not very interested, as you and the original made it sound as if this is primarily for those who keep track of $HOME/.dot files in $HOME/.git, which is one of the ways I would never use Git.

But I do not have to be the target of each and every new feature ;-).
Show 8 quoted lines
>  # check whether printf supports -v
>  __git_printf_supports_v=
> @@ -369,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}" ] &&

Many existing checks on variables are written this way with the "subsitutute with default value" syntax

        if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&

to make sure that people with non-standard settings to report references to unset variables as errors will not have to suffer. Don't you need to do something similar here?

Show 7 quoted lines
> +	   [ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] &&
> +	   git check-ignore -q .
> +	then
> +		if [ $pcmode = yes ]; then
> +			PS1="$ps1pc_start$ps1pc_end"
> +		fi
> +		return

There are already two places where "under pcmode, we need to set PS1 to this empty thing" is known, and this patch adds yet another. Would it be sensible to refactor that into a helper function, or open coding them this way is necessary for performance or some other reasons?

Show 15 quoted lines
> +	fi
> +
>  	local r=""
>  	local b=""
>  	local step=""
> diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
> index 9150984..37953c8 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 &&
Drop the SP after (but not before) redirection operator >>.
Richard Hansen· Jan 7, 2015, 01:22 UTC · re: Junio C Hamano · lore

[PATCH v5 0/2] git-prompt.sh: Option to hide prompt for ignored pwd

On 2015-01-06T15:31-08:00, Junio C Hamano wrote:
Show 26 quoted lines
>> This is the patch from:
>>
>>   http://article.gmane.org/gmane.comp.version-control.git/258313
>>
>> modified to include the changes I suggested in:
>>
>>   http://article.gmane.org/gmane.comp.version-control.git/258355
>>
>> I never heard back regarding my suggested changes.  The feature was so
>> close to ready and I thought it would be a shame for the feature to
>> silently die, so I'm submitting a re-roll with my suggested changes on
>> behalf of the original author.
> 
>> +# 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".
> 
> Perhaps nobody had much interest in the original or the update.
> Occassionally resending with improvements like this is a good way to
> show it to more people who may have missed it the last time to
> solicit comments and supports.
> 
> I am personally not very interested, as you and the original made it
> sound as if this is primarily for those who keep track of $HOME/.dot
> files in $HOME/.git, which is one of the ways I would never use Git.

I do keep my dotfiles in a Git repository (~/.git exists), yet I wouldn't use this feature either. (I just use refs/heads/dotfiles as HEAD so that my prompt is unique when I'm not in some project working directory.)

However, it doesn't seem like a very invasive change to me, and at least one person wants this feature (evidenced by Jess Austin going to the trouble of submitting a patch), so I thought I'd help it along.

If someone has a reasonable objection to this feature, or even if there's not enough positive interest, I wouldn't be too sad to see it not get adopted.

Show 10 quoted lines
>> +	   [ -n "${GIT_PS1_HIDE_IF_PWD_IGNORED}" ] &&
> 
> Many existing checks on variables are written this way with the
> "subsitutute with default value" syntax
> 
>         if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
> 
> to make sure that people with non-standard settings to report
> references to unset variables as errors will not have to suffer.
> Don't you need to do something similar here?
Yes; fixed.
Show 13 quoted lines
>> +	   [ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] &&
>> +	   git check-ignore -q .
>> +	then
>> +		if [ $pcmode = yes ]; then
>> +			PS1="$ps1pc_start$ps1pc_end"
>> +		fi
>> +		return
> 
> There are already two places where "under pcmode, we need to set PS1
> to this empty thing" is known, and this patch adds yet another.
> Would it be sensible to refactor that into a helper function, or
> open coding them this way is necessary for performance or some other
> reasons?

I thought about factoring it out, but didn't because defining a function with just three lines seemed awkward. But I thought of a better way to eliminate the duplicate code without defining a helper function; see the new prequel patch.

>> +	echo "ignored_dir/" >> .gitignore &&
> 
> Drop the SP after (but not before) redirection operator >>.
Done.

Thanks for the review, Richard

Jess Austin (1):
  git-prompt.sh: Option to hide prompt for ignored pwd
Richard Hansen (1):
  git-prompt.sh: if pc mode, immediately set PS1 to a plain prompt
 contrib/completion/git-prompt.sh |  24 ++++++---
 t/t9903-bash-prompt.sh           | 106 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 7 deletions(-)
-- 
2.2.1
Richard Hansen· Jan 7, 2015, 01:22 UTC · re: Richard Hansen · lore

[PATCH v5 1/2] git-prompt.sh: if pc mode, immediately set PS1 to a plain prompt

At the beginning of __git_ps1, right after determining that the function is running in pc mode, set PS1 to a plain (undecorated) prompt. This makes it possible to simply return early without having to set PS1 if the prompt should not be decorated.

Signed-off-by: Richard Hansen <rhansen@bbn.com>
---
 contrib/completion/git-prompt.sh | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
Show changes to contrib/completion/git-prompt.sh +4 −7
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 729f769..b0de082 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -299,6 +299,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}"
 		;;
@@ -350,10 +354,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
 
@@ -412,9 +412,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?
-- 
2.2.1
SZEDER Gábor· Jan 14, 2015, 11:45 UTC · re: Richard Hansen · lore

Re: [PATCH v5 1/2] git-prompt.sh: if pc mode, immediately set PS1

Hi,
Quoting Richard Hansen <rhansen@bbn.com>:
Show 49 quoted lines
> At the beginning of __git_ps1, right after determining that the
> function is running in pc mode, set PS1 to a plain (undecorated)
> prompt.  This makes it possible to simply return early without having
> to set PS1 if the prompt should not be decorated.
>
> Signed-off-by: Richard Hansen <rhansen@bbn.com>
> ---
>   contrib/completion/git-prompt.sh | 11 ++++-------
>   1 file changed, 4 insertions(+), 7 deletions(-)
>
> diff --git a/contrib/completion/git-prompt.sh
> b/contrib/completion/git-prompt.sh
> index 729f769..b0de082 100644
> --- a/contrib/completion/git-prompt.sh
> +++ b/contrib/completion/git-prompt.sh
> @@ -299,6 +299,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}"
>   		;;
> @@ -350,10 +354,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
>
> @@ -412,9 +412,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?
> --
> 2.2.1
As the one responsible for the last hunk I really like this change.

Thanks, Gábor

Richard Hansen· Jan 7, 2015, 01:22 UTC · re: Richard Hansen · lore

[PATCH v5 2/2] git-prompt.sh: Option to hide prompt for ignored pwd

From: Jess Austin <jess.austin@gmail.com>

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 <jess.austin@gmail.com>
Signed-off-by: Richard Hansen <rhansen@bbn.com>
Reviewed-by: Richard Hansen <rhansen@bbn.com>
---
 contrib/completion/git-prompt.sh |  13 +++++
 t/t9903-bash-prompt.sh           | 106 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+)
Show changes to 2 files +119 −0

contrib/completion/git-prompt.sh, t/t9903-bash-prompt.sh

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index b0de082..75c3f0f 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=
@@ -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=""
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 9150984..51ecd3e 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,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
-- 
2.2.1

← back to recent threads