2
0

git.zsh 13 KB

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