Skip to content

Instantly share code, notes, and snippets.

@warrickcustomhomes
Created May 29, 2009 05:15
Show Gist options
  • Save warrickcustomhomes/119794 to your computer and use it in GitHub Desktop.
Save warrickcustomhomes/119794 to your computer and use it in GitHub Desktop.
bash config file to show git branch
# My bash_prompt file to do the following:
# - don't show current path
# - show current branch and branch status if in a git repository
#
# Original taken from:
# http://git.dreaminginlyrics.com/?p=configs.git;a=blob_plain;f=.bash_prompt;hb=HEAD
#
# I added:
# - prompt_git_open_bracket
# - prompt_git_close_bracket
#
# I modified PS1 to do the following:
# - NOT show history line number
# - NOT show current path
# - place brackets around current git branch if in git repository
#
# Included in terminal via .bashrc:
# - '. ~/.bash_prompt' placed at bottom of .bashrc
prompt_username_color() {
if [ "$(id -u)" == "0" ]; then
echo -e "\033[1;31m"
else
echo -e "\033[1;32m"
fi
}
prompt_hostname_color() {
if [[ ${SSH_CLIENT} ]] || [[ ${SSH2_CLIENT} ]]; then
echo -e "\033[1;35m"
else
echo -e "\033[1;34m"
fi
}
prompt_git_branch_color() {
if type -p __git_ps1; then
branch=$(__git_ps1 '%s')
if [ -n "$branch" ]; then
status=$(git status 2> /dev/null)
if $(echo $status | grep 'added to commit' &> /dev/null); then
# If we have modified files but no index (blue)
echo -e "\033[1;34m"
else
if $(echo $status | grep 'to be committed' &> /dev/null); then
# If we have files in index (red)
echo -e "\033[1;31m"
else
# If we are completely clean (green)
echo -e "\033[1;32m"
fi
fi
fi
fi
}
prompt_git_branch() {
if type -p __git_ps1; then
branch=$(__git_ps1 '%s')
if [ -n "$branch" ]; then
echo -e "$branch"
fi
fi
}
prompt_git_label() {
if type -p __git_ps1; then
branch=$(__git_ps1 '%s')
if [ -n "$branch" ]; then
echo -e " git:"
fi
fi
}
prompt_git_open_bracket() {
if type -p __git_ps1; then
branch=$(__git_ps1 '%s')
if [ -n "$branch" ]; then
echo -e "["
fi
fi
}
prompt_git_close_bracket() {
if type -p __git_ps1; then
branch=$(__git_ps1 '%s')
if [ -n "$branch" ]; then
echo -e "]"
fi
fi
}
# PS1="[\[\033[1;31m\]\!\[\033[0m\]]\[\$(prompt_username_color)\]\u\[\033[0m\]@\[\$(prompt_hostname_color)\]\h\[\033[0m\]:\w\$(prompt_git_label)\[\$(prompt_git_branch_color)\]\$(prompt_git_branch)\[\033[1;35m\]\$\[\033[0m\] "
PS1="$(prompt_username_color)\u\[\033[0m\]@\[\$(prompt_hostname_color)\]\h\[\033[0m\] \$(prompt_git_open_bracket)\[\$(prompt_git_branch_color)\]\$(prompt_git_branch)\[\033[0m\]\$(prompt_git_close_bracket)\[\033[1;35m\]\$\[\033[0m\] "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment