git.zsh 12 KB

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