Jun 29, 2011

Hg or git branch in bash prompt

in ~/.bashrc (Ubuntu 12.04)
hg_branch() {
    hg branch 2> /dev/null | \
        awk -v "clrp=$txtpur" -v "clrr=$txtrst" \
            '{ print clrp "@hg:" $1 clrr}'
}
hg_dirty() {
    [ $(hg status 2> /dev/null | wc -l) != 0 ] && \
        echo -e "${txtred}*${txtrst}"
}

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\W$(hg_branch)$(hg_dirty)\$ '
fi
then your prompt will looks like:
someproject$ vi /home/alexey/.bashrc  # make changes above
someproject$ source /home/alexey/.bashrc 
someproject@hg:liveserver$  # liveserver is current brunch
someproject@hg:liveserver$ hg branch
liveserver
someproject@hg:liveserver$ touch blabla
someproject@hg:liveserver*$  # asterisk indicates that VCS has changes
someproject@hg:liveserver*$ hg st
? blabla
Also you can do the same for git:
git_branch() {
    git branch --no-color 2> /dev/null | grep "*" |\
        awk -v "clrp=$txtpur" -v "clrr=$txtrst" \
            '{ print clrp "@git:" $2 clrr}'
}
git_dirty() {
    [ $(git status --short 2> /dev/null | wc -l) != 0 ] && \
        echo -e "${txtred}*${txtrst}"
}

No comments: