In the past few weeks I managed to add changes to a few of my projects in the wrong branches, which led to the merges of unwanted code.
So, I decided that I have to do something about this, something that will prevent me from working on the wrong branch and blindly commit to it :)

I decided that it would be very neat if I have the branch name in my cmd prompt. So I wrote this little script:

#!/bin/bash 

function change_ps {
        PS1='($(git branch 2>/dev/null|awk "/^*/{print \$2}")):\w\$ '
        export PSCHANGED=1
}

function revert_ps {
        PS1="\u@\h:\w\$ "
        export PSCHANGED=0
}

function branch {
        cd $1
        cwd=$(pwd)
        if [[ "$cwd" =~ \/home\/hackman\/Projects ]] && [ -d $cwd/.git ]; then
                if [ "$PSCHANGED" == 1 ]; then
                        if [ "$cwd" != "$OLDPWD" ]; then
                                change_ps
                        fi
                else
                        change_ps
                fi
        else
                revert_ps
        fi
}


After I wrote it, I found another project that does a lot more (git-prompt). But I think that git-prompt is a big overkill for me. The main difference is that my script does a lot less computations for the task of changing the branch.

I have published this code at github


Comments are closed.

Posted by HackMan
Dated: 22nd April 2012
Filled Under: Technology