Git/SVN Repository Information Displayed in the Console

Repository Information in Console

For someone who works a lot with Git repositories, it is interesting to see some basic information in the console while working on the repo. Stuff like which branch or tag is currently active. Sure, there are commands to show that kind of information, but after a while, it becomes tedious to have to put those in.

So, wouldn’t it be nice if the actual command line has this information? Yes! It can be done, at least in Bash, you just need to add a little bit of code to your bashrc file.

# GIT and SVN
parse_svn_repository_root() {
    svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p'
}

parse_svn_url() {
    svn info 2>/dev/null | sed -ne 's#^URL: ##p'
}

parse_git_branch () {
    git name-rev HEAD 2> /dev/null | sed 's#HEAD\ \(.*\)# (git » \1)#'
}

parse_svn_branch() {
    parse_svn_url | sed -e 's#^'"$(parse_svn_repository_root)"'##g' | awk '{print " (svn » "$1")" }'
}


# Add to command prompt
if [[ ${EUID} == 0 ]] ; then
    # root
    export PS1='\[\033[01;31m\]\u@\h\[\033[01;34m\] \W \$\[\033[31m\]$(parse_git_branch)$(parse_svn_branch)\[\033[01;34m\]\[\033[00m\] '
else
    # normal user
    export PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[31m\]$(parse_git_branch)$(parse_svn_branch)\[\033[01;34m\]\[\033[00m\] '
fi

With the next login, you’ll see the active branch and or tag information in your command line.

Leave a Reply

Your email address will not be published. Required fields are marked *