Skip to content

Commit

Permalink
Use --show-stash for Git versions newer than 2.35
Browse files Browse the repository at this point in the history
  • Loading branch information
woefe committed Dec 22, 2023
1 parent 85846cd commit 82a7e03
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
28 changes: 21 additions & 7 deletions git-prompt.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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%}" \
'
Expand Down Expand Up @@ -164,7 +178,7 @@ function _zsh_git_prompt_git_status() {
}
}
$2 == "stash.count" {
$2 == "stash" {
stashed = $3;
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 82a7e03

Please sign in to comment.