git.zsh 4.8 KB

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