Tag Archives: subversion

Easen use of svn switch with bash prompt

The issue

svn switch can safe a lot of time when working in different branches as not all source code has to be checked out again.
But well you know it: How fast does it happen that a file gets committed in the wrong branch?
To prevent you from always have to call svn info before committing, here comes the script which includes the current branch in the bash prompt so you may never commit code in the wrong branch again ;).

Get the script

#!/usr/bin/perl

use Cwd;
my $dir = getcwd;
unless( -r $dir."/.svn" )
{
  exit;
}
$ret="svn";
$dir=`svn info | grep URL`;
$dir =~ s/n//;
if($dir =~ m//trunk/)
{
  $ret="trunk";
}
if($dir =~ m/branches/|tags//)
{
  $dir=$dir."/";
  $dir =~ s/.*(b|t)(ranches|ags)/(.*?)/.*/$1:$3/;
  $ret=$dir;
}
print " [$ret]";

What needs to be installed

Subversion of course and perl to run the script, which is usually installed in most distributions.

How to use it

Let’s store the script to svn-ps1.pl somewhere let’s say $HOME/bin.
Now open the bashrc file with vi ~/.bashrc and adjust the export of the PS1 variable:

PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '

to the following:

PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$(~/bin/svn-ps1.pl)$ '

Now simply change to some source code checked out with subversion and you will see that the prompt will look like this:
user@laptop:~/src/svn/some-code [b:branch-name]$

Cheers!