1c58a746af7a67f311ee47f97285a855eaf18b5e

Author
Robert Estelle <robertestelle@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

lib: safety fix and speed-ups for git.zsh prompt functions (#7804)

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/git.zsh b/lib/git.zsh
  2index f7d4738caf2126d96f0e278901c6f863cb031303..ffc7c01a164f24863a3f4feafe3a66ce21830e92 100644
  3--- a/lib/git.zsh
  4+++ b/lib/git.zsh
  5@@ -1,9 +1,20 @@
  6+# The git prompt's git commands are read-only and should not interfere with
  7+# other processes. This environment variable is equivalent to running with `git
  8+# --no-optional-locks`, but falls back gracefully for older versions of git.
  9+# See git(1) for and git-status(1) for a description of that flag.
 10+#
 11+# We wrap in a local function instead of exporting the variable directly in
 12+# order to avoid interfering with manually-run git commands by the user.
 13+function __git_prompt_git() {
 14+  GIT_OPTIONAL_LOCKS=0 command git "$@"
 15+}
 16+
 17 # Outputs current branch info in prompt format
 18 function git_prompt_info() {
 19   local ref
 20-  if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
 21-    ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
 22-    ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
 23+  if [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
 24+    ref=$(__git_prompt_git symbolic-ref HEAD 2> /dev/null) || \
 25+    ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return 0
 26     echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
 27   fi
 28 }
 29@@ -13,7 +24,7 @@ function parse_git_dirty() {
 30   local STATUS
 31   local -a FLAGS
 32   FLAGS=('--porcelain')
 33-  if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
 34+  if [[ "$(__git_prompt_git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
 35     if [[ "${DISABLE_UNTRACKED_FILES_DIRTY:-}" == "true" ]]; then
 36       FLAGS+='--untracked-files=no'
 37     fi
 38@@ -27,7 +38,7 @@ function parse_git_dirty() {
 39         FLAGS+="--ignore-submodules=${GIT_STATUS_IGNORE_SUBMODULES:-dirty}"
 40         ;;
 41     esac
 42-    STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
 43+    STATUS=$(__git_prompt_git status ${FLAGS} 2> /dev/null | tail -n1)
 44   fi
 45   if [[ -n $STATUS ]]; then
 46     echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
 47@@ -39,10 +50,10 @@ function parse_git_dirty() {
 48 # Gets the difference between the local and remote branches
 49 function git_remote_status() {
 50     local remote ahead behind git_remote_status git_remote_status_detailed
 51-    remote=${$(command git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
 52+    remote=${$(__git_prompt_git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
 53     if [[ -n ${remote} ]]; then
 54-        ahead=$(command git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
 55-        behind=$(command git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
 56+        ahead=$(__git_prompt_git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
 57+        behind=$(__git_prompt_git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
 58 
 59         if [[ $ahead -eq 0 ]] && [[ $behind -eq 0 ]]; then
 60             git_remote_status="$ZSH_THEME_GIT_PROMPT_EQUAL_REMOTE"
 61@@ -71,11 +82,11 @@ function git_remote_status() {
 62 # it's not a symbolic ref, but in a Git repo.
 63 function git_current_branch() {
 64   local ref
 65-  ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
 66+  ref=$(__git_prompt_git symbolic-ref --quiet HEAD 2> /dev/null)
 67   local ret=$?
 68   if [[ $ret != 0 ]]; then
 69     [[ $ret == 128 ]] && return  # no git repo.
 70-    ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
 71+    ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return
 72   fi
 73   echo ${ref#refs/heads/}
 74 }
 75@@ -83,8 +94,8 @@ function git_current_branch() {
 76 
 77 # Gets the number of commits ahead from remote
 78 function git_commits_ahead() {
 79-  if command git rev-parse --git-dir &>/dev/null; then
 80-    local commits="$(git rev-list --count @{upstream}..HEAD 2>/dev/null)"
 81+  if __git_prompt_git rev-parse --git-dir &>/dev/null; then
 82+    local commits="$(__git_prompt_git rev-list --count @{upstream}..HEAD 2>/dev/null)"
 83     if [[ -n "$commits" && "$commits" != 0 ]]; then
 84       echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$commits$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
 85     fi
 86@@ -93,8 +104,8 @@ function git_commits_ahead() {
 87 
 88 # Gets the number of commits behind remote
 89 function git_commits_behind() {
 90-  if command git rev-parse --git-dir &>/dev/null; then
 91-    local commits="$(git rev-list --count HEAD..@{upstream} 2>/dev/null)"
 92+  if __git_prompt_git rev-parse --git-dir &>/dev/null; then
 93+    local commits="$(__git_prompt_git rev-list --count HEAD..@{upstream} 2>/dev/null)"
 94     if [[ -n "$commits" && "$commits" != 0 ]]; then
 95       echo "$ZSH_THEME_GIT_COMMITS_BEHIND_PREFIX$commits$ZSH_THEME_GIT_COMMITS_BEHIND_SUFFIX"
 96     fi
 97@@ -103,21 +114,21 @@ function git_commits_behind() {
 98 
 99 # Outputs if current branch is ahead of remote
100 function git_prompt_ahead() {
101-  if [[ -n "$(command git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
102+  if [[ -n "$(__git_prompt_git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
103     echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
104   fi