svn.plugin.zsh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # vim:ft=zsh ts=2 sw=2 sts=2
  2. #
  3. function svn_prompt_info() {
  4. local _DISPLAY
  5. if in_svn; then
  6. if [ "x$SVN_SHOW_BRANCH" = "xtrue" ]; then
  7. unset SVN_SHOW_BRANCH
  8. _DISPLAY=$(svn_get_branch_name)
  9. else
  10. _DISPLAY=$(svn_get_repo_name)
  11. _DISPLAY=$(omz_urldecode "${_DISPLAY}")
  12. fi
  13. echo "$ZSH_PROMPT_BASE_COLOR$ZSH_THEME_SVN_PROMPT_PREFIX\
  14. $ZSH_THEME_REPO_NAME_COLOR$_DISPLAY$ZSH_PROMPT_BASE_COLOR$ZSH_THEME_SVN_PROMPT_SUFFIX$ZSH_PROMPT_BASE_COLOR$(svn_dirty)$(svn_dirty_pwd)$ZSH_PROMPT_BASE_COLOR"
  15. fi
  16. }
  17. function in_svn() {
  18. if $(svn info >/dev/null 2>&1); then
  19. return 0
  20. fi
  21. return 1
  22. }
  23. function svn_get_repo_name() {
  24. if in_svn; then
  25. svn info | sed -n 's/Repository\ Root:\ .*\///p' | read SVN_ROOT
  26. svn info | sed -n "s/URL:\ .*$SVN_ROOT\///p"
  27. fi
  28. }
  29. function svn_get_branch_name() {
  30. local _DISPLAY=$(
  31. svn info 2> /dev/null | \
  32. awk -F/ \
  33. '/^URL:/ { \
  34. for (i=0; i<=NF; i++) { \
  35. if ($i == "branches" || $i == "tags" ) { \
  36. print $(i+1); \
  37. break;\
  38. }; \
  39. if ($i == "trunk") { print $i; break; } \
  40. } \
  41. }'
  42. )
  43. if [ "x$_DISPLAY" = "x" ]; then
  44. svn_get_repo_name
  45. else
  46. echo $_DISPLAY
  47. fi
  48. }
  49. function svn_get_rev_nr() {
  50. if in_svn; then
  51. svn info 2> /dev/null | sed -n 's/Revision:\ //p'
  52. fi
  53. }
  54. function svn_dirty_choose() {
  55. if in_svn; then
  56. local root=`svn info 2> /dev/null | sed -n 's/^Working Copy Root Path: //p'`
  57. if $(svn status $root 2> /dev/null | command grep -Eq '^\s*[ACDIM!?L]'); then
  58. # Grep exits with 0 when "One or more lines were selected", return "dirty".
  59. echo $1
  60. else
  61. # Otherwise, no lines were found, or an error occurred. Return clean.
  62. echo $2
  63. fi
  64. fi
  65. }
  66. function svn_dirty() {
  67. svn_dirty_choose $ZSH_THEME_SVN_PROMPT_DIRTY $ZSH_THEME_SVN_PROMPT_CLEAN
  68. }
  69. function svn_dirty_choose_pwd () {
  70. if in_svn; then
  71. local root=$PWD
  72. if $(svn status $root 2> /dev/null | command grep -Eq '^\s*[ACDIM!?L]'); then
  73. # Grep exits with 0 when "One or more lines were selected", return "dirty".
  74. echo $1
  75. else
  76. # Otherwise, no lines were found, or an error occurred. Return clean.
  77. echo $2
  78. fi
  79. fi
  80. }
  81. function svn_dirty_pwd () {
  82. svn_dirty_choose_pwd $ZSH_THEME_SVN_PROMPT_DIRTY_PWD $ZSH_THEME_SVN_PROMPT_CLEAN_PWD
  83. }