git.zsh 7.9 KB

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