Personalize your bash with GIT/SVN and colors

If you work everyday on a Linux shell and need to manage projects on GIT or SVN code control systems, could be useful to get informations about them directly on your bash.

You can easily do that with some changes into /etc/bashrc file of your Linux.
Go to the end of that file and add the followings lines:

 parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(git::1)/'
}
parse_svn_branch() {
  parse_svn_url | sed -e 's#^'"$(parse_svn_repository_root)"'##g' | awk -F / '{print "(svn::"$1 "/" $2 ")"}'
}
parse_svn_url() {
  svn info 2>/dev/null | grep -e '^URL*' | sed -e 's#^URL: *(.*)#1#g '
}
parse_svn_repository_root() {
  svn info 2>/dev/null | grep -e '^Repository Root:*' | sed -e 's#^Repository Root: *(.*)#1/#g '
}

# vim:ts=4:sw=4
# Colors in Terminal
if [ $USER = root ]; then
        PS1='[33[1;31m][u@h W]$[33[0m] '
else
        #PS1='[33[01;32m]u@h[33[00m] [33[01;34m]W[33[00m][33[1;32m]$[33[m] '
        PS1="[33[01;32m]u@h[33[00m] [33[01;34m]W[33[00m][33[1;32m][33[31m]$(parse_git_branch)$(parse_svn_branch)[33[00m][33[1;32m]$[33[m] "

We have added some Bash functions to call git and svn commands and retrieve informations about your code control.
Then we override the PS1 variable, used by Bash program to personalize the prompt, adding colors (red for root user) and calling defined functions.
The result, when you enter into a repository folder, is the following (for a git project):

 mmornati@desktop raskiidoc(git::master)$

Indicating the repository type (git) and the name of the current branch (master).

Schermata 2013-11-11 alle 22.54.07 Schermata 2013-11-11 alle 22.54.38