git.zsh 4.5 KB

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