Here’s a snippet of bash script that I wrote to make my prompt tell me when I’m in a git repository and what branch I’m on.
And to my pleasant surprise, the PROMPT_COMMAND mechanism will respect color codes, so if you have your branch listings in git colorized, it will be reflected in your prompt.
function rfind
{
dir=$PWD
while [ ! -e $1 ]; do
if [ $PWD == "/" ]; then
command cd $dir
return 1
else
command cd ..
fi
done
rfdir=$PWD
command cd $dir
return 0
}
function git_dir
{
typeset str
rfdir=""
rfind '.git'
if [ $? -eq 0 ]; then
str="{`git branch | grep '*' | cut -d ' ' -f 2`}"
rfdir=`echo $rfdir | sed "s#$HOME#~#"`
fi
echo "$str"
}
function mkprompt
{
typeset branch
branch=`git_dir`
PS1="${branch}$ "
}
PROMPT_COMMAND=mkprompt
export PROMPT_COMMAND
Andrew Connell
Contact