Skip to content

Instantly share code, notes, and snippets.

@fzero

fzero/gitprompt.sh

Last active Mar 27, 2017
Embed
What would you like to do?
Bash prompt customizations for git use
# Git prompt goodness, adapted and curated from several sources.
# Just add this code to your .bash_profile/.bashrc to use it.
#
# It looks like this:
# ~/my/folder(my-git-branch)$
#
# There are variations if there are untracked files (a + will appear), uncommited changes
# (branch colour turns yellow) and if you need to pull/push (arrows will appear).
function parse_git_branch {
# Inits
GIT_BRANCH=""
GIT_UNTRACKED=""
GIT_CLEAN=""
GIT_REMOTE=""
# Match patterns
local branch_pattern="On branch ([^${IFS}]*)"
local remote_pattern="Your branch is (ahead|behind)"
local clean_pattern="working tree clean"
local untracked_pattern="Untracked files"
local diverge_pattern="Your branch and (.*) have diverged"
# Get git status
local git_status="$(git status 2> /dev/null)"
# Get branch
if [[ ${git_status} =~ ${branch_pattern} ]]; then
GIT_BRANCH="${BASH_REMATCH[1]}"
fi
# Check if our working dir is clean
if [[ ! ${git_status} =~ ${clean_pattern} ]]; then
GIT_CLEAN="dirty"
fi
# Check for untracked files
if [[ ${git_status} =~ ${untracked_pattern} ]]; then
GIT_UNTRACKED="+"
fi
# Check if we're ahead or behind
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
GIT_REMOTE=""
elif [[ ${BASH_REMATCH[1]} == "behind" ]]; then
GIT_REMOTE=""
fi
fi
# Check if we diverged
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
GIT_REMOTE=""
fi
}
function awesome_prompt {
local BLACK="\[\033[0;30m\]"
local BLACKBOLD="\[\033[1;30m\]"
local RED="\[\033[0;31m\]"
local REDBOLD="\[\033[1;31m\]"
local GREEN="\[\033[0;32m\]"
local GREENBOLD="\[\033[1;32m\]"
local YELLOW="\[\033[0;33m\]"
local YELLOWBOLD="\[\033[1;33m\]"
local BLUE="\[\033[0;34m\]"
local BLUEBOLD="\[\033[1;34m\]"
local PURPLE="\[\033[0;35m\]"
local PURPLEBOLD="\[\033[1;35m\]"
local CYAN="\[\033[0;36m\]"
local CYANBOLD="\[\033[1;36m\]"
local WHITE="\[\033[0;37m\]"
local WHITEBOLD="\[\033[1;37m\]"
case $TERM in
xterm*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
TITLEBAR=""
;;
esac
# Get git status and fill in status vars
parse_git_branch
GIT_PART=""
if [ "$GIT_BRANCH" != "" ]; then
# Different colours if working dir isn't clean
if [[ "$GIT_CLEAN" == "dirty" ]]; then
GIT_BRANCH_COLOR=$YELLOW
else
GIT_BRANCH_COLOR=$GREEN
fi
GIT_PART="$BLUE($GIT_BRANCH_COLOR$GIT_BRANCH$YELLOW$GIT_REMOTE$RED$GIT_UNTRACKED$BLUE)"
fi
HOSTPART="$BLUE\u@\h"
MAINPART="$CYAN\w$GIT_PART$WHITE\$ "
# Without hostname
export PS1="$TITLEBAR$MAINPART"
# With hostname:
# export PS1="$TITLEBAR$HOSTPART $MAINPART"
}
export PROMPT_DIRTRIM=1 # Shorten directory part - bash 4+ only
export PROMPT_COMMAND="awesome_prompt"
export PS2='> '
export PS4='+ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment