git.zsh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # get the name of the branch we are on
  2. function git_prompt_info() {
  3. if [[ "$(git config --get oh-my-zsh.hide-status)" != "1" ]]; then
  4. ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
  5. ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
  6. echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
  7. fi
  8. }
  9. # Checks if working tree is dirty
  10. parse_git_dirty() {
  11. local SUBMODULE_SYNTAX=''
  12. local GIT_STATUS=''
  13. local CLEAN_MESSAGE='nothing to commit (working directory clean)'
  14. if [[ "$(command git config --get oh-my-zsh.hide-status)" != "1" ]]; then
  15. if [[ $POST_1_7_2_GIT -gt 0 ]]; then
  16. SUBMODULE_SYNTAX="--ignore-submodules=dirty"
  17. fi
  18. if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]; then
  19. GIT_STATUS=$(command git status -s ${SUBMODULE_SYNTAX} -uno 2> /dev/null | tail -n1)
  20. else
  21. GIT_STATUS=$(command git status -s ${SUBMODULE_SYNTAX} 2> /dev/null | tail -n1)
  22. fi
  23. if [[ -n $GIT_STATUS ]]; then
  24. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  25. else
  26. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  27. fi
  28. else
  29. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  30. fi
  31. }
  32. # get the difference between the local and remote branches
  33. git_remote_status() {
  34. remote=${$(command git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
  35. if [[ -n ${remote} ]] ; then
  36. ahead=$(command git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
  37. behind=$(command git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
  38. if [ $ahead -eq 0 ] && [ $behind -gt 0 ]
  39. then
  40. echo "$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
  41. elif [ $ahead -gt 0 ] && [ $behind -eq 0 ]
  42. then
  43. echo "$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
  44. elif [ $ahead -gt 0 ] && [ $behind -gt 0 ]
  45. then
  46. echo "$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
  47. fi
  48. fi
  49. }
  50. # Checks if there are commits ahead from remote
  51. function git_prompt_ahead() {
  52. if $(echo "$(command git log @{upstream}..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
  53. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  54. fi
  55. }
  56. # Gets the number of commits ahead from remote
  57. function git_commits_ahead() {
  58. if $(echo "$(command git log @{upstream}..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
  59. echo "$(command git log @{upstream}..HEAD | grep '^commit' | wc -l | tr -d ' ')"
  60. fi
  61. }
  62. # Formats prompt string for current git commit short SHA
  63. function git_prompt_short_sha() {
  64. SHA=$(command git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  65. }
  66. # Formats prompt string for current git commit long SHA
  67. function git_prompt_long_sha() {
  68. SHA=$(command git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  69. }
  70. # Get the status of the working tree
  71. git_prompt_status() {
  72. INDEX=$(command git status --porcelain -b 2> /dev/null)
  73. STATUS=""
  74. if $(echo "$INDEX" | grep -E '^\?\? ' &> /dev/null); then
  75. STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
  76. fi
  77. if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
  78. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  79. elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
  80. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  81. fi
  82. if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
  83. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  84. elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
  85. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  86. elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
  87. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  88. fi
  89. if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
  90. STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
  91. fi
  92. if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
  93. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  94. elif $(echo "$INDEX" | grep '^D ' &> /dev/null); then
  95. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  96. elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
  97. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  98. fi
  99. if $(command git rev-parse --verify refs/stash >/dev/null 2>&1); then
  100. STATUS="$ZSH_THEME_GIT_PROMPT_STASHED$STATUS"
  101. fi
  102. if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
  103. STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  104. fi
  105. if $(echo "$INDEX" | grep '^## .*ahead' &> /dev/null); then
  106. STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS"
  107. fi
  108. if $(echo "$INDEX" | grep '^## .*behind' &> /dev/null); then
  109. STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS"
  110. fi
  111. if $(echo "$INDEX" | grep '^## .*diverged' &> /dev/null); then
  112. STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS"
  113. fi
  114. echo $STATUS
  115. }
  116. #compare the provided version of git to the version installed and on path
  117. #prints 1 if input version <= installed version
  118. #prints -1 otherwise
  119. function git_compare_version() {
  120. local INPUT_GIT_VERSION=$1;
  121. local INSTALLED_GIT_VERSION
  122. INPUT_GIT_VERSION=(${(s/./)INPUT_GIT_VERSION});
  123. INSTALLED_GIT_VERSION=($(command git --version 2>/dev/null));
  124. INSTALLED_GIT_VERSION=(${(s/./)INSTALLED_GIT_VERSION[3]});
  125. for i in {1..3}; do
  126. if [[ $INSTALLED_GIT_VERSION[$i] -lt $INPUT_GIT_VERSION[$i] ]]; then
  127. echo -1
  128. return 0
  129. fi
  130. done
  131. echo 1
  132. }
  133. #this is unlikely to change so make it all statically assigned
  134. POST_1_7_2_GIT=$(git_compare_version "1.7.2")
  135. #clean up the namespace slightly by removing the checker function
  136. unset -f git_compare_version