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$
9 Responses to “SVN customized bash prompt (and Git!)”
SVN customized bash prompt http://bit.ly/cDSq9l /by @caludio
This comment was originally posted on Twitter
Splendido e già bell’e che messo ovunque, grazie!
This comment was originally posted on FriendFeed
veramente ottimo, ora lo estendo anche per git e hg… due consigli: "grep -q" per evitare la ridirezione, e "LC_MESSAGES=C svn info" per diventare indipendente dal locale/traduzione
This comment was originally posted on FriendFeed
grande Giovanni, grazie.
This comment was originally posted on FriendFeed
http://pastebin.com/m3cb75b7d supporto git (e ho messo echo -n perché a me piace di più, YMMV)
This comment was originally posted on FriendFeed
Giovanni, aggiorno il post. Ho messo il tuo indirizzo Facebook… se vuoi dammene un altro :)
This comment was originally posted on FriendFeed
LOL, dici che è arrivato il momento di aprire un blog o almeno fare uno sputo di sito web? :) Metti un link a http://www.develer.com che è meglio, grazie :)
This comment was originally posted on FriendFeed
Questo mi fa anche capire che non c’è proprio più nessuno che usa CVS (manco io, in effetti).
This comment was originally posted on FriendFeed
Svn Customized Bash Prompt (and Git) – http://goo.gl/h8lm
This comment was originally posted on Twitter
Additional comments powered by BackType