Skip to content

Instantly share code, notes, and snippets.

@lwr
Last active August 29, 2015 14:00
Show Gist options
  • Save lwr/11480177 to your computer and use it in GitHub Desktop.
Save lwr/11480177 to your computer and use it in GitHub Desktop.
_vcs_info() {
# parse git branch
git name-rev HEAD 2> /dev/null | sed 's#HEAD\ \(.*\)#git:\1)#' | grep git && return
# parse svn branch
# http://svn.host/xxx
REPO_ROOT=`svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p'`
[ -z "$REPO_ROOT" ] && return
# http://svn.host/xxx -> 空串
# http://svn.host/xxx/trunk -> /trunk
# http://svn.host/xxx/branches/yyy/zzz -> /branches/yyy/zzz
# http://svn.host/xxx/tags/yyy/zzz -> /xxx/tags/yyy/zzz
WORK_PATH=`svn info 2>/dev/null | sed -ne 's#^URL: ##p' | sed -e 's#^'"$REPO_ROOT"'##g'`
if [ -z "$WORK_PATH" ] ; then
BRANCH=/
elif echo "$WORK_PATH" | grep -q /trunk ; then
BRANCH=`echo "$WORK_PATH" | egrep -o '/trunk([^/]*)' | sed -ne 's#/##p'`
elif echo "$WORK_PATH" | grep -q /branches/ ; then
BRANCH=`echo "$WORK_PATH" | egrep -o '/branches/([^/]*)' | sed -ne 's#^/branches/##p'`
elif echo "$WORK_PATH" | grep -q /tags/ ; then
BRANCH=`echo "$WORK_PATH" | egrep -o '/tags/([^/]*)' | sed -ne 's#^/tags/##p'`
else
BRANCH=`echo "$WORK_PATH" | awk -F"/" '{print $1?$1:$2}'`
fi
echo "$BRANCH" | awk '{print "svn:"$1")" }'
}
# Rainbow style prompt (first line) - user@host path ]
PS1=""
PS1="$PS1\[$(tput bold)\]"
PS1="$PS1\[$(tput setaf 3)\]\u"
PS1="$PS1\[$(tput setaf 2)\]@"
PS1="$PS1\[$(tput setaf 4)\]\h"
PS1="$PS1\[$(tput setaf 5)\] \w"
PS1="$PS1\[$(tput setaf 1)\] ]"
# (second line) - $ _
# - svn:trunk)$ _
# - git:master)$ _
PS1="$PS1\n"
PS1="$PS1\[$(tput bold)\]\[$(tput setaf 3)\]↪ " #
PS1="$PS1\[$(tput sgr0)\]\[$(tput setaf 2)\]\$(_vcs_info)"
PS1="$PS1\[$(tput bold)\]\[$(tput setaf 7)\]\$"
PS1="$PS1\[$(tput sgr0)\] "
export PS1
export -f _vcs_info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment