1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-12-18 10:22:00 +00:00

perf(bureau): remove multiple grep calls in git status check

This commit is contained in:
Marc Cornellà 2022-02-07 17:44:05 +01:00
parent 9e9831fcf2
commit 74a3db75e4
No known key found for this signature in database
GPG key ID: 0314585E776A9C1B

View file

@ -16,7 +16,7 @@ ZSH_THEME_GIT_PROMPT_STAGED="%{$fg_bold[green]%}●%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_UNSTAGED="%{$fg_bold[yellow]%}●%{$reset_color%}" ZSH_THEME_GIT_PROMPT_UNSTAGED="%{$fg_bold[yellow]%}●%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}●%{$reset_color%}" ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}●%{$reset_color%}"
bureau_git_branch () { bureau_git_info () {
local ref local ref
ref=$(command git symbolic-ref HEAD 2> /dev/null) || \ ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
@ -30,16 +30,16 @@ bureau_git_status() {
# check status of files # check status of files
local gitfiles="$(tail -n +2 <<< "$gitstatus")" local gitfiles="$(tail -n +2 <<< "$gitstatus")"
if [[ -n "$gitfiles" ]]; then if [[ -n "$gitfiles" ]]; then
if $(echo "$gitfiles" | command grep -q '^[AMRD]. '); then if [[ "$gitfiles" =~ $'(^|\n)[AMRD]. ' ]]; then
result+="$ZSH_THEME_GIT_PROMPT_STAGED" result+="$ZSH_THEME_GIT_PROMPT_STAGED"
fi fi
if $(echo "$gitfiles" | command grep -q '^.[MTD] '); then if [[ "$gitfiles" =~ $'(^|\n).[MTD] ' ]]; then
result+="$ZSH_THEME_GIT_PROMPT_UNSTAGED" result+="$ZSH_THEME_GIT_PROMPT_UNSTAGED"
fi fi
if $(echo "$gitfiles" | command grep -q -E '^\?\? '); then if [[ "$gitfiles" =~ $'(^|\n)\\?\\? ' ]]; then
result+="$ZSH_THEME_GIT_PROMPT_UNTRACKED" result+="$ZSH_THEME_GIT_PROMPT_UNTRACKED"
fi fi
if $(echo "$gitfiles" | command grep -q '^UU '); then if [[ "$gitfiles" =~ $'(^|\n)UU ' ]]; then
result+="$ZSH_THEME_GIT_PROMPT_UNMERGED" result+="$ZSH_THEME_GIT_PROMPT_UNMERGED"
fi fi
else else
@ -48,17 +48,18 @@ bureau_git_status() {
# check status of local repository # check status of local repository
local gitbranch="$(head -n 1 <<< "$gitstatus")" local gitbranch="$(head -n 1 <<< "$gitstatus")"
if $(echo "$gitbranch" | command grep -q '^## .*ahead'); then if [[ "$gitbranch" =~ '^## .*ahead' ]]; then
result+="$ZSH_THEME_GIT_PROMPT_AHEAD" result+="$ZSH_THEME_GIT_PROMPT_AHEAD"
fi fi
if $(echo "$gitbranch" | command grep -q '^## .*behind'); then if [[ "$gitbranch" =~ '^## .*behind' ]]; then
result+="$ZSH_THEME_GIT_PROMPT_BEHIND" result+="$ZSH_THEME_GIT_PROMPT_BEHIND"
fi fi
if $(echo "$gitbranch" | command grep -q '^## .*diverged'); then if [[ "$gitbranch" =~ '^## .*diverged' ]]; then
result+="$ZSH_THEME_GIT_PROMPT_DIVERGED" result+="$ZSH_THEME_GIT_PROMPT_DIVERGED"
fi fi
if $(command git rev-parse --verify refs/stash &> /dev/null); then # check if there are stashed changes
if command git rev-parse --verify refs/stash &> /dev/null; then
result+="$ZSH_THEME_GIT_PROMPT_STASHED" result+="$ZSH_THEME_GIT_PROMPT_STASHED"
fi fi
@ -66,21 +67,22 @@ bureau_git_status() {
} }
bureau_git_prompt() { bureau_git_prompt() {
local gitbranch=$(bureau_git_branch) # check git information
local gitstatus=$(bureau_git_status) local gitinfo=$(bureau_git_info)
local info if [[ -z "$gitinfo" ]]; then
if [[ -z "$gitbranch" ]]; then
return return
fi fi
info="${gitbranch:gs/%/%%}" # quote % in git information
local output="${gitinfo:gs/%/%%}"
# check git status
local gitstatus=$(bureau_git_status)
if [[ -n "$gitstatus" ]]; then if [[ -n "$gitstatus" ]]; then
info+=" $gitstatus" output+=" $gitstatus"
fi fi
echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${info}${ZSH_THEME_GIT_PROMPT_SUFFIX}" echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${output}${ZSH_THEME_GIT_PROMPT_SUFFIX}"
} }