Old but not bus­ted … – Die­ser Inhalt wur­de vor mehr als 13 Jah­ren publi­ziert. Die Kor­rekt­heit und Ver­füg­bar­keit von Links kön­nen lei­der nicht gewähr­leis­tet werden.

The fol­lo­wig post is not writ­ten by mys­elf. It’s a copy! – Today I found the artic­le in the Goog­le Cache and thought the­re are more peo­p­le (than me) inte­res­ted in the archi­ved text that was ori­gi­nal­ly published at a blog named M‑x Kel­sin.

Let’s start …

After rea­ding ano­ther cool blog post about put­ting your cur­rent git branch in your bash prompt I deci­ded I nee­ded to try this out. Once I got it working I added in color coding to see the sta­tus of the cur­rent check­out as well!

First off, you need bash-com­ple­ti­on and git instal­led on your ser­ver (bash-com­ple­ti­on and git-core on Debian/Ubuntu). Once instal­led you can enable bash com­ple­ti­on in the sys­tem wide bash file (/etc/bash.bashrc) or in your own ~/.bashrc by adding the­se lines (Cle­ar­ly if you are not on Debian/Ubuntu dou­ble check file paths):

# Completion
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

Once this is all set you should have a func­tion cal­led git_ps1 available. Try it out by just run­ning git_ps1 on your com­mand line from a git repo. You should get the branch name retur­ned insi­de parenthesis’s.

Now comes my varia­ti­ons on how to include this in your prompt. My enti­re ~/.bash_prompt file can be found on my git repo. I source this file into my ~/.bashrc. The two most inte­res­t­ing parts are the func­tion that deter­mi­nes the color of the branch based on git-sta­tus out­put and the func­tion that gets the branch name. Branch name is pret­ty simp­le. We check that the __git_ps1 func­tion is available and if it is, check that we’­re in a branch using it. If we are we echo the branch name. Pret­ty clean.

prompt_git_branch() {
    if type -p __git_ps1; then
        branch=$(__git_ps1 '%s')
        if [ -n "$branch" ]; then
            echo -e "$branch"
        fi
    fi
}

The next func­tion has to grep stuff out of git sta­tus to deter­mi­ne what sta­te the repo is in. If we are com­ple­te­ly up to date we use green. If I have local chan­ges it’s blue. If we have files in our index rea­dy to be com­mit­ted I use red. This is real­ly gre­at with my home direc­to­ry cau­se it helps remind me to add new dot­files that I don’t care about to .gitigno­re (or com­mit them if they should be public con­fig files).

prompt_git_branch_color() {
    if type -p __git_ps1; then
        branch=$(__git_ps1 '%s')
        if [ -n "$branch" ]; then
            status=$(git status 2> /dev/null)

            if $(echo $status | grep 'added to commit' &> /dev/null); then
                # If we have modified files but no index (blue)
                echo -e "\033[1;34m"
            else
                if $(echo $status | grep 'to be committed' &> /dev/null); then
                    # If we have files in index (red)
                    echo -e "\033[1;31m"
                else
                    # If we are completely clean (green)
                    echo -e "\033[1;32m"
                fi
            fi
        fi
    fi
}

It took some play­ing but I final­ly found the right final line to cor­rect­ly tell bash which cha­rac­ters in the prompt are visi­ble. If anyo­ne has a good way of making the­se func­tions smal­ler or fas­ter I’d love to hear it. I had some trou­ble making sure that the func­tions were always exe­cu­ted (not just on a new shell’s crea­ti­on, but on every dis­play of PS1). The speed is FINE on all of my com­pu­ters but more speed never hurts.