Skip to content

Instantly share code, notes, and snippets.

@sdthornton
Last active August 29, 2015 13:57
Show Gist options
  • Save sdthornton/9536133 to your computer and use it in GitHub Desktop.
Save sdthornton/9536133 to your computer and use it in GitHub Desktop.
Git Prompt
# For better git prompts
c_cyan=`tput setaf 6`
c_red=`tput setaf 1`
c_green=`tput sgr0 ; tput setaf 2`
c_sgr0=`tput sgr0`
branch_color ()
{
if git rev-parse --git-dir >/dev/null 2>&1
then
git_status="$(git status 2> /dev/null)"
color=""
if [[ ${git_status} =~ "working directory clean" ]]; then
color="${c_green}"
else
color=${c_red}
fi
else
return 0
fi
echo -ne $color
}
parse_git_bullet ()
{
if git rev-parse --git-dir >/dev/null 2>&1
then
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo "•"
fi
else
return 0
fi
}
parse_git_branch ()
{
if git rev-parse --git-dir >/dev/null 2>&1
then
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " (${branch} "
fi
else
return 0
fi
}
parse_git_remote ()
{
if git rev-parse --git-dir >/dev/null 2>&1
then
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="↑"
elif [ ${BASH_REMATCH[1]} == "behind" ]]; then
remote="↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo "${remote})"
fi
else
return 0
fi
}
function prompt
{
local WHITE="\[\033[1;37m\]"
local PURPLE="\[\033[0;35m\]"
local GRAY="\[\033[0;37m\]"
local DARK_GRAY="\[\033[1;30m\]"
local BLUE="\[\033[0;34m\]"
local bul="\342\200\242"
export PS1="${PURPLE}\w"'\[${c_sgr0}\]$(parse_git_branch)\[$(branch_color)\]$(parse_git_bullet)\[${c_sgr0}\]$(parse_git_remote)'"${GRAY}$ "
}
prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment