mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-11-21 21:30:09 +00:00
Fix performance drop of iterating over lines and other stuff
- Fix 'STASHED' key and unsupported syntax in 5.0.2 - Optimise `if` statement to make it more idiomatic.
This commit is contained in:
parent
45a954cb0f
commit
865f6572d5
1 changed files with 30 additions and 32 deletions
62
lib/git.zsh
62
lib/git.zsh
|
@ -150,15 +150,11 @@ function git_prompt_long_sha() {
|
||||||
function git_prompt_status() {
|
function git_prompt_status() {
|
||||||
[[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return
|
[[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return
|
||||||
|
|
||||||
local status_prompt=""
|
|
||||||
|
|
||||||
# A lookup table of each git status encountered
|
|
||||||
local -A statuses_seen
|
|
||||||
|
|
||||||
# Maps a git status prefix to an internal constant
|
# Maps a git status prefix to an internal constant
|
||||||
# This cannot use the prompt constants, as they may be empty
|
# This cannot use the prompt constants, as they may be empty
|
||||||
local -A prefix_constant_map=(
|
local -A prefix_constant_map
|
||||||
'?? ' 'UNTRACKED'
|
prefix_constant_map=(
|
||||||
|
'\?\? ' 'UNTRACKED'
|
||||||
'A ' 'ADDED'
|
'A ' 'ADDED'
|
||||||
'M ' 'ADDED'
|
'M ' 'ADDED'
|
||||||
'MM ' 'ADDED'
|
'MM ' 'ADDED'
|
||||||
|
@ -176,7 +172,8 @@ function git_prompt_status() {
|
||||||
)
|
)
|
||||||
|
|
||||||
# Maps the internal constant to the prompt theme
|
# Maps the internal constant to the prompt theme
|
||||||
local -A constant_prompt_map=(
|
local -A constant_prompt_map
|
||||||
|
constant_prompt_map=(
|
||||||
'UNTRACKED' "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
|
'UNTRACKED' "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
|
||||||
'ADDED' "$ZSH_THEME_GIT_PROMPT_ADDED"
|
'ADDED' "$ZSH_THEME_GIT_PROMPT_ADDED"
|
||||||
'MODIFIED' "$ZSH_THEME_GIT_PROMPT_MODIFIED"
|
'MODIFIED' "$ZSH_THEME_GIT_PROMPT_MODIFIED"
|
||||||
|
@ -190,25 +187,33 @@ function git_prompt_status() {
|
||||||
)
|
)
|
||||||
|
|
||||||
# The order that the prompt displays should be added to the prompt
|
# The order that the prompt displays should be added to the prompt
|
||||||
local status_constants=(UNTRACKED ADDED MODIFIED RENAMED DELETED STASHED
|
local status_constants
|
||||||
UNMERGED AHEAD BEHIND DIVERGED)
|
status_constants=(
|
||||||
|
UNTRACKED ADDED MODIFIED RENAMED DELETED
|
||||||
|
STASHED UNMERGED AHEAD BEHIND DIVERGED
|
||||||
|
)
|
||||||
|
|
||||||
local status_text=$(__git_prompt_git status --porcelain -b 2> /dev/null)
|
local status_text="$(__git_prompt_git status --porcelain -b 2> /dev/null)"
|
||||||
|
|
||||||
# Don't continue on a catastrophic failure
|
# Don't continue on a catastrophic failure
|
||||||
if [[ $? -eq 128 ]]; then
|
if [[ $? -eq 128 ]]; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if $(__git_prompt_git rev-parse --verify refs/stash >/dev/null 2>&1); then
|
# A lookup table of each git status encountered
|
||||||
statuses_seen['STASHED']=1
|
local -A statuses_seen
|
||||||
|
|
||||||
|
if __git_prompt_git rev-parse --verify refs/stash &>/dev/null; then
|
||||||
|
statuses_seen[STASHED]=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local status_lines=("${(@f)${status_text}}");
|
local status_lines
|
||||||
|
status_lines=("${(@f)${status_text}}")
|
||||||
|
|
||||||
# If the tracking line exists, get and parse it
|
# If the tracking line exists, get and parse it
|
||||||
if [[ $status_lines[1] =~ "^## [^ ]+ \[(.*)\]" ]]; then
|
if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
|
||||||
local branch_statuses=("${(@s/,/)match}")
|
local branch_statuses
|
||||||
|
branch_statuses=("${(@s/,/)match}")
|
||||||
for branch_status in $branch_statuses; do
|
for branch_status in $branch_statuses; do
|
||||||
if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
|
if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
|
||||||
continue
|
continue
|
||||||
|
@ -216,29 +221,22 @@ function git_prompt_status() {
|
||||||
local last_parsed_status=$prefix_constant_map[$match[1]]
|
local last_parsed_status=$prefix_constant_map[$match[1]]
|
||||||
statuses_seen[$last_parsed_status]=$match[2]
|
statuses_seen[$last_parsed_status]=$match[2]
|
||||||
done
|
done
|
||||||
shift status_lines
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# This not only gives us a status lookup, but the count of each type
|
# For each status prefix, do a regex comparison
|
||||||
for status_line in ${status_lines}; do
|
for status_prefix in ${(k)prefix_constant_map}; do
|
||||||
local status_prefix=${status_line[1, 3]}
|
local status_constant="${prefix_constant_map[$status_prefix]}"
|
||||||
local status_constant=${(v)prefix_constant_map[$status_prefix]}
|
local status_regex="(^|\n)$status_prefix"
|
||||||
|
|
||||||
if [[ -z $status_constant ]]; then
|
if [[ "$status_text" =~ $status_regex ]]; then
|
||||||
continue
|
statuses_seen[$status_constant]=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
(( statuses_seen[$status_constant]++ ))
|
|
||||||
done
|
done
|
||||||
|
|
||||||
# At this point, the statuses_seen hash contains:
|
# Display the seen statuses in the order specified
|
||||||
# - Tracking => The difference between tracked and current
|
local status_prompt
|
||||||
# - Modifications => The count of that type of modification
|
|
||||||
# - Stash => Whether or not a stash exists
|
|
||||||
# Might be useful for someone?
|
|
||||||
|
|
||||||
for status_constant in $status_constants; do
|
for status_constant in $status_constants; do
|
||||||
if [[ ${+statuses_seen[$status_constant]} -eq 1 ]]; then
|
if (( ${+statuses_seen[$status_constant]} )); then
|
||||||
local next_display=$constant_prompt_map[$status_constant]
|
local next_display=$constant_prompt_map[$status_constant]
|
||||||
status_prompt="$next_display$status_prompt"
|
status_prompt="$next_display$status_prompt"
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue