svn.plugin.zsh 2.2 KB

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