git.zsh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. # Outputs the name of the previously checked out branch
  148. # Usage example: git pull origin $(git_current_branch)
  149. # rev-parse --symbolic-full-name @{-1} only prints if it is a branch
  150. function git_previous_branch() {
  151. local ref
  152. ref=$(__git_prompt_git rev-parse --quiet --symbolic-full-name @{-1} 2> /dev/null)
  153. local ret=$?
  154. if [[ $ret != 0 ]] || [[ -z $ref ]]; then
  155. return # no git repo or non-branch previous ref
  156. fi
  157. echo ${ref#refs/heads/}
  158. }
  159. # Gets the number of commits ahead from remote
  160. function git_commits_ahead() {
  161. if __git_prompt_git rev-parse --git-dir &>/dev/null; then
  162. local commits="$(__git_prompt_git rev-list --count @{upstream}..HEAD 2>/dev/null)"
  163. if [[ -n "$commits" && "$commits" != 0 ]]; then
  164. echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$commits$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
  165. fi
  166. fi
  167. }
  168. # Gets the number of commits behind remote
  169. function git_commits_behind() {
  170. if __git_prompt_git rev-parse --git-dir &>/dev/null; then
  171. local commits="$(__git_prompt_git rev-list --count HEAD..@{upstream} 2>/dev/null)"
  172. if [[ -n "$commits" && "$commits" != 0 ]]; then
  173. echo "$ZSH_THEME_GIT_COMMITS_BEHIND_PREFIX$commits$ZSH_THEME_GIT_COMMITS_BEHIND_SUFFIX"
  174. fi
  175. fi
  176. }
  177. # Outputs if current branch is ahead of remote
  178. function git_prompt_ahead() {
  179. if [[ -n "$(__git_prompt_git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
  180. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  181. fi
  182. }
  183. # Outputs if current branch is behind remote
  184. function git_prompt_behind() {
  185. if [[ -n "$(__git_prompt_git rev-list HEAD..origin/$(git_current_branch) 2> /dev/null)" ]]; then
  186. echo "$ZSH_THEME_GIT_PROMPT_BEHIND"
  187. fi
  188. }
  189. # Outputs if current branch exists on remote or not
  190. function git_prompt_remote() {
  191. if [[ -n "$(__git_prompt_git show-ref origin/$(git_current_branch) 2> /dev/null)" ]]; then
  192. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS"
  193. else
  194. echo "$ZSH_THEME_GIT_PROMPT_REMOTE_MISSING"
  195. fi
  196. }
  197. # Formats prompt string for current git commit short SHA
  198. function git_prompt_short_sha() {
  199. local SHA
  200. 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"
  201. }
  202. # Formats prompt string for current git commit long SHA
  203. function git_prompt_long_sha() {
  204. local SHA
  205. SHA=$(__git_prompt_git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  206. }
  207. function _omz_git_prompt_status() {
  208. [[ "$(__git_prompt_git config --get oh-my-zsh.hide-status 2>/dev/null)" = 1 ]] && return
  209. # Maps a git status prefix to an internal constant
  210. # This cannot use the prompt constants, as they may be empty
  211. local -A prefix_constant_map
  212. prefix_constant_map=(
  213. '\?\? ' 'UNTRACKED'
  214. 'A ' 'ADDED'
  215. 'M ' 'MODIFIED'
  216. 'MM ' 'MODIFIED'
  217. ' M ' 'MODIFIED'
  218. 'AM ' 'MODIFIED'
  219. ' T ' 'MODIFIED'
  220. 'R ' 'RENAMED'
  221. ' D ' 'DELETED'
  222. 'D ' 'DELETED'
  223. 'UU ' 'UNMERGED'
  224. 'ahead' 'AHEAD'
  225. 'behind' 'BEHIND'
  226. 'diverged' 'DIVERGED'
  227. 'stashed' 'STASHED'
  228. )
  229. # Maps the internal constant to the prompt theme
  230. local -A constant_prompt_map
  231. constant_prompt_map=(
  232. 'UNTRACKED' "$ZSH_THEME_GIT_PROMPT_UNTRACKED"
  233. 'ADDED' "$ZSH_THEME_GIT_PROMPT_ADDED"
  234. 'MODIFIED' "$ZSH_THEME_GIT_PROMPT_MODIFIED"
  235. 'RENAMED' "$ZSH_THEME_GIT_PROMPT_RENAMED"
  236. 'DELETED' "$ZSH_THEME_GIT_PROMPT_DELETED"
  237. 'UNMERGED' "$ZSH_THEME_GIT_PROMPT_UNMERGED"
  238. 'AHEAD' "$ZSH_THEME_GIT_PROMPT_AHEAD"
  239. 'BEHIND' "$ZSH_THEME_GIT_PROMPT_BEHIND"
  240. 'DIVERGED' "$ZSH_THEME_GIT_PROMPT_DIVERGED"
  241. 'STASHED' "$ZSH_THEME_GIT_PROMPT_STASHED"
  242. )
  243. # The order that the prompt displays should be added to the prompt
  244. local status_constants
  245. status_constants=(
  246. UNTRACKED ADDED MODIFIED RENAMED DELETED
  247. STASHED UNMERGED AHEAD BEHIND DIVERGED
  248. )
  249. local status_text
  250. status_text="$(__git_prompt_git status --porcelain -b 2> /dev/null)"
  251. # Don't continue on a catastrophic failure
  252. if [[ $? -eq 128 ]]; then
  253. return 1
  254. fi
  255. # A lookup table of each git status encountered
  256. local -A statuses_seen
  257. if __git_prompt_git rev-parse --verify refs/stash &>/dev/null; then
  258. statuses_seen[STASHED]=1
  259. fi
  260. local status_lines
  261. status_lines=("${(@f)${status_text}}")
  262. # If the tracking line exists, get and parse it
  263. if [[ "$status_lines[1]" =~ "^## [^ ]+ \[(.*)\]" ]]; then
  264. local branch_statuses
  265. branch_statuses=("${(@s/,/)match}")
  266. for branch_status in $branch_statuses; do
  267. if [[ ! $branch_status =~ "(behind|diverged|ahead) ([0-9]+)?" ]]; then
  268. continue
  269. fi
  270. local last_parsed_status=$prefix_constant_map[$match[1]]
  271. statuses_seen[$last_parsed_status]=$match[2]
  272. done
  273. fi
  274. # For each status prefix, do a regex comparison
  275. for status_prefix in ${(k)prefix_constant_map}; do
  276. local status_constant="${prefix_constant_map[$status_prefix]}"
  277. local status_regex=$'(^|\n)'"$status_prefix"
  278. if [[ "$status_text" =~ $status_regex ]]; then
  279. statuses_seen[$status_constant]=1
  280. fi
  281. done
  282. # Display the seen statuses in the order specified
  283. local status_prompt
  284. for status_constant in $status_constants; do
  285. if (( ${+statuses_seen[$status_constant]} )); then
  286. local next_display=$constant_prompt_map[$status_constant]
  287. status_prompt="$next_display$status_prompt"
  288. fi
  289. done
  290. echo $status_prompt
  291. }
  292. # Outputs the name of the current user
  293. # Usage example: $(git_current_user_name)
  294. function git_current_user_name() {
  295. __git_prompt_git config user.name 2>/dev/null
  296. }
  297. # Outputs the email of the current user
  298. # Usage example: $(git_current_user_email)
  299. function git_current_user_email() {
  300. __git_prompt_git config user.email 2>/dev/null
  301. }
  302. # Output the name of the root directory of the git repository
  303. # Usage example: $(git_repo_name)
  304. function git_repo_name() {
  305. local repo_path
  306. if repo_path="$(__git_prompt_git rev-parse --show-toplevel 2>/dev/null)" && [[ -n "$repo_path" ]]; then
  307. echo ${repo_path:t}
  308. fi
  309. }