diff mbox series

git-prompt.sh not working with vcs_info under zsh

Message ID d8044e41ed12459c405a9a3be522c0f1@condition-alpha.com (mailing list archive)
State New, archived
Headers show
Series git-prompt.sh not working with vcs_info under zsh | expand

Commit Message

Alexander Adolf June 4, 2021, 3:17 p.m. UTC
Hello,

I discovered and fixed a small glitch in git-prompt.sh in conjunction
with zsh's vcs_info library. I prefer using git-prompt.sh through
vcs_info because it allows me to leverage the support of other version
control systems provided by vcs_info, and still get the more detailed
information provided by git-prompt.sh. Best-of-both-worlds approach. ;-)

The issue is that the prompt_subst option (required for using vcs_info)
breaks the rendering of the percent character to indicate untracked
objects. More details in the attached patch.

For the curious, here is the relevant snippet from my ~/.zshrc:
---------------------------- Begin Quote -----------------------------
setopt prompt_subst
autoload -U promptinit; promptinit
autoload -Uz vcs_info
zstyle ':vcs_info:*' enable git svn cvs hg
zstyle ':vcs_info:*' check-for-changes true
source ~/.zsh/git-prompt.sh	# defines __git_ps1()
GIT_PS1_SHOWCOLORHINTS="1"	# colors are based on the colored output of "git status -sb"
GIT_PS1_SHOWDIRTYSTATE="1"	# '*' unstaged and '+' staged changes
GIT_PS1_SHOWSTASHSTATE="1"	# '$' if something is stashed
GIT_PS1_SHOWUNTRACKEDFILES="1"	# '%' if untracked files exist
GIT_PS1_SHOWUPSTREAM=('name' 'verbose' 'git')
function git-ps1-wrapper () { reply=" (%s:$(__git_ps1 '%s'))" }
zstyle -e ':vcs_info:git:*' formats       'git-ps1-wrapper'
zstyle -e ':vcs_info:git:*' actionformats 'git-ps1-wrapper'
zstyle  ':vcs_info:*'     formats       ' (%s:%b%u%c)'
zstyle  ':vcs_info:*'     actionformats ' (%s:%b%u%c|%a)'
precmd () { vcs_info }
PROMPT='
%B%m:%2~%b${vcs_info_msg_0_}
%B%(!.#.▶)%b '
----------------------------- End Quote ------------------------------


Many thanks, looking forward to your thoughts, and have a great weekend,

  --alexander
diff mbox series

Patch

From 5a00f8f58a18c87f2a7b9815d832c0beef7b9bff Mon Sep 17 00:00:00 2001
From: Alexander Adolf <c-alpha@users.noreply.github.com>
Date: Fri, 4 Jun 2021 01:41:56 +0200
Subject: [PATCH] Cannot Use __git_ps1 in normal mode for zsh vcs_info

When using the __git_ps1 shell function in normal mode (i.e. passing
it a single argument) in conjunction with the vcs_info library under
zsh, the percent character ('%'), used to indicate untracked objects,
breaks the rendering ('%f' is no longer recognised as ending the
colour code, but appears verbatim in the prompt).

This is because using vcs_info requires the PROMPT_SUBST option to be
set in zsh, but which changes the handling of percent characters. When
PROMPT_SUBST is in effect, one additional 'level' of percent signs is
consumed by the prompt substitution, and to get the desired
effect (resulting in a single, verbatim percent character in the
prompt), four percent signs need to be inserted.

This commit changes the way the needed percent character(s) to
indicate untracked objects are inserted when running under zsh,
depending on whether the PROMPT_SUBST option is in effect or not.
---
 contrib/completion/git-prompt.sh | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index db7c0068fb..f8d19139b6 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -540,7 +540,11 @@  __git_ps1 ()
 		   [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
 		   git ls-files --others --exclude-standard --directory --no-empty-directory --error-unmatch -- ':/*' >/dev/null 2>/dev/null
 		then
-			u="%${ZSH_VERSION+%}"
+			if [ $ps1_expanded = yes ]; then
+				u="%${ZSH_VERSION+%%%}"
+			else
+				u="%${ZSH_VERSION+%}"
+			fi
 		fi
 
 		if [ -n "${GIT_PS1_COMPRESSSPARSESTATE-}" ] &&
-- 
2.31.1