Skip to content

Instantly share code, notes, and snippets.

@jstralko
Last active March 31, 2016 14:54
Show Gist options
  • Save jstralko/a9ee995c2da50fa04d1ec932a203a608 to your computer and use it in GitHub Desktop.
Save jstralko/a9ee995c2da50fa04d1ec932a203a608 to your computer and use it in GitHub Desktop.
c_cyan=`tput setaf 6`
c_red=`tput setaf 1`
c_green=`tput setaf 2`
c_sgr0=`tput sgr0`
c_pink=`tput setaf 5`
parse_git_branch ()
{
git rev-list -n 1 --all >/dev/null 2>&1
retcode=$?
if [ $retcode -eq 129 ]; then
echo " (EMPTY)"
elif [ $retcode -eq 128 ]; then
return 0
else
git rev-parse --git-dir >/dev/null 2>&1
remote=""
git_status="$(git status 2> /dev/null)"
if [ $? -eq 128 ]; then
echo " (!GIT_DIR!)"
else
branch=`git rev-parse --abbrev-ref HEAD`
git config --get branch.$branch.remote >/dev/null 2>&1
if [ $? -eq 0 ]; then
#its a tracking branch
behind=`git log ..@{u} --pretty=oneline | wc -l | sed -e 's/[ \t]*//g'`
ahead=`git log @{u}.. --pretty=oneline | wc -l | sed -e 's/[ \t]*//g'`
diverged=`git log @{u}... --pretty=oneline | wc -l | sed -e 's/[ \t]*//g'`
if [ $diverged -ne 0 ]; then
if [ "$behind" == "$diverged" ]; then
#remote=" [$behind]↓"
remote="↓"
elif [ "$ahead" == "$diverged" ]; then
#remote=" [$ahead]
remote="↑"
else
remote="↕"
#remote=" [$behind]↓ [$ahead]↑"
fi
fi
fi
echo " (${branch}${remote})"
fi
fi
}
branch_color ()
{
color=""
git rev-parse --git-dir >/dev/null 2>&1
if [ $? -eq 0 ]; then
git status --untracked-files=no --porcelain >/dev/null 2>&1
if [ $? -eq 128 ]; then
color=${c_pink}
elif [ -z "$(git status --untracked-files=no --porcelain)" ]; then
color="${c_green}"
else
color=${c_red}
fi
else
return 0
fi
echo -ne $color
}
export PS1='\[\033[1;32m\]\u\[\033[0m\]@\[\033[1;36m\]\h\[\033[0m\] \W\[$(branch_color)\]$(parse_git_branch)\[\033[0m\] \$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment