git.zsh 6.8 KB

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