git.zsh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 -a FLAGS
  14. FLAGS=('--porcelain')
  15. if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
  16. if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]; then
  17. FLAGS+='--untracked-files=no'
  18. fi
  19. case "$GIT_STATUS_IGNORE_SUBMODULES" in
  20. git)
  21. # let git decide (this respects per-repo config in .gitmodules)
  22. ;;
  23. *)
  24. # if unset: ignore dirty submodules
  25. # other values are passed to --ignore-submodules
  26. FLAGS+="--ignore-submodules=${GIT_STATUS_IGNORE_SUBMODULES:-dirty}"
  27. ;;
  28. esac
  29. STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
  30. fi
  31. if [[ -n $STATUS ]]; then
  32. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  33. else
  34. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  35. fi
  36. }
  37. # Gets the difference between the local and remote branches
  38. function git_remote_status() {
  39. local remote ahead behind git_remote_status git_remote_status_detailed
  40. remote=${$(command git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
  41. if [[ -n ${remote} ]]; then
  42. ahead=$(command git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
  43. behind=$(command git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
  44. if [[ $ahead -eq 0 ]] && [[ $behind -eq 0 ]]; then
  45. git_remote_status="$ZSH_THEME_GIT_PROMPT_EQUAL_REMOTE"
  46. elif [[ $ahead -gt 0 ]] && [[ $behind -eq 0 ]]; then
  47. git_remote_status="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
  48. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}"
  49. elif [[ $behind -gt 0 ]] && [[ $ahead -eq 0 ]]; then
  50. git_remote_status="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
  51. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  52. elif [[ $ahead -gt 0 ]] && [[ $behind -gt 0 ]]; then
  53. git_remote_status="$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
  54. 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%}"
  55. fi
  56. if [[ -n $ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]; then
  57. git_remote_status="$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX$remote$git_remote_status_detailed$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX"
  58. fi
  59. echo $git_remote_status
  60. fi
  61. }
  62. # Outputs the name of the current branch
  63. # Usage example: git pull origin $(git_current_branch)
  64. # Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
  65. # it's not a symbolic ref, but in a Git repo.
  66. function git_current_branch() {
  67. local ref
  68. ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
  69. local ret=$?
  70. if [[ $ret != 0 ]]; then
  71. [[ $ret == 128 ]] && return # no git repo.
  72. ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
  73. fi
  74. echo ${ref#refs/heads/}
  75. }
  76. # Gets the number of commits ahead from remote
  77. function git_commits_ahead() {
  78. if command git rev-parse --git-dir &>/dev/null; then
  79. local commits="$(git rev-list --count @{upstream}..HEAD 2>/dev/null)"
  80. if [[ -n "$commits" && "$commits" != 0 ]]; then
  81. echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$commits$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
  82. fi
  83. fi
  84. }
  85. # Gets the number of commits behind remote
  86. function git_commits_behind() {
  87. if command git rev-parse --git-dir &>/dev/null; then
  88. local commits="$(git rev-list --count HEAD..@{upstream} 2>/dev/null)"
  89. if [[ -n "$commits" && "$commits" != 0 ]]; then
  90. echo "$ZSH_THEME_GIT_COMMITS_BEHIND_PREFIX$commits$ZSH_THEME_GIT_COMMITS_BEHIND_SUFFIX"
  91. fi
  92. fi
  93. }
  94. # Outputs if current branch is ahead of remote
  95. function git_prompt_ahead() {
  96. if [[ -n "$(command git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
  97. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  98. fi
  99. }
  100. # Outputs if current branch is behind remote
  101. function git_prompt_behind() {
  102. if [[ -n "$(command git rev-list HEAD..origin/$(git_current_branch) 2> /dev/null)" ]]; then
  103. echo "$ZSH_THEME_GIT_PROMPT_BEHIND"
  104. fi
  105. }
  106. # Outputs if current branch exists on remote or not
  107. function git_prompt_remote() {
  108. if [[ -n "$(command git show-ref origin/$(git_current_branch) 2> /dev/null)" ]]; then
  109. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS"
  110. else
  111. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_MISSING"
  112. fi
  113. }
  114. # Formats prompt string for current git commit short SHA
  115. function git_prompt_short_sha() {
  116. local SHA
  117. SHA=$(command git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  118. }
  119. # Formats prompt string for current git commit long SHA
  120. function git_prompt_long_sha() {
  121. local SHA
  122. SHA=$(command git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  123. }
  124. # Get the status of the working tree
  125. function git_prompt_status() {
  126. local INDEX STATUS
  127. INDEX=$(command git status --porcelain -b 2> /dev/null)
  128. STATUS=""
  129. if $(echo "$INDEX" | command grep -E '^\?\? ' &> /dev/null); then
  130. STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
  131. fi
  132. if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
  133. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  134. elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
  135. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  136. elif $(echo "$INDEX" | grep '^MM ' &> /dev/null); then
  137. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  138. fi
  139. if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
  140. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  141. elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
  142. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  143. elif $(echo "$INDEX" | grep '^MM ' &> /dev/null); then
  144. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  145. elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
  146. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  147. fi
  148. if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
  149. STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
  150. fi
  151. if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
  152. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  153. elif $(echo "$INDEX" | grep '^D ' &> /dev/null); then
  154. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  155. elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
  156. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  157. fi
  158. if $(command git rev-parse --verify refs/stash >/dev/null 2>&1); then
  159. STATUS="$ZSH_THEME_GIT_PROMPT_STASHED$STATUS"
  160. fi
  161. if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
  162. STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  163. fi
  164. if $(echo "$INDEX" | grep '^## [^ ]\+ .*ahead' &> /dev/null); then
  165. STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS"
  166. fi
  167. if $(echo "$INDEX" | grep '^## [^ ]\+ .*behind' &> /dev/null); then
  168. STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS"
  169. fi
  170. if $(echo "$INDEX" | grep '^## [^ ]\+ .*diverged' &> /dev/null); then
  171. STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS"
  172. fi
  173. echo $STATUS
  174. }
  175. # Outputs the name of the current user
  176. # Usage example: $(git_current_user_name)
  177. function git_current_user_name() {
  178. command git config user.name 2>/dev/null
  179. }
  180. # Outputs the email of the current user
  181. # Usage example: $(git_current_user_email)
  182. function git_current_user_email() {
  183. command git config user.email 2>/dev/null
  184. }