Skip to content

Instantly share code, notes, and snippets.

@adyp
Created August 7, 2014 16:21
Show Gist options
  • Save adyp/a2f7e97b79245bbb1109 to your computer and use it in GitHub Desktop.
Save adyp/a2f7e97b79245bbb1109 to your computer and use it in GitHub Desktop.
PROMPT_COMMAND for SVN repo information (from mixed sources and additional tweaking)
# Include SVN info in prompt, if possible
PROMPT_COMMAND=svn_prompt_cmd
svn_prompt_cmd() {
# The various escape codes that we can use to color our prompt.
RED="\e[0;31m"
GREEN="\e[0;32m"
BROWN="\e[0;33m"
BLUE="\e[0;34m"
PURPLE="\e[0;35m"
CYAN="\e[0;36m"
LIGHT_BLUE="\e[1;34m"
LIGHT_RED="\e[1;31m"
LIGHT_GREEN="\e[1;32m"
WHITE="\e[1;37m"
LIGHT_GRAY="\e[0;37m"
YELLOW="\e[1;33m"
COLOR_NONE="\e[00m"
if [[ -d ".svn" ]] ; then
local info rev url ver stat rrev
info=$(LC_MESSAGES=C svn info 2>/dev/null)
rev=$(echo "${info}" | awk '/^Revision: [0-9]+/{print $2}')
url=$(echo "${info}" | awk '/^URL: .*/{print $2}')
case $(echo ${url} | egrep -o "/(trunk|branches|tags)\>" | tr -d "/") in
trunk) ver=${CYAN}trunk
;;
tags) ver=${YELLOW}tag:$(echo ${url} | grep -o "/tags.*" | awk -F/ '{print $3}')
;;
branches) ver=${GREEN}branch:$(echo ${url} | grep -o "/branches.*" | awk -F/ '{print $3}')
;;
esac
stat=$(LC_MESSAGES=C svn status -u 2>/dev/null | tail -3) # SVN remote update state
rrev=$(echo "$stat" | grep "^Status against revision" | grep -o "[0-9]\+$") # SVN remote revision head
# Establish color of rev field based on local/remote freshness of the checkout:
# light red: there are changes in the remote repo against local checkout
# light blue: the local checkout is behind the remote repo
# purple dot: there are uncommitted changes in local checkout
[ $(echo "$stat" | wc -l) -gt 1 ] && rev="${LIGHT_RED}$rev" || \
[ "$rrev" != "$rev" ] && rev="${LIGHT_BLUE}$rev"
[ $(svn status 2>/dev/null | head -1 | wc -l) -gt 0 ] && rev="${rev}${PURPLE}*"
echo -e "[${BROWN}svn:r${rev}${COLOR_NONE} @ ${ver}${COLOR_NONE}]"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment