git.zsh 4.6 KB

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