git.zsh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. if [[ -n $(git status -s 2> /dev/null) ]]; then
  9. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  10. else
  11. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  12. fi
  13. }
  14. # get the difference between the local and remote branches
  15. git_remote_status() {
  16. remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
  17. if [[ -n ${remote} ]] ; then
  18. ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
  19. behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
  20. if [ $ahead -eq 0 ] && [ $behind -gt 0 ]
  21. then
  22. echo "$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
  23. elif [ $ahead -gt 0 ] && [ $behind -eq 0 ]
  24. then
  25. echo "$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
  26. elif [ $ahead -gt 0 ] && [ $behind -gt 0 ]
  27. then
  28. echo "$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
  29. fi
  30. fi
  31. }
  32. # Checks if there are commits ahead from remote
  33. function git_prompt_ahead() {
  34. if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
  35. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  36. fi
  37. }
  38. # Formats prompt string for current git commit short SHA
  39. function git_prompt_short_sha() {
  40. SHA=$(git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  41. }
  42. # Formats prompt string for current git commit long SHA
  43. function git_prompt_long_sha() {
  44. SHA=$(git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  45. }
  46. # Get the status of the working tree
  47. git_prompt_status() {
  48. INDEX=$(git status --porcelain 2> /dev/null)
  49. STATUS=""
  50. if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
  51. STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
  52. fi
  53. if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
  54. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  55. elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
  56. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  57. fi
  58. if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
  59. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  60. elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
  61. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  62. elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
  63. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  64. fi
  65. if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
  66. STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
  67. fi
  68. if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
  69. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  70. elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
  71. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  72. fi
  73. if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
  74. STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  75. fi
  76. echo $STATUS
  77. }