A moonbase.git prompt

Dennis Veatch dennisveatch at bellsouth.net
Wed Jan 6 22:56:18 CET 2010


Florin made mention today in IRC about a "proper git prompt" and not realizing 
he was in his dev branch. That got me to thinking it would be nice if your 
bash prompt would change to reflect where you were at in your local 
repository. So instead of reinventing the wheel, I looked around and found a 
guy that has what I think a suitable answer;

http://aaroncrane.co.uk/2009/03/git_branch_prompt/

In a nutshell, put this in your .bash_profile;

function find_git_branch {
    local dir=. head
    until [ "$dir" -ef / ]; do
        if [ -f "$dir/.git/HEAD" ]; then
            head=$(< "$dir/.git/HEAD")
            if [[ $head == ref:\ refs/heads/* ]]; then
                git_branch=" ${head#*/*/}"
            elif [[ $head != '' ]]; then
                git_branch=' (detached)'
            else
                git_branch=' (unknown)'
            fi
            return
        fi
        dir="../$dir"
    done
    git_branch=''
}

green=$'\e[1;32m'
magenta=$'\e[1;35m'
normal_colours=$'\e[m'

PS1="\[$green\]\u@\h:
\w\[$magenta\]\$git_branch\[$green\]\\$\[$normal_colours\] "

PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"


It works rather well afaict at this point. The nice thing about this is it 
does not depend on the name of your repository because it looks at the 
contents of .git/HEAD.

Perhaps with a little tweaking from others we might want to add that to our 
bash module.

Dennis


More information about the Lunar-dev mailing list