You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.3 KiB

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