Monday, April 14, 2008

A little bash script hacking

Puttering around the blog-o-tubes, I discovered an awesome little post concerning git and your command line. The problem with the script is its too limiting. I use both svn and git. So enter my little hack.

In ~/.bashrc


function parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(git::\1)/'
}
function parse_svn_branch() {
parse_svn_url | sed -e 's#^'"$(parse_svn_repository_root)"'##g' | awk -F / '{print "(svn::"$1 "/" $2 ")"}'
}
function 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 '
}
function parse_scm_branch() {
local value=$(parse_git_branch)
if [ -z "$value" ]; then
local value=$(parse_svn_branch)
fi
echo "$value"
}

export PS1="\[\033[00m\]\u@\h\[\033[01;34m\] \w \[\033[31m\]\$(parse_scm_branch) \[\033[00m\]$\[\033[00m\] "



For git directories

jeremyf@reclusive-geek ~/Documents/Repositories/git/ccs_portal (git::master) $


For svn directories

jeremyf@reclusive-geek ~/Documents/Repositories/svn/ccs_main/app/controllers (svn::ccs_main/trunk) $


If anyone knows how to tighten this script up, let me know.

1 comment:

Haskell the Dog said...

Real nice! Thanks. BTW, best blog title evah.