Git the most out of your bash prompt

I was looking for a way to add as much relevant info about my Liferay git repository to my prompt as would not slow it down to much, cause I hated having to constantly do git branch/status to find branch/state all the time.

So after reading a couple nice posts about it,

http://henrik.nyh.se/2008/12/git-dirty-prompt

http://plasti.cx/2009/10/23/vebose-git-dirty-prompt

I took the best of both of those (performance wise and output wise), and since I use git-svn added to mine the requirement of showing the svn revision my branch is currently synced with.

Here is the bash code I added to by ~/.bashrc :

function parse_git_dirty {
status=`git status 2> /dev/null`
        dirty=`    echo -n "${status}" 2> /dev/null | grep -q "Changed but not updated" 2> /dev/null; echo "$?"`
        untracked=`echo -n "${status}" 2> /dev/null | grep -q "Untracked files" 2> /dev/null; echo "$?"`
        ahead=`    echo -n "${status}" 2> /dev/null | grep -q "Your branch is ahead of" 2> /dev/null; echo "$?"`
        newfile=`  echo -n "${status}" 2> /dev/null | grep -q "new file:" 2> /dev/null; echo "$?"`
        renamed=`  echo -n "${status}" 2> /dev/null | grep -q "renamed:" 2> /dev/null; echo "$?"`
        bits=''
        if [ "${dirty}" == "0" ]; then
                bits="${bits}☭"
        fi
        if [ "${untracked}" == "0" ]; then
                bits="${bits}?"
        fi
        if [ "${newfile}" == "0" ]; then
                bits="${bits}*"
        fi
        if [ "${ahead}" == "0" ]; then
                bits="${bits}+"
        fi
        if [ "${renamed}" == "0" ]; then
                bits="${bits}>"
        fi
        echo "${bits}"
}

function parse_git_svn_revision {
        ref1=$(__git_ps1 | sed -e "s/ (\(.*\))/(git: \1$(parse_git_dirty))/")
        #ref1=$(parse_git_branch)

        if [ "x$ref1" != "x"  ]; then
                ref2=$(git svn info | grep Revision)
                echo " ${ref1} (svn: r"${ref2#Revision: }") "
        fi
}

PS1='\[\033[0;37m\][\[\033[0;31m\]\u@\h\[\033[0;33m\]`parse_git_svn_revision`\[\033[0;32m\]\W\[\033[0;37m\]]\$ '

 
This is what it looks like:

the output of a bash session using the new prompt

 

As per Plasticx's blog the dirty flags are as such:

  • ‘☭’ – files have been modified
  • ‘?’ – there are untracted files in the project
  • ‘*’ – a new file has been add to the project but not committed
  • ‘+’ – the local project is ahead of the remote
  • ‘>’ – file has been moved or renamed

 

Much better!

Blogs
Ray, I'm getting this "__git_ps1: command not found"...

BTW this is very cool!!!
Oh, sorry for that you need git 1.6.

I installed mine from the source tarball. Was a simple build and install. See the README file for instructions. I think it was only two commands.