diff --git a/README.md b/README.md index 3b61a6d..dc0ec0a 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,7 @@ Furthermore, a warning symbol can be configured through `ZSH_THEME_GIT_PROMPT_UP ### Show number of stash entries The number of stash entries will be shown if `ZSH_GIT_PROMPT_SHOW_STASH` is set. -Enabling this will execute another Git command every time a new prompt is shown! +On Git versions older than 2.35.0 this will execute another Git command every time a new prompt is shown! To enable stash entries add the following line to your `.zshrc`: ```bash @@ -203,6 +203,6 @@ time ZSH_GIT_PROMPT_AWK_CMD=awk zsh -f -c ' ## Known issues * If the current working directory is not a Git repository and some external application initializes a new repository in the same directory, the Git prompt will not be shown immediately. Also, updates made by external programs or another shell do not show up immediately. - Executing any command or simply pressing enter will fix the issue. + Executing any command or simply pressing enter to draw a new prompt will fix the issue. * In large repositories the prompt might slow down, because Git has to find untracked files. See `man git-status`, Section `--untracked-files` for possible options to speed things up. diff --git a/git-prompt.zsh b/git-prompt.zsh index aadb7a1..5c4f798 100644 --- a/git-prompt.zsh +++ b/git-prompt.zsh @@ -71,16 +71,29 @@ setopt PROMPT_SUBST (( $+commands[nawk] )) && : "${ZSH_GIT_PROMPT_AWK_CMD:=nawk}" : "${ZSH_GIT_PROMPT_AWK_CMD:=awk}" -function _zsh_git_prompt_git_status() { - emulate -L zsh - { +# Use --show-stash for git versions newer than 2.35.0 +_zsh_git_prompt_git_version=$(command git version) +if [[ "${_zsh_git_prompt_git_version:12}" == 2.<35->.<-> ]]; then + _zsh_git_prompt_git_cmd() { + GIT_OPTIONAL_LOCKS=0 command git status --show-stash --branch --porcelain=v2 2>&1 \ + || echo "fatal: git command failed" + } +else + _zsh_git_prompt_git_cmd() { [[ -n "$ZSH_GIT_PROMPT_SHOW_STASH" ]] && ( c=$(command git rev-list --walk-reflogs --count refs/stash 2> /dev/null) - [[ -n "$c" ]] && echo "# stash.count $c" + [[ -n "$c" ]] && echo "# stash $c" ) GIT_OPTIONAL_LOCKS=0 command git status --branch --porcelain=v2 2>&1 \ || echo "fatal: git command failed" - } | $ZSH_GIT_PROMPT_AWK_CMD \ + } +fi +unset _zsh_git_prompt_git_version + + +function _zsh_git_prompt_git_status() { + emulate -L zsh + _zsh_git_prompt_git_cmd | $ZSH_GIT_PROMPT_AWK_CMD \ -v PREFIX="$ZSH_THEME_GIT_PROMPT_PREFIX" \ -v SUFFIX="$ZSH_THEME_GIT_PROMPT_SUFFIX" \ -v SEPARATOR="$ZSH_THEME_GIT_PROMPT_SEPARATOR" \ @@ -98,6 +111,7 @@ function _zsh_git_prompt_git_status() { -v UNSTAGED="$ZSH_THEME_GIT_PROMPT_UNSTAGED" \ -v UNTRACKED="$ZSH_THEME_GIT_PROMPT_UNTRACKED" \ -v STASHED="$ZSH_THEME_GIT_PROMPT_STASHED" \ + -v SHOW_STASH="$ZSH_GIT_PROMPT_SHOW_STASH" \ -v CLEAN="$ZSH_THEME_GIT_PROMPT_CLEAN" \ -v RC="%{$reset_color%}" \ ' @@ -164,7 +178,7 @@ function _zsh_git_prompt_git_status() { } } - $2 == "stash.count" { + $2 == "stash" { stashed = $3; } @@ -215,7 +229,7 @@ function _zsh_git_prompt_git_status() { prompt_element(UNTRACKED, untracked); } - if (stashed > 0) { + if (stashed > 0 && SHOW_STASH != "") { prompt_element(STASHED, stashed); }