git.zsh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 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 '^AD ' &> /dev/null); then
  76. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  77. fi
  78. if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
  79. STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  80. fi
  81. echo $STATUS
  82. }
  83. #compare the provided version of git to the version installed and on path
  84. #prints 1 if input version <= installed version
  85. #prints -1 otherwise
  86. function git_compare_version() {
  87. local INPUT_GIT_VERSION=$1;
  88. local INSTALLED_GIT_VERSION
  89. INPUT_GIT_VERSION=(${(s/./)INPUT_GIT_VERSION});
  90. INSTALLED_GIT_VERSION=($(git --version));
  91. INSTALLED_GIT_VERSION=(${(s/./)INSTALLED_GIT_VERSION[3]});
  92. for i in {1..3}; do
  93. if [[ $INSTALLED_GIT_VERSION[$i] -lt $INPUT_GIT_VERSION[$i] ]]; then
  94. echo -1
  95. return 0
  96. fi
  97. done
  98. echo 1
  99. }
  100. #this is unlikely to change so make it all statically assigned
  101. POST_1_7_2_GIT=$(git_compare_version "1.7.2")
  102. #clean up the namespace slightly by removing the checker function
  103. unset -f git_compare_version