git.zsh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. function git_prompt_info() {
  12. # If we are on a folder not tracked by git, get out.
  13. # Otherwise, check for hide-info at global and local repository level
  14. if ! __git_prompt_git rev-parse --git-dir &> /dev/null \
  15. || [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then
  16. return 0
  17. fi
  18. local ref
  19. ref=$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null) \
  20. || ref=$(__git_prompt_git describe --tags --exact-match HEAD 2> /dev/null) \
  21. || ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) \
  22. || return 0
  23. # Use global ZSH_THEME_GIT_SHOW_UPSTREAM=1 for including upstream remote info
  24. local upstream
  25. if (( ${+ZSH_THEME_GIT_SHOW_UPSTREAM} )); then
  26. upstream=$(__git_prompt_git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}" 2>/dev/null) \
  27. && upstream=" -> ${upstream}"
  28. fi
  29. echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${ref:gs/%/%%}${upstream:gs/%/%%}$(parse_git_dirty)${ZSH_THEME_GIT_PROMPT_SUFFIX}"
  30. }
  31. # Checks if working tree is dirty
  32. function parse_git_dirty() {
  33. local STATUS
  34. local -a FLAGS
  35. FLAGS=('--porcelain')
  36. if [[ "$(__git_prompt_git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
  37. if [[ "${DISABLE_UNTRACKED_FILES_DIRTY:-}" == "true" ]]; then
  38. FLAGS+='--untracked-files=no'
  39. fi
  40. case "${GIT_STATUS_IGNORE_SUBMODULES:-}" in
  41. git)
  42. # let git decide (this respects per-repo config in .gitmodules)
  43. ;;
  44. *)
  45. # if unset: ignore dirty submodules
  46. # other values are passed to --ignore-submodules
  47. FLAGS+="--ignore-submodules=${GIT_STATUS_IGNORE_SUBMODULES:-dirty}"
  48. ;;
  49. esac
  50. STATUS=$(__git_prompt_git status ${FLAGS} 2> /dev/null | tail -n 1)
  51. fi
  52. if [[ -n $STATUS ]]; then
  53. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  54. else
  55. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  56. fi
  57. }
  58. # Gets the difference between the local and remote branches
  59. function git_remote_status() {
  60. local remote ahead behind git_remote_status git_remote_status_detailed
  61. remote=${$(__git_prompt_git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
  62. if [[ -n ${remote} ]]; then
  63. ahead=$(__git_prompt_git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
  64. behind=$(__git_prompt_git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
  65. if [[ $ahead -eq 0 ]] && [[ $behind -eq 0 ]]; then
  66. git_remote_status="$ZSH_THEME_GIT_PROMPT_EQUAL_REMOTE"
  67. elif [[ $ahead -gt 0 ]] && [[ $behind -eq 0 ]]; then
  68. git_remote_status="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
  69. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}"
  70. elif [[ $behind -gt 0 ]] && [[ $ahead -eq 0 ]]; then
  71. git_remote_status="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
  72. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  73. elif [[ $ahead -gt 0 ]] && [[ $behind -gt 0 ]]; then
  74. git_remote_status="$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
  75. 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%}"
  76. fi
  77. if [[ -n $ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]; then
  78. git_remote_status="$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX${remote:gs/%/%%}$git_remote_status_detailed$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX"
  79. fi
  80. echo $git_remote_status
  81. fi
  82. }
  83. # Outputs the name of the current branch
  84. # Usage example: git pull origin $(git_current_branch)
  85. # Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
  86. # it's not a symbolic ref, but in a Git repo.
  87. function git_current_branch() {
  88. local ref
  89. ref=$(__git_prompt_git symbolic-ref --quiet HEAD 2> /dev/null)
  90. local ret=$?
  91. if [[ $ret != 0 ]]; then
  92. [[ $ret == 128 ]] && return # no git repo.
  93. ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return
  94. fi
  95. echo ${ref#refs/heads/}
  96. }
  97. # Gets the number of commits ahead from remote
  98. function git_commits_ahead() {
  99. if __git_prompt_git rev-parse --git-dir &>/dev/null; then
  100. local commits="$(__git_prompt_git rev-list --count @{upstream}..HEAD 2>/dev/null)"
  101. if [[ -n "$commits" && "$commits" != 0 ]]; then
  102. echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$commits$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
  103. fi
  104. fi
  105. }
  106. # Gets the number of commits behind remote
  107. function git_commits_behind() {
  108. if __git_prompt_git rev-parse --git-dir &>/dev/null; then
  109. local commits="$(__git_prompt_git rev-list --count HEAD..@{upstream} 2>/dev/null)"
  110. if [[ -n "$commits" && "$commits" != 0 ]]; then
  111. echo "$ZSH_THEME_GIT_COMMITS_BEHIND_PREFIX$commits$ZSH_THEME_GIT_COMMITS_BEHIND_SUFFIX"
  112. fi
  113. fi
  114. }
  115. # Outputs if current branch is ahead of remote
  116. function git_prompt_ahead() {
  117. if [[ -n "$(__git_prompt_git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
  118. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  119. fi
  120. }
  121. # Outputs if current branch is behind remote
  122. function git_prompt_behind() {
  123. if [[ -n "$(__git_prompt_git rev-list HEAD..origin/$(git_current_branch) 2> /dev/null)" ]]; then
  124. echo "$ZSH_THEME_GIT_PROMPT_BEHIND"
  125. fi
  126. }
  127. # Outputs if current branch exists on remote or not
  128. function git_prompt_remote() {
  129. if [[ -n "$(__git_prompt_git show-ref origin/$(git_current_branch) 2> /dev/null)" ]]; then
  130. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS"
  131. else
  132. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_MISSING"
  133. fi
  134. }
  135. # Formats prompt string for current git commit short SHA
  136. function git_prompt_short_sha() {
  137. local SHA
  138. 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"
  139. }
  140. # Formats prompt string for current git commit long SHA
  141. function git_prompt_long_sha() {
  142. local SHA
  143. SHA=$(__git_prompt_git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  144. }
  145. function git_prompt_status() {
  146. [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return
  147. # Maps a git status prefix to an internal constant
  148. # This cannot use the prompt constants, as they may be empty
  149. local -A prefix_constant_map
  150. prefix_constant_map=(
  151. '\?\? ' 'UNTRACKED'
  152. 'A ' 'ADDED'
  153. 'M ' 'ADDED'
  154. 'MM ' 'MODIFIED'
  155. ' M ' 'MODIFIED'
  156. 'AM ' 'MODIFIED'
  157. ' T ' 'MODIFIED'
  158. 'R ' 'RENAMED'
  159. ' D ' 'DELETED'
  160. 'D ' 'DELETED'
  161. 'UU ' 'UNMERGED'
  162. 'ahead' 'AHEAD'
  163. 'behind' 'BEHIND'
  164. 'diverged' 'DIVERGED'
  165. 'stashed' 'STASHED'
  166. )
  167. # Maps the internal constant to the prompt theme
  168. local -A constant_prompt_map
  169. constant_prompt_map=(
  170. 'UNTRACKED' "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
  171. 'ADDED' "$ZSH_THEME_GIT_PROMPT_ADDED"
  172. 'MODIFIED' "$ZSH_THEME_GIT_PROMPT_MODIFIED"
  173. 'RENAMED' "$ZSH_THEME_GIT_PROMPT_RENAMED"
  174. 'DELETED' "$ZSH_THEME_GIT_PROMPT_DELETED"
  175. 'UNMERGED' "$ZSH_THEME_GIT_PROMPT_UNMERGED"
  176. 'AHEAD' "$ZSH_THEME_GIT_PROMPT_AHEAD"
  177. 'BEHIND' "$ZSH_THEME_GIT_PROMPT_BEHIND"
  178. 'DIVERGED' "$ZSH_THEME_GIT_PROMPT_DIVERGED"
  179. 'STASHED' "$ZSH_THEME_GIT_PROMPT_STASHED"
  180. )
  181. # The order that the prompt displays should be added to the prompt
  182. local status_constants
  183. status_constants=(
  184. UNTRACKED ADDED MODIFIED RENAMED DELETED
  185. STASHED UNMERGED AHEAD BEHIND DIVERGED
  186. )
  187. local status_text
  188. status_text="$(__git_prompt_git status --porcelain -b 2> /dev/null)"
  189. # Don't continue on a catastrophic failure
  190. if [[ $? -eq 128 ]]; then
  191. return 1
  192. fi
  193. # A lookup table of each git status encountered
  194. local -A statuses_seen
  195. if __git_prompt_git rev-parse --verify refs/stash &>/dev/null; then
  196. statuses_seen[STASHED]=1
  197. fi
  198. local status_lines
  199. status_lines=("${(@f)${status_text}}")
  200. # If the tracking line exists, get and parse it
  201. if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
  202. local branch_statuses
  203. branch_statuses=("${(@s/,/)match}")
  204. for branch_status in $branch_statuses; do
  205. if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
  206. continue
  207. fi
  208. local last_parsed_status=$prefix_constant_map[$match[1]]
  209. statuses_seen[$last_parsed_status]=$match[2]
  210. done
  211. fi
  212. # For each status prefix, do a regex comparison
  213. for status_prefix in ${(k)prefix_constant_map}; do
  214. local status_constant="${prefix_constant_map[$status_prefix]}"
  215. local status_regex=$'(^|\n)'"$status_prefix"
  216. if [[ "$status_text" =~ $status_regex ]]; then
  217. statuses_seen[$status_constant]=1
  218. fi
  219. done
  220. # Display the seen statuses in the order specified
  221. local status_prompt
  222. for status_constant in $status_constants; do
  223. if (( ${+statuses_seen[$status_constant]} )); then
  224. local next_display=$constant_prompt_map[$status_constant]
  225. status_prompt="$next_display$status_prompt"
  226. fi
  227. done
  228. echo $status_prompt
  229. }
  230. # Outputs the name of the current user
  231. # Usage example: $(git_current_user_name)
  232. function git_current_user_name() {
  233. __git_prompt_git config user.name 2>/dev/null
  234. }
  235. # Outputs the email of the current user
  236. # Usage example: $(git_current_user_email)
  237. function git_current_user_email() {
  238. __git_prompt_git config user.email 2>/dev/null
  239. }
  240. # Output the name of the root directory of the git repository
  241. # Usage example: $(git_repo_name)
  242. function git_repo_name() {
  243. local repo_path
  244. if repo_path="$(__git_prompt_git rev-parse --show-toplevel 2>/dev/null)" && [[ -n "$repo_path" ]]; then
  245. echo ${repo_path:t}
  246. fi
  247. }