git.zsh 5.0 KB

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