Skip to content

Instantly share code, notes, and snippets.

@langford
Last active December 20, 2015 16:59
Show Gist options
  • Save langford/6165405 to your computer and use it in GitHub Desktop.
Save langford/6165405 to your computer and use it in GitHub Desktop.
.profile script to make git colorize and show what state your repo is in.
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function parse_git_branch_remote {
git rev-parse --git-dir > /dev/null 2>&1
if [ $? -eq 0 ]; then
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
detached_branch_pattern="# Not currently on any branch"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
untracked_pattern="# Untracked files:"
new_pattern="new file:"
not_staged_pattern="Changes not staged for commit"
local remote=""
# add an else if or two here if you want to get more specific
# show if we're ahead or behind HEAD
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="[ahead]"
else
remote="[behind]"
fi
fi
#new files
if [[ ${git_status} =~ ${new_pattern} ]]; then
remote="[new]"
fi
#untracked files
if [[ ${git_status} =~ ${untracked_pattern} ]]; then
remote="[untracked]"
fi
#diverged branch
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="[diverged]"
fi
#branch name
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
#detached branch
elif [[ ${git_status} =~ ${detached_branch_pattern} ]]; then
branch="NO BRANCH"
fi
#files not staged for commit
if [[ ${git_status}} =~ ${not_staged_pattern} ]]; then
remote="[dirty]"
fi
echo "${remote}"
fi
return
}
function proml {
local BLUE="\[\033[0;34m\]"
# OPTIONAL - if you want to use any of these other colors:
local RED="\[\033[0;31m\]"
local LIGHT_RED="\[\033[1;31m\]"
local GREEN="\[\033[0;32m\]"
local LIGHT_GREEN="\[\033[1;32m\]"
local WHITE="\[\033[1;37m\]"
local LIGHT_GRAY="\[\033[0;37m\]"
local BOLD_GREEN="\[\e[1;32m\]"
# END OPTIONAL
local DEFAULT="\[\033[0m\]"
#PS1="\W:$BOLD_GREEN\$(parse_git_branch)$DEFAULT> "
export PS1="\W:$BOLD_GREEN\$(parse_git_branch)$LIGHT_RED\$(parse_git_branch_remote)$DEFAULT> "
}
proml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment