perf(git): fork git less often for the prompt (#14069)

This commit is contained in:
Robby Russell
2026-09-14 21:43:51 +02:00
committed by GitHub
parent c648271b6b
commit fcf965912c
+21 -10
View File
@@ -12,10 +12,16 @@ function __git_prompt_git() {
} }
function _omz_git_prompt_info() { function _omz_git_prompt_info() {
# If we are on a folder not tracked by git, get out. # Get the git dir and the current branch name in one call. Outside a repo
# Otherwise, check for hide-info at global and local repository level # there is no output. On a detached HEAD the branch is "HEAD". On an unborn
if ! __git_prompt_git rev-parse --git-dir &> /dev/null \ # branch git exits non-zero after printing the git dir.
|| [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then local -a info
info=("${(@f)$(__git_prompt_git rev-parse --git-dir --abbrev-ref HEAD 2> /dev/null)}")
local unborn=$(( $? != 0 ))
[[ -n "$info[1]" ]] || return 0
# Check for hide-info at global and local repository level
if [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then
return 0 return 0
fi fi
@@ -23,11 +29,15 @@ function _omz_git_prompt_info() {
# - the current branch name # - the current branch name
# - the tag name if we are on a tag # - the tag name if we are on a tag
# - the short SHA of the current commit # - the short SHA of the current commit
local ref # a git dir path may contain newlines, so the branch is the last line
ref=$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null) \ local ref="$info[-1]"
|| ref=$(__git_prompt_git describe --tags --exact-match HEAD 2> /dev/null) \ if (( unborn )); then
|| ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) \ ref=$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null) || return 0
|| return 0 elif [[ "$ref" == HEAD ]]; then
ref=$(__git_prompt_git describe --tags --exact-match HEAD 2> /dev/null) \
|| ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) \
|| return 0
fi
# Use global ZSH_THEME_GIT_SHOW_UPSTREAM=1 for including upstream remote info # Use global ZSH_THEME_GIT_SHOW_UPSTREAM=1 for including upstream remote info
local upstream local upstream
@@ -244,7 +254,8 @@ function parse_git_dirty() {
FLAGS+="--ignore-submodules=${GIT_STATUS_IGNORE_SUBMODULES:-dirty}" FLAGS+="--ignore-submodules=${GIT_STATUS_IGNORE_SUBMODULES:-dirty}"
;; ;;
esac esac
STATUS=$(__git_prompt_git status ${FLAGS} 2> /dev/null | tail -n 1) # only non-emptiness matters, so keep one line instead of the whole list
__git_prompt_git status ${FLAGS} 2> /dev/null | read -r STATUS
fi fi
if [[ -n $STATUS ]]; then if [[ -n $STATUS ]]; then
echo "$ZSH_THEME_GIT_PROMPT_DIRTY" echo "$ZSH_THEME_GIT_PROMPT_DIRTY"