Skip to content

Instantly share code, notes, and snippets.

@wballard
Created January 27, 2014 23:51
Show Gist options
  • Save wballard/8659809 to your computer and use it in GitHub Desktop.
Save wballard/8659809 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
RED="\[$(tput setaf 1)\]"
YELLOW="\[$(tput setaf 3)\]"
GREEN="\[$(tput setaf 2)\]"
BLUE="\[$(tput setaf 4)\]"
WHITE="\[$(tput setaf 7)\]"
BOLD="\[$(tput bold)\]"
NORMAL="\[$(tput sgr0)\]"
function parse_git_branch {
git rev-parse --git-dir &> /dev/null
git_status="$(git status 2> /dev/null)"
branch_pattern="On branch ([^${IFS}]*)"
remote_pattern="Your branch is (.*) of (.*) by ([0-9]*) commit"
diverge_pattern="Your branch and (.*) have diverged"
untracked_pattern="Untracked files"
if [[ ${git_status} =~ "${untracked_pattern}" ]]; then
adds="+"
fi
if [[ ! ${git_status} =~ "working directory clean" ]]; then
state="${YELLOW} ${RED}✐ ${adds}${NORMAL}"
fi
# 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="${YELLOW}↑${BASH_REMATCH[3]}"
else
remote="${YELLOW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " (${branch})${remote}${state}"
fi
}
function prompt_func() {
previous_return_value=$?;
prompt="${BLUE}\w${GREEN}$(parse_git_branch)${BLUE}${NORMAL} "
if test $previous_return_value -eq 0
then
PS1="${prompt}➔ "
else
PS1="${prompt}${RED}➔${NORMAL} "
fi
}
PROMPT_COMMAND=prompt_func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment