git.zsh 12 KB

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