Skip to content

Instantly share code, notes, and snippets.

@mfa
Forked from augustjoki/gist:31867
Created November 6, 2009 10:23
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 mfa/227894 to your computer and use it in GitHub Desktop.
Save mfa/227894 to your computer and use it in GitHub Desktop.
# version for bash 2.03
# no use of =~ and BASH_REMATCH allowed
function parse_git_branch {
if [[ `uname -s` == "SunOS" ]]; then
return 1
fi
if git rev-parse --git-dir >/dev/null 2>&1
then
git_status="$(git status 2> /dev/null)"
remote_pattern="# Your branch is "
diverge_pattern="have diverged"
if echo ${git_status} | grep -qv "working directory clean"; then
state="*"
fi
# add an else if or two here if you want to get more specific
if echo ${git_status} | grep -q "${remote_pattern}"; then
if echo ${git_status} | grep -q "ahead" ; then
remote="↑"
else
remote="↓"
fi
fi
if echo ${git_status} | grep -q "${diverge_pattern}"; then
remote="↕"
fi
branch=`echo ${git_status} | grep -v "Not currently on any branch" | head -n1 | sed -e 's/^\# On branch \([a-zA-Z0-9\-]*\).*$/\1/'`
echo "[${branch}${state}${remote}] "
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment