git.zsh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 --ignore-submodules=dirty 2> /dev/null) ]]; then
  9. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  10. else
  11. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  12. fi
  13. }
  14. # Checks if there are commits ahead from remote
  15. function git_prompt_ahead() {
  16. if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
  17. echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  18. fi
  19. }
  20. # Formats prompt string for current git commit short SHA
  21. function git_prompt_short_sha() {
  22. SHA=$(git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  23. }
  24. # Formats prompt string for current git commit long SHA
  25. function git_prompt_long_sha() {
  26. SHA=$(git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  27. }
  28. # Get the status of the working tree
  29. git_prompt_status() {
  30. INDEX=$(git status --porcelain 2> /dev/null)
  31. STATUS=""
  32. if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
  33. STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
  34. fi
  35. if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
  36. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  37. elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
  38. STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  39. fi
  40. if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
  41. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  42. elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
  43. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  44. elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
  45. STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  46. fi
  47. if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
  48. STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
  49. fi
  50. if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
  51. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  52. elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
  53. STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  54. fi
  55. if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
  56. STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  57. fi
  58. echo $STATUS
  59. }