I recently had to split an SVN repository of mine into a TRUNK and a TAGged branch. Nothing fancy: a tag to identify the REV1.x and the TRUNK to keep developing.
From time to time I have to svn switch, jumping from the tagged branch to the trunk (testing, merging, usual staff…). And you know what? I’m keeping committing on the tagged branch! And that is bad (more on this with an appropriate future post. I promise).
How beautiful could be having a bash prompt that will costantly display the branch I’m in and the svn repository revision of the current directory?
So, here we are:
append this code to your ~/.bashrc, logout/login and enter a svn managed directory (beware: I’ve tested it only on my Ubuntu box… the key here is to use the PROMPT_COMMAND env variabile, passing it the function that does the “sniffing”. The PROMPT_COMMAND command will be executed every time, just before displaying the shell prompt).
PROMPT_COMMAND=prompt_command
prompt_command() {
if [[ -d ".svn" ]] ; then
local info rev url ver
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}')
echo ${url} | grep -q "/trunk\b"
if [[ $? -eq 0 ]] ; then
ver=trunk
else
echo ${url} | grep -q "/tags\b"
if [[ $? -eq 0 ]] ; then
ver=tag-$(echo ${url} | grep -o "/tags.*" | awk -F/ '{print $3}')
fi
fi
echo -e "\e[00;33m[svn:r${rev}@${ver}]\e[00m"
fi
}
The svn information will be displayed in yellow (is the 33 in the final echo line). Change it at your liking.
UPDATE: Giovanni Bajo gives the hints for the "-q" switch and the LC_MESSAGES trick to avoid locale inconsistences.
Giovanni has gone a step forward, giving us the code for a git enhanced prompt too.
Here it is:
local gitout
gitout=$(git branch -v --abbrev --no-color 2>/dev/null)
if [[ $? -eq 0 ]]; then
local full branch sha1
full=$(echo "${gitout}" | grep '^*')
branch=$(echo "${full}" | awk '/^* \w+ \w+/{print $2}')
sha1=$(echo "${full}" | awk '/^* \w+ \w+/{print $3}')
echo -e "\e[00;33m[git:${sha1}@${branch}]\e[00m"
fi
Thanks to Uqbar on Freenode IRC for the awk hints!
example:
claudioc@enebish:~/Sites$ claudioc@enebish:~/Sites$ cd scrive2/ [svn:r452@trunk] claudioc@enebish:~/Sites/scrive2$ svn switch $myrepos/tags/R2.4 [svn:r431@tag-R2.4] claudioc@enebish:~/Sites/scrive2$
RSS






