Skip to content

Instantly share code, notes, and snippets.

@ismasan
Created February 1, 2012 23:16
Show Gist options
  • Save ismasan/1720083 to your computer and use it in GitHub Desktop.
Save ismasan/1720083 to your computer and use it in GitHub Desktop.
#########################
# FUNCTIONS
#########################
function parse_git_branch {
[ -d .git ] || return 1
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*)"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state=" *"
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=" ↑"
else
remote=" ↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote=" ↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo ": [${branch}${state}${remote}]"
fi
}
############################################
# TERMINAL PROMPT WITH CURRENT GIT BRANCH
###########################################
if [ "$PS1" ]; then
PS1="\[\033[01;34m\]\w\[\033[00m\]\$(parse_git_branch) $ "
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment