git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
RHRichard Hansen <rhansen@bbn.com>
Date
Oct 15, 2014, 20:28 UTC
Message-ID
<543ED906.403@bbn.com>
In-Reply-To
<1413345993-14393-1-git-send-email-jess.austin@gmail.com>
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:

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
Previous: Jess AustinNext: Richard Hansen
Message 12 of 18 in “git-prompt.sh: Omit prompt for ignored directories”
  1. git-prompt.sh: Omit prompt for ignored directoriesJess Austin, Oct 8, 2014
  2. Richard HansenOct 8, 2014
  3. Fwd: [PATCH] git-prompt.sh: Omit prompt for ignored directoriesJess Austin, Oct 8, 2014
  4. Richard HansenOct 9, 2014
  5. Jess AustinOct 9, 2014
  6. Richard HansenOct 9, 2014
  7. git-prompt.sh: Hide prompt for ignored pwdJess Austin, Oct 14, 2014
  8. Johannes SixtOct 14, 2014
  9. Richard HansenOct 14, 2014
  10. Richard HansenOct 14, 2014
  11. git-prompt.sh: Option to hide prompt for ignored pwdJess Austin, Oct 15, 2014
  12. Richard HansenOct 15, 2014
  13. git-prompt.sh: Option to hide prompt for ignored pwdRichard Hansen, Jan 5, 2015
  14. Junio C HamanoJan 6, 2015
  15. 0/2 git-prompt.sh: Option to hide prompt for ignored pwdRichard Hansen, Jan 7, 2015
  16. 1/2 git-prompt.sh: if pc mode, immediately set PS1 to a plain promptRichard Hansen, Jan 7, 2015
  17. SZEDER GáborJan 14, 2015
  18. 2/2 git-prompt.sh: Option to hide prompt for ignored pwdRichard Hansen, Jan 7, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.