865f6572d586e7ba1231333cadf808002688603e

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

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.

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/git.zsh b/lib/git.zsh
  2index 578f319e45d73d4cf503de0481de208753dbebd5..f9b27949ec123bb10d22a10429f6750af9560250 100644
  3--- a/lib/git.zsh
  4+++ b/lib/git.zsh
  5@@ -150,15 +150,11 @@ function git_prompt_long_sha() {
  6 function git_prompt_status() {
  7   [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return
  8 
  9-  local status_prompt=""
 10-
 11-  # A lookup table of each git status encountered
 12-  local -A statuses_seen
 13-
 14   # Maps a git status prefix to an internal constant
 15   # This cannot use the prompt constants, as they may be empty
 16-  local -A prefix_constant_map=(
 17-    '?? '       'UNTRACKED'
 18+  local -A prefix_constant_map
 19+  prefix_constant_map=(
 20+    '\?\? '     'UNTRACKED'
 21     'A  '       'ADDED'
 22     'M  '       'ADDED'
 23     'MM '       'ADDED'
 24@@ -176,7 +172,8 @@ function git_prompt_status() {
 25   )
 26 
 27   # Maps the internal constant to the prompt theme
 28-  local -A constant_prompt_map=(
 29+  local -A constant_prompt_map
 30+  constant_prompt_map=(
 31     'UNTRACKED' "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
 32     'ADDED'     "$ZSH_THEME_GIT_PROMPT_ADDED"
 33     'MODIFIED'  "$ZSH_THEME_GIT_PROMPT_MODIFIED"
 34@@ -190,25 +187,33 @@ function git_prompt_status() {
 35   )
 36 
 37   # The order that the prompt displays should be added to the prompt
 38-  local status_constants=(UNTRACKED ADDED MODIFIED RENAMED DELETED STASHED
 39-                          UNMERGED AHEAD BEHIND DIVERGED)
 40+  local status_constants
 41+  status_constants=(
 42+    UNTRACKED ADDED MODIFIED RENAMED DELETED
 43+    STASHED UNMERGED AHEAD BEHIND DIVERGED
 44+  )
 45 
 46-  local status_text=$(__git_prompt_git status --porcelain -b 2> /dev/null)
 47+  local status_text="$(__git_prompt_git status --porcelain -b 2> /dev/null)"
 48 
 49   # Don't continue on a catastrophic failure
 50   if [[ $? -eq 128 ]]; then
 51     return 1
 52   fi
 53 
 54-  if $(__git_prompt_git rev-parse --verify refs/stash >/dev/null 2>&1); then
 55-    statuses_seen['STASHED']=1
 56+  # A lookup table of each git status encountered
 57+  local -A statuses_seen
 58+
 59+  if __git_prompt_git rev-parse --verify refs/stash &>/dev/null; then
 60+    statuses_seen[STASHED]=1
 61   fi
 62 
 63-  local status_lines=("${(@f)${status_text}}");
 64+  local status_lines
 65+  status_lines=("${(@f)${status_text}}")
 66 
 67   # If the tracking line exists, get and parse it
 68-  if [[ $status_lines[1] =~ "^## [^ ]+ \[(.*)\]" ]]; then
 69-    local branch_statuses=("${(@s/,/)match}")
 70+  if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
 71+    local branch_statuses
 72+    branch_statuses=("${(@s/,/)match}")
 73     for branch_status in $branch_statuses; do
 74       if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
 75         continue
 76@@ -216,29 +221,22 @@ function git_prompt_status() {
 77       local last_parsed_status=$prefix_constant_map[$match[1]]
 78       statuses_seen[$last_parsed_status]=$match[2]
 79     done
 80-    shift status_lines
 81   fi
 82 
 83-  # This not only gives us a status lookup, but the count of each type
 84-  for status_line in ${status_lines}; do
 85-    local status_prefix=${status_line[1, 3]}
 86-    local status_constant=${(v)prefix_constant_map[$status_prefix]}
 87+  # For each status prefix, do a regex comparison
 88+  for status_prefix in ${(k)prefix_constant_map}; do
 89+    local status_constant="${prefix_constant_map[$status_prefix]}"
 90+    local status_regex="(^|\n)$status_prefix"
 91 
 92-    if [[ -z $status_constant ]]; then
 93-      continue
 94+    if [[ "$status_text" =~ $status_regex ]]; then
 95+      statuses_seen[$status_constant]=1
 96     fi
 97-
 98-    (( statuses_seen[$status_constant]++ ))
 99   done
100 
101-  # At this point, the statuses_seen hash contains:
102-  # - Tracking      => The difference between tracked and current
103-  # - Modifications => The count of that type of modification
104-  # - Stash         => Whether or not a stash exists