Bash prompt with Git branch and status
Tuesday, April 13, 2010 by alexandrulBased on Gunnar Wolf’s .bashrc, I have customized my .bash_profile to show exit status, bare repositories, and outside work tree cases:



#
# term colors
txtrst="�33[0m" # reset
# regular colors
txtK="�33[0;30m" # black
txtR="�33[0;31m" # red
txtG="�33[0;32m" # green
txtY="�33[0;33m" # yellow
txtB="�33[0;34m" # blue
txtM="�33[0;35m" # magenta
txtC="�33[0;36m" # cyan
txtW="�33[0;37m" # white
# emphasized colors
emK="�33[1;30m"
emR="�33[1;31m"
emG="�33[1;32m"
emY="�33[1;33m"
emB="�33[1;34m"
emM="�33[1;35m"
emC="�33[1;36m"
emW="�33[1;37m"
# background colors
bgK="�33[40m"
bgR="�33[41m"
bgG="�33[42m"
bgY="�33[43m"
bgB="�33[44m"
bgM="�33[45m"
bgC="�33[46m"
bgW="�33[47m"
#
# aliases
alias less='less -r'
# --show-control-chars: help showing Korean or accented characters
alias ls='ls -F --color --show-control-chars'
alias ll='ls -al'
#
# branch info
parse_git_branch() {
branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/1/'`;
# ? valid branch
if [ ! -z "$branch" ];
then
# ? bare repo
if [ x`git config core.bare`x == "xtruex" ];
then
branch="${branch}${txtrst} (bare)";
else
gitstatus=`git status 2>&1`;
# ? outside work tree
if echo "${gitstatus}" | grep -i 'must be run in a work tree' &> /dev/null;
then
branch="${branch}${txtrst} (outside work tree)";
# ? modifications
elif ! echo "${gitstatus}" |grep 'nothing to commit .working directory clean' 2>&1 > /dev/null;
then
branch="${branch}${emY}*";
new=$(( `git ls-files -o --exclude-standard|wc -l` ));
del=$(( `git ls-files -d --exclude-standard|wc -l` ));
mod=$(( `git ls-files -m --exclude-standard|wc -l` - ${del} ));
#let "mod = ${mod} - ${del}"
if [ $del != 0 ]; then branch="${branch}${emR} ${del}d"; fi;
if [ $mod != 0 ]; then branch="${branch}${emY} ${mod}m"; fi;
if [ $new != 0 ]; then branch="${branch}${emC} ${new}n"; fi;
fi;
fi;
fi;
echo -ne "${branch}";
}
#
# exit status
PROMPT_COMMAND='RET=$?;';
parse_return_value() {
ret_value="${txtrst}$RET";
if [[ $RET = 0 ]];
then
echo -ne "${ret_value}${txtG} ;)";
else
echo -ne "${ret_value}${emR} :(";
fi;
}
#
# custom prompt
# PS1='[�33]0;$MSYSTEM:w�07
# �33[32m]u@h [�33[33mw $(parse_git_branch)�33[0m]
# $ '
TITLE_BAR="[�33]0;w - u@h using $MSYSTEM�07]";
PS1="$TITLE_BARn $(parse_return_value)${txtY} w${emG} $(parse_git_branch)${txtrst}n$ ";
You can download the archived code here: bash_profile.zip