git.zsh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. git_prompt_info () {
  2. local ref
  3. if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]
  4. then
  5. ref=$(command git symbolic-ref HEAD 2> /dev/null) || ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
  6. echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
  7. fi
  8. }
  9. git_remote_status () {
  10. local remote ahead behind git_remote_status git_remote_status_detailed
  11. remote=${$(command git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
  12. if [[ -n ${remote} ]]
  13. then
  14. ahead=$(command git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
  15. behind=$(command git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
  16. if [[ $ahead -eq 0 ]] && [[ $behind -eq 0 ]]
  17. then
  18. git_remote_status="$ZSH_THEME_GIT_PROMPT_EQUAL_REMOTE"
  19. elif [[ $ahead -gt 0 ]] && [[ $behind -eq 0 ]]
  20. then
  21. git_remote_status="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
  22. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}"
  23. elif [[ $behind -gt 0 ]] && [[ $ahead -eq 0 ]]
  24. then
  25. git_remote_status="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
  26. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  27. elif [[ $ahead -gt 0 ]] && [[ $behind -gt 0 ]]
  28. then
  29. git_remote_status="$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
  30. git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  31. fi
  32. if [[ -n $ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]
  33. then
  34. git_remote_status="$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX$remote$git_remote_status_detailed$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX"
  35. fi
  36. echo $git_remote_status
  37. fi
  38. }
  39. parse_git_dirty () {
  40. local STATUS=''
  41. local -a FLAGS
  42. FLAGS=('--porcelain')
  43. if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]
  44. then
  45. if [[ $POST_1_7_2_GIT -gt 0 ]]
  46. then
  47. FLAGS+='--ignore-submodules=dirty'
  48. fi
  49. if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]
  50. then
  51. FLAGS+='--untracked-files=no'
  52. fi
  53. STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
  54. fi
  55. if [[ -n $STATUS ]]
  56. then
  57. echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  58. else
  59. echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  60. fi
  61. }