Created
January 18, 2010 05:56
-
-
Save jonspeicher/279815 to your computer and use it in GitHub Desktop.
Add colored status to your bash prompt while in a git repo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define a few colors for later use. The escaped brackets tell bash that they | |
# are non-printable and keep word-wrapping sane. | |
TXT_RED='\['`tput setaf 1`'\]' | |
TXT_GREEN='\['`tput setaf 2`'\]' | |
TXT_RESET='\['`tput sgr0`'\]' | |
# Build a prompt decorator if we're in a git repo. The branch name is included | |
# in green if the branch is master, red otherwise. An asterisk appears if the | |
# working directory is not clean. | |
function parse_git_branch { | |
status="$(git status 2> /dev/null)" | |
pattern="^# On branch ([^${IFS}]*)" | |
git_prompt_decorator=" " | |
if [[ ${status} =~ ${pattern} ]]; then | |
branch_name=${BASH_REMATCH[1]} | |
dirty="" | |
color=${TXT_RED} | |
if [[ ! ${status} =~ "working directory clean" ]]; then | |
dirty="*" | |
fi | |
if [[ ${branch_name} == "master" ]]; then | |
color=${TXT_GREEN} | |
fi | |
git_prompt_decorator=" [${color}${branch_name}${TXT_RESET}${dirty}] " | |
fi | |
} | |
# Set up a function that is run every time the shell prompt is generated. | |
function set_shell_prompt { | |
parse_git_branch | |
PS1="\h:\W"${git_prompt_decorator}"\$ " | |
} | |
export PROMPT_COMMAND=set_shell_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment