Skip to content

Instantly share code, notes, and snippets.

@jacquesbh
Created July 31, 2012 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacquesbh/3217912 to your computer and use it in GitHub Desktop.
Save jacquesbh/3217912 to your computer and use it in GitHub Desktop.
PS1 :)
# Prompt
# Get the branch name of the current directory
git_branch()
{
if git rev-parse --git-dir >/dev/null 2>&1 ; then
gitver=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
else
return 0
fi
if [[ $# == 1 ]]; then
echo -e "$gitver"
else
echo -e "($gitver)"
fi
}
# Set a color depending on the branch state:
# - green if the repository is up to date
# - yellow if there is some commits not pushed
# - red if there is changes to commit
git_branch_color()
{
if git rev-parse --git-dir >/dev/null 2>&1 ; then
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
color=""
if git diff --quiet 2>/dev/null >&2 ; then
branch="$(git_branch name)"
has_commit=`git rev-list origin/$branch..$branch`
if [ "$has_commit" != "" ] ; then
color="\033[01;33m" # some commits to push
else
color="\033[01;32m" # nothing to commit or push
fi
else
color="\033[01;31m" # changes to commit
fi
else
return 0
fi
echo -ne $color
}
export PS1='\[\033[01;32m\]\u@\H\[\033[01;36m\] \w `git_branch_color``git_branch`\[\033[01;36m\]\n\$ \[\033[00m\]'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment