git.zsh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # get the name of the branch we are on
  2. function git_prompt_info() {
  3. ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  4. echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
  5. }
  6. # Checks if working tree is dirty
  7. parse_git_dirty() {
  8. local SUBMODULE_SYNTAX=''
  9. if [[ $POST_1_7_2_GIT -gt 0 ]]; then
  10. SUBMODULE_SYNTAX="--ignore-submodules=dirty"
  11. fi
  12. if [[ -n $(git status -s ${SUBMODULE_SYNTAX} 2> /dev/null) ]]; then
  13. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  14. else
  15. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  16. fi
  17. }
  18. # Checks if there are commits ahead from remote
  19. function git_prompt_ahead() {
  20. if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
  21. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  22. fi
  23. }
  24. # Formats prompt string for current git commit short SHA
  25. function git_prompt_short_sha() {
  26. SHA=$(git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  27. }
  28. # Formats prompt string for current git commit long SHA
  29. function git_prompt_long_sha() {
  30. SHA=$(git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  31. }
  32. # Get the status of the working tree
  33. git_prompt_status() {
  34. INDEX=$(git status --porcelain 2> /dev/null)
  35. STATUS=""
  36. if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
  37. STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
  38. fi
  39. if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
  40. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  41. elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
  42. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  43. fi
  44. if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
  45. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  46. elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
  47. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  48. elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
  49. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  50. fi
  51. if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
  52. STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
  53. fi
  54. if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
  55. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  56. elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
  57. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  58. fi
  59. if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
  60. STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  61. fi
  62. echo $STATUS
  63. }
  64. #compare the provided version of git to the version installed and on path
  65. #prints 1 if input version <= installed version
  66. #prints -1 otherwise
  67. function git_compare_version() {
  68. local INPUT_GIT_VERSION=$1;
  69. local INSTALLED_GIT_VERSION
  70. INPUT_GIT_VERSION=(${(s/./)INPUT_GIT_VERSION});
  71. INSTALLED_GIT_VERSION=($(git --version));
  72. INSTALLED_GIT_VERSION=(${(s/./)INSTALLED_GIT_VERSION[3]});
  73. for i in {1..3}; do
  74. if [[ $INSTALLED_GIT_VERSION[$i] -lt $INPUT_GIT_VERSION[$i] ]]; then
  75. echo -1
  76. return 0
  77. fi
  78. done
  79. echo 1
  80. }
  81. #this is unlikely to change so make it all statically assigned
  82. POST_1_7_2_GIT=$(git_compare_version "1.7.2")
  83. #clean up the namespace slightly by removing the checker function
  84. unset -f git_compare_version