git.zsh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # Outputs current branch info in prompt format
  2. function git_prompt_info() {
  3. local ref
  4. if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
  5. ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
  6. ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
  7. echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
  8. fi
  9. }
  10. # Checks if working tree is dirty
  11. function parse_git_dirty() {
  12. local STATUS=''
  13. local FLAGS
  14. FLAGS=('--porcelain')
  15. if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
  16. if [[ $POST_1_7_2_GIT -gt 0 ]]; then
  17. FLAGS+='--ignore-submodules=dirty'
  18. fi
  19. if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]; then
  20. FLAGS+='--untracked-files=no'
  21. fi
  22. STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
  23. fi
  24. if [[ -n $STATUS ]]; then
  25. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  26. else
  27. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  28. fi
  29. }
  30. # Gets the difference between the local and remote branches
  31. function git_remote_status() {
  32. local remote ahead behind git_remote_status git_remote_status_detailed
  33. remote=${$(command git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
  34. if [[ -n ${remote} ]]; then
  35. ahead=$(command git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
  36. behind=$(command git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
  37. if [[ $ahead -gt 0 ]] && [[ $behind -eq 0 ]]; then
  38. git_remote_status="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
  39. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}"
  40. elif [[ $behind -gt 0 ]] && [[ $ahead -eq 0 ]]; then
  41. git_remote_status="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
  42. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  43. elif [[ $ahead -gt 0 ]] && [[ $behind -gt 0 ]]; then
  44. git_remote_status="$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
  45. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  46. fi
  47. if [[ -n $ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]; then
  48. git_remote_status="$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX$remote$git_remote_status_detailed$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX"
  49. fi
  50. echo $git_remote_status
  51. fi
  52. }
  53. # Outputs the name of the current branch
  54. # Usage example: git pull origin $(git_current_branch)
  55. # Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
  56. # it's not a symbolic ref, but in a Git repo.
  57. function git_current_branch() {
  58. local ref
  59. ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
  60. local ret=$?
  61. if [[ $ret != 0 ]]; then
  62. [[ $ret == 128 ]] && return # no git repo.
  63. ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
  64. fi
  65. echo ${ref#refs/heads/}
  66. }
  67. # Gets the number of commits ahead from remote
  68. function git_commits_ahead() {
  69. if $(echo "$(command git log @{upstream}..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
  70. local COMMITS
  71. COMMITS=$(command git log @{upstream}..HEAD | grep '^commit' | wc -l | tr -d ' ')
  72. echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$COMMITS$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
  73. fi
  74. }
  75. # Outputs if current branch is ahead of remote
  76. function git_prompt_ahead() {
  77. if [[ -n "$(command git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
  78. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  79. fi
  80. }
  81. # Outputs if current branch is behind remote
  82. function git_prompt_behind() {
  83. if [[ -n "$(command git rev-list HEAD..origin/$(git_current_branch) 2> /dev/null)" ]]; then
  84. echo "$ZSH_THEME_GIT_PROMPT_BEHIND"
  85. fi
  86. }
  87. # Outputs if current branch exists on remote or not
  88. function git_prompt_remote() {
  89. if [[ -n "$(command git show-ref origin/$(git_current_branch) 2> /dev/null)" ]]; then
  90. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS"
  91. else
  92. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_MISSING"
  93. fi
  94. }
  95. # Formats prompt string for current git commit short SHA
  96. function git_prompt_short_sha() {
  97. local SHA
  98. SHA=$(command git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  99. }
  100. # Formats prompt string for current git commit long SHA
  101. function git_prompt_long_sha() {
  102. local SHA
  103. SHA=$(command git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  104. }
  105. # Get the status of the working tree
  106. function git_prompt_status() {
  107. local INDEX STATUS
  108. INDEX=$(command git status --porcelain -b 2> /dev/null)
  109. STATUS=""
  110. if $(echo "$INDEX" | command grep -E '^\?\? ' &> /dev/null); then
  111. STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
  112. fi
  113. if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
  114. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  115. elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
  116. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  117. fi
  118. if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
  119. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  120. elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
  121. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  122. elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
  123. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  124. fi
  125. if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
  126. STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
  127. fi
  128. if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
  129. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  130. elif $(echo "$INDEX" | grep '^D ' &> /dev/null); then
  131. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  132. elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
  133. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  134. fi
  135. if $(command git rev-parse --verify refs/stash >/dev/null 2>&1); then
  136. STATUS="$ZSH_THEME_GIT_PROMPT_STASHED$STATUS"
  137. fi
  138. if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
  139. STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  140. fi
  141. if $(echo "$INDEX" | grep '^## .*ahead' &> /dev/null); then
  142. STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS"
  143. fi
  144. if $(echo "$INDEX" | grep '^## .*behind' &> /dev/null); then
  145. STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS"
  146. fi
  147. if $(echo "$INDEX" | grep '^## .*diverged' &> /dev/null); then
  148. STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS"
  149. fi
  150. echo $STATUS
  151. }
  152. # Compares the provided version of git to the version installed and on path
  153. # Outputs -1, 0, or 1 if the installed version is less than, equal to, or
  154. # greater than the input version, respectively.
  155. function git_compare_version() {
  156. local INPUT_GIT_VERSION INSTALLED_GIT_VERSION
  157. INPUT_GIT_VERSION=(${(s/./)1})
  158. INSTALLED_GIT_VERSION=($(command git --version 2>/dev/null))
  159. INSTALLED_GIT_VERSION=(${(s/./)INSTALLED_GIT_VERSION[3]})
  160. for i in {1..3}; do
  161. if [[ $INSTALLED_GIT_VERSION[$i] -gt $INPUT_GIT_VERSION[$i] ]]; then
  162. echo 1
  163. return 0
  164. fi
  165. if [[ $INSTALLED_GIT_VERSION[$i] -lt $INPUT_GIT_VERSION[$i] ]]; then
  166. echo -1
  167. return 0
  168. fi
  169. done
  170. echo 0
  171. }
  172. # This is unlikely to change so make it all statically assigned
  173. POST_1_7_2_GIT=$(git_compare_version "1.7.2")
  174. # Clean up the namespace slightly by removing the checker function
  175. unfunction git_compare_version