git.zsh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. autoload -Uz is-at-least
  2. # The git prompt's git commands are read-only and should not interfere with
  3. # other processes. This environment variable is equivalent to running with `git
  4. # --no-optional-locks`, but falls back gracefully for older versions of git.
  5. # See git(1) for and git-status(1) for a description of that flag.
  6. #
  7. # We wrap in a local function instead of exporting the variable directly in
  8. # order to avoid interfering with manually-run git commands by the user.
  9. function __git_prompt_git() {
  10. GIT_OPTIONAL_LOCKS=0 command git "$@"
  11. }
  12. function _omz_git_prompt_info() {
  13. # If we are on a folder not tracked by git, get out.
  14. # Otherwise, check for hide-info at global and local repository level
  15. if ! __git_prompt_git rev-parse --git-dir &> /dev/null \
  16. || [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then
  17. return 0
  18. fi
  19. # Get either:
  20. # - the current branch name
  21. # - the tag name if we are on a tag
  22. # - the short SHA of the current commit
  23. local ref
  24. ref=$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null) \
  25. || ref=$(__git_prompt_git describe --tags --exact-match HEAD 2> /dev/null) \
  26. || ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) \
  27. || return 0
  28. # Use global ZSH_THEME_GIT_SHOW_UPSTREAM=1 for including upstream remote info
  29. local upstream
  30. if (( ${+ZSH_THEME_GIT_SHOW_UPSTREAM} )); then
  31. upstream=$(__git_prompt_git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}" 2>/dev/null) \
  32. && upstream=" -> ${upstream}"
  33. fi
  34. echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${ref:gs/%/%%}${upstream:gs/%/%%}$(parse_git_dirty)${ZSH_THEME_GIT_PROMPT_SUFFIX}"
  35. }
  36. # Use async version if setting is enabled, or unset but zsh version is at least 5.0.6.
  37. # This avoids async prompt issues caused by previous zsh versions:
  38. # - https://github.com/ohmyzsh/ohmyzsh/issues/12331
  39. # - https://github.com/ohmyzsh/ohmyzsh/issues/12360
  40. # TODO(2024-06-12): @mcornella remove workaround when CentOS 7 reaches EOL
  41. if zstyle -t ':omz:alpha:lib:git' async-prompt \
  42. || { is-at-least 5.0.6 && zstyle -T ':omz:alpha:lib:git' async-prompt }; then
  43. function git_prompt_info() {
  44. if [[ -n "${_OMZ_ASYNC_OUTPUT[_omz_git_prompt_info]}" ]]; then
  45. echo -n "${_OMZ_ASYNC_OUTPUT[_omz_git_prompt_info]}"
  46. fi
  47. }
  48. function git_prompt_status() {
  49. if [[ -n "${_OMZ_ASYNC_OUTPUT[_omz_git_prompt_status]}" ]]; then
  50. echo -n "${_OMZ_ASYNC_OUTPUT[_omz_git_prompt_status]}"
  51. fi
  52. }
  53. # Conditionally register the async handler, only if it's needed in $PROMPT
  54. # or any of the other prompt variables
  55. function _defer_async_git_register() {
  56. # Check if git_prompt_info is used in a prompt variable
  57. case "${PS1}:${PS2}:${PS3}:${PS4}:${RPROMPT}:${RPS1}:${RPS2}:${RPS3}:${RPS4}" in
  58. *(\$\(git_prompt_info\)|\`git_prompt_info\`)*)
  59. _omz_register_handler _omz_git_prompt_info
  60. ;;
  61. esac
  62. case "${PS1}:${PS2}:${PS3}:${PS4}:${RPROMPT}:${RPS1}:${RPS2}:${RPS3}:${RPS4}" in
  63. *(\$\(git_prompt_status\)|\`git_prompt_status\`)*)
  64. _omz_register_handler _omz_git_prompt_status
  65. ;;
  66. esac
  67. add-zsh-hook -d precmd _defer_async_git_register
  68. unset -f _defer_async_git_register
  69. }
  70. # Register the async handler first. This needs to be done before
  71. # the async request prompt is run
  72. precmd_functions=(_defer_async_git_register $precmd_functions)
  73. else
  74. function git_prompt_info() {
  75. _omz_git_prompt_info
  76. }
  77. function git_prompt_status() {
  78. _omz_git_prompt_status
  79. }
  80. fi
  81. # Checks if working tree is dirty
  82. function parse_git_dirty() {
  83. local STATUS
  84. local -a FLAGS
  85. FLAGS=('--porcelain')
  86. if [[ "$(__git_prompt_git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
  87. if [[ "${DISABLE_UNTRACKED_FILES_DIRTY:-}" == "true" ]]; then
  88. FLAGS+='--untracked-files=no'
  89. fi
  90. case "${GIT_STATUS_IGNORE_SUBMODULES:-}" in
  91. git)
  92. # let git decide (this respects per-repo config in .gitmodules)
  93. ;;
  94. *)
  95. # if unset: ignore dirty submodules
  96. # other values are passed to --ignore-submodules
  97. FLAGS+="--ignore-submodules=${GIT_STATUS_IGNORE_SUBMODULES:-dirty}"
  98. ;;
  99. esac
  100. STATUS=$(__git_prompt_git status ${FLAGS} 2> /dev/null | tail -n 1)
  101. fi
  102. if [[ -n $STATUS ]]; then
  103. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  104. else
  105. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  106. fi
  107. }
  108. # Gets the difference between the local and remote branches
  109. function git_remote_status() {
  110. local remote ahead behind git_remote_status git_remote_status_detailed
  111. remote=${$(__git_prompt_git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
  112. if [[ -n ${remote} ]]; then
  113. ahead=$(__git_prompt_git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
  114. behind=$(__git_prompt_git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
  115. if [[ $ahead -eq 0 ]] && [[ $behind -eq 0 ]]; then
  116. git_remote_status="$ZSH_THEME_GIT_PROMPT_EQUAL_REMOTE"
  117. elif [[ $ahead -gt 0 ]] && [[ $behind -eq 0 ]]; then
  118. git_remote_status="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
  119. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}"
  120. elif [[ $behind -gt 0 ]] && [[ $ahead -eq 0 ]]; then
  121. git_remote_status="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
  122. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  123. elif [[ $ahead -gt 0 ]] && [[ $behind -gt 0 ]]; then
  124. git_remote_status="$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
  125. 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%}"
  126. fi
  127. if [[ -n $ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]; then
  128. git_remote_status="$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX${remote:gs/%/%%}$git_remote_status_detailed$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX"
  129. fi
  130. echo $git_remote_status
  131. fi
  132. }
  133. # Outputs the name of the current branch
  134. # Usage example: git pull origin $(git_current_branch)
  135. # Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
  136. # it's not a symbolic ref, but in a Git repo.
  137. function git_current_branch() {
  138. local ref
  139. ref=$(__git_prompt_git symbolic-ref --quiet HEAD 2> /dev/null)
  140. local ret=$?
  141. if [[ $ret != 0 ]]; then
  142. [[ $ret == 128 ]] && return # no git repo.
  143. ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) || return
  144. fi
  145. echo ${ref#refs/heads/}
  146. }
  147. # Gets the number of commits ahead from remote
  148. function git_commits_ahead() {
  149. if __git_prompt_git rev-parse --git-dir &>/dev/null; then
  150. local commits="$(__git_prompt_git rev-list --count @{upstream}..HEAD 2>/dev/null)"
  151. if [[ -n "$commits" && "$commits" != 0 ]]; then
  152. echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$commits$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
  153. fi
  154. fi
  155. }
  156. # Gets the number of commits behind remote
  157. function git_commits_behind() {
  158. if __git_prompt_git rev-parse --git-dir &>/dev/null; then
  159. local commits="$(__git_prompt_git rev-list --count HEAD..@{upstream} 2>/dev/null)"
  160. if [[ -n "$commits" && "$commits" != 0 ]]; then
  161. echo "$ZSH_THEME_GIT_COMMITS_BEHIND_PREFIX$commits$ZSH_THEME_GIT_COMMITS_BEHIND_SUFFIX"
  162. fi
  163. fi
  164. }
  165. # Outputs if current branch is ahead of remote
  166. function git_prompt_ahead() {
  167. if [[ -n "$(__git_prompt_git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
  168. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  169. fi
  170. }
  171. # Outputs if current branch is behind remote
  172. function git_prompt_behind() {
  173. if [[ -n "$(__git_prompt_git rev-list HEAD..origin/$(git_current_branch) 2> /dev/null)" ]]; then
  174. echo "$ZSH_THEME_GIT_PROMPT_BEHIND"
  175. fi
  176. }
  177. # Outputs if current branch exists on remote or not
  178. function git_prompt_remote() {
  179. if [[ -n "$(__git_prompt_git show-ref origin/$(git_current_branch) 2> /dev/null)" ]]; then
  180. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS"
  181. else
  182. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_MISSING"
  183. fi
  184. }
  185. # Formats prompt string for current git commit short SHA
  186. function git_prompt_short_sha() {
  187. local SHA
  188. 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"
  189. }
  190. # Formats prompt string for current git commit long SHA
  191. function git_prompt_long_sha() {
  192. local SHA
  193. SHA=$(__git_prompt_git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  194. }
  195. function _omz_git_prompt_status() {
  196. [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return
  197. # Maps a git status prefix to an internal constant
  198. # This cannot use the prompt constants, as they may be empty
  199. local -A prefix_constant_map
  200. prefix_constant_map=(
  201. '\?\? ' 'UNTRACKED'
  202. 'A ' 'ADDED'
  203. 'M ' 'ADDED'
  204. 'MM ' 'MODIFIED'
  205. ' M ' 'MODIFIED'
  206. 'AM ' 'MODIFIED'
  207. ' T ' 'MODIFIED'
  208. 'R ' 'RENAMED'
  209. ' D ' 'DELETED'
  210. 'D ' 'DELETED'
  211. 'UU ' 'UNMERGED'
  212. 'ahead' 'AHEAD'
  213. 'behind' 'BEHIND'
  214. 'diverged' 'DIVERGED'
  215. 'stashed' 'STASHED'
  216. )
  217. # Maps the internal constant to the prompt theme
  218. local -A constant_prompt_map
  219. constant_prompt_map=(
  220. 'UNTRACKED' "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
  221. 'ADDED' "$ZSH_THEME_GIT_PROMPT_ADDED"
  222. 'MODIFIED' "$ZSH_THEME_GIT_PROMPT_MODIFIED"
  223. 'RENAMED' "$ZSH_THEME_GIT_PROMPT_RENAMED"
  224. 'DELETED' "$ZSH_THEME_GIT_PROMPT_DELETED"
  225. 'UNMERGED' "$ZSH_THEME_GIT_PROMPT_UNMERGED"
  226. 'AHEAD' "$ZSH_THEME_GIT_PROMPT_AHEAD"
  227. 'BEHIND' "$ZSH_THEME_GIT_PROMPT_BEHIND"
  228. 'DIVERGED' "$ZSH_THEME_GIT_PROMPT_DIVERGED"
  229. 'STASHED' "$ZSH_THEME_GIT_PROMPT_STASHED"
  230. )
  231. # The order that the prompt displays should be added to the prompt
  232. local status_constants
  233. status_constants=(
  234. UNTRACKED ADDED MODIFIED RENAMED DELETED
  235. STASHED UNMERGED AHEAD BEHIND DIVERGED
  236. )
  237. local status_text
  238. status_text="$(__git_prompt_git status --porcelain -b 2> /dev/null)"
  239. # Don't continue on a catastrophic failure
  240. if [[ $? -eq 128 ]]; then
  241. return 1
  242. fi
  243. # A lookup table of each git status encountered
  244. local -A statuses_seen
  245. if __git_prompt_git rev-parse --verify refs/stash &>/dev/null; then
  246. statuses_seen[STASHED]=1
  247. fi
  248. local status_lines
  249. status_lines=("${(@f)${status_text}}")
  250. # If the tracking line exists, get and parse it
  251. if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
  252. local branch_statuses
  253. branch_statuses=("${(@s/,/)match}")
  254. for branch_status in $branch_statuses; do
  255. if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
  256. continue
  257. fi
  258. local last_parsed_status=$prefix_constant_map[$match[1]]
  259. statuses_seen[$last_parsed_status]=$match[2]
  260. done
  261. fi
  262. # For each status prefix, do a regex comparison
  263. for status_prefix in ${(k)prefix_constant_map}; do
  264. local status_constant="${prefix_constant_map[$status_prefix]}"
  265. local status_regex=$'(^|\n)'"$status_prefix"
  266. if [[ "$status_text" =~ $status_regex ]]; then
  267. statuses_seen[$status_constant]=1
  268. fi
  269. done
  270. # Display the seen statuses in the order specified
  271. local status_prompt
  272. for status_constant in $status_constants; do
  273. if (( ${+statuses_seen[$status_constant]} )); then
  274. local next_display=$constant_prompt_map[$status_constant]
  275. status_prompt="$next_display$status_prompt"
  276. fi
  277. done
  278. echo $status_prompt
  279. }
  280. # Outputs the name of the current user
  281. # Usage example: $(git_current_user_name)
  282. function git_current_user_name() {
  283. __git_prompt_git config user.name 2>/dev/null
  284. }
  285. # Outputs the email of the current user
  286. # Usage example: $(git_current_user_email)
  287. function git_current_user_email() {
  288. __git_prompt_git config user.email 2>/dev/null
  289. }
  290. # Output the name of the root directory of the git repository
  291. # Usage example: $(git_repo_name)
  292. function git_repo_name() {
  293. local repo_path
  294. if repo_path="$(__git_prompt_git rev-parse --show-toplevel 2>/dev/null)" && [[ -n "$repo_path" ]]; then
  295. echo ${repo_path:t}
  296. fi
  297. }