Skip to content

Instantly share code, notes, and snippets.

@HowlingMind
Forked from matthewmccullough/gist:47267
Created May 27, 2011 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HowlingMind/996093 to your computer and use it in GitHub Desktop.
Save HowlingMind/996093 to your computer and use it in GitHub Desktop.
Git status Prompt including dirty and number of commits ahead/behind
# Colors
# ######
DULL=0
BRIGHT=1
NORMAL_COLOR="\[$ESC[m\]"
##
# Shortcuts for Colored Text ( Bright and FG Only )
##
# DULL TEXT
BLACK="\[$ESC[${DULL};30m\]"
RED="\[$ESC[${DULL};31m\]"
GREEN="\[$ESC[${DULL};32m\]"
YELLOW="\[$ESC[${DULL};33m\]"
BLUE="\[$ESC[${DULL};34m\]"
VIOLET="\[$ESC[${DULL};35m\]"
CYAN="\[$ESC[${DULL};36m\]"
WHITE="\[$ESC[${DULL};37m\]"
# BRIGHT TEXT
BRIGHT_BLACK="\[$ESC[${BRIGHT};30m\]"
BRIGHT_RED="\[$ESC[${BRIGHT};31m\]"
BRIGHT_GREEN="\[$ESC[${BRIGHT};32m\]"
BRIGHT_YELLOW="\[$ESC[${BRIGHT};33m\]"
BRIGHT_BLUE="\[$ESC[${BRIGHT};34m\]"
BRIGHT_VIOLET="\[$ESC[${BRIGHT};35m\]"
BRIGHT_CYAN="\[$ESC[${BRIGHT};36m\]"
BRIGHT_WHITE="\[$ESC[${BRIGHT};37m\]"
# Colorized Git branch and status
# ###############################
function parse_git_branch {
git rev-parse --git-dir &> /dev/null
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (ahead|behind).*by ([[:digit:]]*) commit"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="${BRIGHT_RED} Z"
else
state="${BLUE} C"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${YELLOW} ^${BRIGHT_YELLOW}[${YELLOW}${BASH_REMATCH[2]}${BRIGHT_YELLOW}]"
else
remote="${YELLOW} v${BRIGHT_YELLOW}[${YELLOW}${BASH_REMATCH[2]}${BRIGHT_YELLOW}]"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW} <>"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo " ${BRIGHT_BLUE}(${BRIGHT_CYAN}${branch}${BRIGHT_BLUE})${remote}${state}"
fi
}
# PROMPT
# ######
function prompt_func() {
previous_return_value=$?;
# prompt="${TITLEBAR}$BLUE[$RED\w$GREEN$(__git_ps1)$YELLOW$(git_dirty_flag)$BLUE]$COLOR_NONE "
#prompt="${TITLEBAR}${BLUE}[${RED}\w${GREEN}$(parse_git_branch)${BLUE}]${COLOR_NONE} "
prompt="${BRIGHT_BLUE}[${CYAN}\u@\h${WHITE}:${BRIGHT_GREEN}\w${BRIGHT_BLUE}]${BRIGHT_YELLOW}$(parse_git_branch) \n${NORMAL_COLOR}"
if test $previous_return_value -eq 0
then
PS1="${prompt}\$ "
else
PS1="${prompt}${RED}\$${NORMAL_COLOR} "
fi
}
PROMPT_COMMAND=prompt_func
@HowlingMind
Copy link
Author

This is my version of the gitPrompt (based on matthewmccullough's fantastic bash prompt. I changed the symbols to characters, so I can use it on my windows Cygwin machine and my unix machines. Also added a "C" for clean working directory.

@HowlingMind
Copy link
Author

Updated to include number of commits ahead/behind.

@voku
Copy link

voku commented Sep 13, 2014

only for info: this is also working for zsh, but you have to use "$match" instead of "$BASH_REMATCH" -> voku/dotfiles@2f4e40c :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment