svn.plugin.zsh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # vim:ft=zsh ts=2 sw=2 sts=2
  2. #
  3. function svn_prompt_info() {
  4. if in_svn; then
  5. if [ "x$SVN_SHOW_BRANCH" = "xtrue" ]; then
  6. unset SVN_SHOW_BRANCH
  7. _DISPLAY=$(svn_get_branch_name)
  8. else
  9. _DISPLAY=$(svn_get_repo_name)
  10. fi
  11. echo "$ZSH_PROMPT_BASE_COLOR$ZSH_THEME_SVN_PROMPT_PREFIX\
  12. $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"
  13. unset _DISPLAY
  14. fi
  15. }
  16. function in_svn() {
  17. if $(svn info >/dev/null 2>&1); then
  18. return 0
  19. fi
  20. return 1
  21. }
  22. function svn_get_repo_name() {
  23. if in_svn; then
  24. svn info | sed -n 's/Repository\ Root:\ .*\///p' | read SVN_ROOT
  25. svn info | sed -n "s/URL:\ .*$SVN_ROOT\///p"
  26. fi
  27. }
  28. function svn_get_branch_name() {
  29. _DISPLAY=$(
  30. svn info 2> /dev/null | \
  31. awk -F/ \
  32. '/^URL:/ { \
  33. for (i=0; i<=NF; i++) { \
  34. if ($i == "branches" || $i == "tags" ) { \
  35. print $(i+1); \
  36. break;\
  37. }; \
  38. if ($i == "trunk") { print $i; break; } \
  39. } \
  40. }'
  41. )
  42. if [ "x$_DISPLAY" = "x" ]; then
  43. svn_get_repo_name
  44. else
  45. echo $_DISPLAY
  46. fi
  47. unset _DISPLAY
  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. root=`svn info 2> /dev/null | sed -n 's/^Working Copy Root Path: //p'`
  57. if $(svn status $root 2> /dev/null | 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. root=$PWD
  72. if $(svn status $root 2> /dev/null | 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. }