git-prompt.plugin.zsh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # ZSH Git Prompt Plugin from:
  2. # http://github.com/olivierverdier/zsh-git-prompt
  3. __GIT_PROMPT_DIR="${0:A:h}"
  4. ## Hook function definitions
  5. function chpwd_update_git_vars() {
  6. update_current_git_vars
  7. }
  8. function preexec_update_git_vars() {
  9. case "$2" in
  10. git*|hub*|gh*|stg*)
  11. __EXECUTED_GIT_COMMAND=1
  12. ;;
  13. esac
  14. }
  15. function precmd_update_git_vars() {
  16. if [ -n "$__EXECUTED_GIT_COMMAND" ] || [ ! -n "$ZSH_THEME_GIT_PROMPT_CACHE" ]; then
  17. update_current_git_vars
  18. unset __EXECUTED_GIT_COMMAND
  19. fi
  20. }
  21. chpwd_functions+=(chpwd_update_git_vars)
  22. precmd_functions+=(precmd_update_git_vars)
  23. preexec_functions+=(preexec_update_git_vars)
  24. ## Function definitions
  25. function update_current_git_vars() {
  26. unset __CURRENT_GIT_STATUS
  27. local gitstatus="$__GIT_PROMPT_DIR/gitstatus.py"
  28. _GIT_STATUS=$(python ${gitstatus} 2>/dev/null)
  29. __CURRENT_GIT_STATUS=("${(@s: :)_GIT_STATUS}")
  30. GIT_BRANCH=$__CURRENT_GIT_STATUS[1]
  31. GIT_AHEAD=$__CURRENT_GIT_STATUS[2]
  32. GIT_BEHIND=$__CURRENT_GIT_STATUS[3]
  33. GIT_STAGED=$__CURRENT_GIT_STATUS[4]
  34. GIT_CONFLICTS=$__CURRENT_GIT_STATUS[5]
  35. GIT_CHANGED=$__CURRENT_GIT_STATUS[6]
  36. GIT_UNTRACKED=$__CURRENT_GIT_STATUS[7]
  37. }
  38. git_super_status() {
  39. precmd_update_git_vars
  40. if [ -n "$__CURRENT_GIT_STATUS" ]; then
  41. STATUS="$ZSH_THEME_GIT_PROMPT_PREFIX$ZSH_THEME_GIT_PROMPT_BRANCH$GIT_BRANCH%{${reset_color}%}"
  42. if [ "$GIT_BEHIND" -ne "0" ]; then
  43. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_BEHIND$GIT_BEHIND%{${reset_color}%}"
  44. fi
  45. if [ "$GIT_AHEAD" -ne "0" ]; then
  46. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_AHEAD$GIT_AHEAD%{${reset_color}%}"
  47. fi
  48. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_SEPARATOR"
  49. if [ "$GIT_STAGED" -ne "0" ]; then
  50. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STAGED$GIT_STAGED%{${reset_color}%}"
  51. fi
  52. if [ "$GIT_CONFLICTS" -ne "0" ]; then
  53. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CONFLICTS$GIT_CONFLICTS%{${reset_color}%}"
  54. fi
  55. if [ "$GIT_CHANGED" -ne "0" ]; then
  56. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CHANGED$GIT_CHANGED%{${reset_color}%}"
  57. fi
  58. if [ "$GIT_UNTRACKED" -ne "0" ]; then
  59. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNTRACKED%{${reset_color}%}"
  60. fi
  61. if [ "$GIT_CHANGED" -eq "0" ] && [ "$GIT_CONFLICTS" -eq "0" ] && [ "$GIT_STAGED" -eq "0" ] && [ "$GIT_UNTRACKED" -eq "0" ]; then
  62. STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_CLEAN"
  63. fi
  64. STATUS="$STATUS%{${reset_color}%}$ZSH_THEME_GIT_PROMPT_SUFFIX"
  65. echo "$STATUS"
  66. fi
  67. }
  68. # Default values for the appearance of the prompt.
  69. ZSH_THEME_GIT_PROMPT_PREFIX="("
  70. ZSH_THEME_GIT_PROMPT_SUFFIX=")"
  71. ZSH_THEME_GIT_PROMPT_SEPARATOR="|"
  72. ZSH_THEME_GIT_PROMPT_BRANCH="%{$fg_bold[magenta]%}"
  73. ZSH_THEME_GIT_PROMPT_STAGED="%{$fg[red]%}%{●%G%}"
  74. ZSH_THEME_GIT_PROMPT_CONFLICTS="%{$fg[red]%}%{✖%G%}"
  75. ZSH_THEME_GIT_PROMPT_CHANGED="%{$fg[blue]%}%{✚%G%}"
  76. ZSH_THEME_GIT_PROMPT_BEHIND="%{↓%G%}"
  77. ZSH_THEME_GIT_PROMPT_AHEAD="%{↑%G%}"
  78. ZSH_THEME_GIT_PROMPT_UNTRACKED="%{…%G%}"
  79. ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔%G%}"
  80. # Set the prompt.
  81. RPROMPT='$(git_super_status)'