git.zsh 12 KB

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