-
-
Save HowlingMind/996093 to your computer and use it in GitHub Desktop.
Git status Prompt including dirty and number of commits ahead/behind
This file contains 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
# 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 |
Updated to include number of commits ahead/behind.
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
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.