svn-fast-info.plugin.zsh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # vim:ft=zsh ts=2 sw=2 sts=2 et
  2. #
  3. # Faster alternative to the current SVN plugin implementation.
  4. #
  5. # Works with svn 1.6, 1.7, 1.8.
  6. # Use `svn_prompt_info` method to enquire the svn data.
  7. # It's faster because his efficient use of svn (single svn call) which saves a lot on a huge codebase
  8. # It displays the current status of the local files (added, deleted, modified, replaced, or else...)
  9. #
  10. # Use as a drop-in replacement of the svn plugin not as complementary plugin
  11. function svn_prompt_info() {
  12. local info
  13. info=$(svn info 2>&1) || return 1; # capture stdout and stderr
  14. local repo_need_upgrade=$(svn_repo_need_upgrade $info)
  15. if [[ -n $repo_need_upgrade ]]; then
  16. printf '%s%s%s%s%s%s%s\n' \
  17. $ZSH_PROMPT_BASE_COLOR \
  18. $ZSH_THEME_SVN_PROMPT_PREFIX \
  19. $ZSH_PROMPT_BASE_COLOR \
  20. $repo_need_upgrade \
  21. $ZSH_PROMPT_BASE_COLOR \
  22. $ZSH_THEME_SVN_PROMPT_SUFFIX \
  23. $ZSH_PROMPT_BASE_COLOR
  24. else
  25. printf '%s%s%s %s%s:%s%s%s%s%s' \
  26. $ZSH_PROMPT_BASE_COLOR \
  27. $ZSH_THEME_SVN_PROMPT_PREFIX \
  28. \
  29. "$(svn_status_info $info)" \
  30. $ZSH_PROMPT_BASE_COLOR \
  31. \
  32. $ZSH_THEME_BRANCH_NAME_COLOR \
  33. $(svn_current_branch_name $info) \
  34. $ZSH_PROMPT_BASE_COLOR \
  35. \
  36. $(svn_current_revision $info) \
  37. $ZSH_PROMPT_BASE_COLOR \
  38. \
  39. $ZSH_THEME_SVN_PROMPT_SUFFIX \
  40. $ZSH_PROMPT_BASE_COLOR
  41. fi
  42. }
  43. function svn_repo_need_upgrade() {
  44. grep -q "E155036" <<< ${1:-$(svn info 2> /dev/null)} && \
  45. echo "E155036: upgrade repo with svn upgrade"
  46. }
  47. function svn_current_branch_name() {
  48. grep '^URL:' <<< "${1:-$(svn info 2> /dev/null)}" | egrep -o '(tags|branches)/[^/]+|trunk'
  49. }
  50. function svn_repo_root_name() {
  51. grep '^Repository\ Root:' <<< "${1:-$(svn info 2> /dev/null)}" | sed 's#.*/##'
  52. }
  53. function svn_current_revision() {
  54. echo "${1:-$(svn info 2> /dev/null)}" | sed -n 's/Revision: //p'
  55. }
  56. function svn_status_info() {
  57. local svn_status_string="$ZSH_THEME_SVN_PROMPT_CLEAN"
  58. local svn_status="$(svn status 2> /dev/null)";
  59. if command grep -E '^\s*A' &> /dev/null <<< $svn_status; then svn_status_string="$svn_status_string ${ZSH_THEME_SVN_PROMPT_ADDITIONS:-+}"; fi
  60. if command grep -E '^\s*D' &> /dev/null <<< $svn_status; then svn_status_string="$svn_status_string ${ZSH_THEME_SVN_PROMPT_DELETIONS:-✖}"; fi
  61. if command grep -E '^\s*M' &> /dev/null <<< $svn_status; then svn_status_string="$svn_status_string ${ZSH_THEME_SVN_PROMPT_MODIFICATIONS:-✎}"; fi
  62. if command grep -E '^\s*[R~]' &> /dev/null <<< $svn_status; then svn_status_string="$svn_status_string ${ZSH_THEME_SVN_PROMPT_REPLACEMENTS:-∿}"; fi
  63. if command grep -E '^\s*\?' &> /dev/null <<< $svn_status; then svn_status_string="$svn_status_string ${ZSH_THEME_SVN_PROMPT_UNTRACKED:-?}"; fi
  64. if command grep -E '^\s*[CI!L]' &> /dev/null <<< $svn_status; then svn_status_string="$svn_status_string ${ZSH_THEME_SVN_PROMPT_DIRTY:-'!'}"; fi
  65. echo $svn_status_string
  66. }