Skip to content

Instantly share code, notes, and snippets.

@brodkin
Created April 6, 2013 18:55
Show Gist options
  • Save brodkin/5327196 to your computer and use it in GitHub Desktop.
Save brodkin/5327196 to your computer and use it in GitHub Desktop.
Git-Enhanced Bash Prompt Displays color-coded information about your current project on the command line.
######################################
# Git-Enhanced Bash Prompt #
######################################
txtred='\033[0;31m'
txtgrn='\033[0;32m'
txtylw='\033[1;33m'
txtltcyn='\033[1;36m'
end='\033[0m'
# Color-Coded Name of Branch
function git_branch_name {
branch=$(__git_ps1 "%s")
if [[ -z $branch ]]; then
return
fi
status="$(git status 2>/dev/null)"
if [[ $status =~ "Untracked files" ]]; then
branch=${txtred}${branch}${end}
fi
if [[ $status =~ "Changed but not updated" ]]; then
branch=${txtylw}${branch}${end}
fi
if [[ $status =~ "Changes to be committed" ]]; then
branch=${txtgrn}${branch}${end}
fi
echo -e "($branch)"
}
# Full System Path to Git Repo
function git_repo_path {
echo `git rev-parse --show-toplevel 2>/dev/null`
}
# Name of Git Repo w/ Color
function git_repo_name {
path=`git_repo_path`
basename=`basename $path`
repo="${basename//[$'\n',$'\r']/ }"
echo -e "${txtltcyn}${repo}${end}"
}
# Relative Path Within Repository
function git_working_path {
path_current=`pwd`
path_repo=`git rev-parse --show-toplevel 2>/dev/null`
echo ${path_current#${path_repo}}
}
# Update Bash PS1
function update_bash_prompt {
if [[ -d `git_repo_path` ]];
# we're in a git repo
then
export PS1='$(git_repo_name) $(git_working_path) $(git_branch_name) \$ '
# we're not in a git repo :(
else
export PS1='\h:\w $ '
fi
}
# Update on Load
update_bash_prompt;
# Update on CD
function cd () {
builtin cd "$@" && update_bash_prompt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment