Skip to content

Instantly share code, notes, and snippets.

@wereHamster
Created January 15, 2011 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wereHamster/781117 to your computer and use it in GitHub Desktop.
Save wereHamster/781117 to your computer and use it in GitHub Desktop.
My old bash git prompt
__git_branch () {
local b
if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
b="$(git rev-parse 2>/dev/null | cut -c1-7)..."
fi
fi
echo "$b"
}
__git_dirty () {
[[ -n "$(git diff-index --name-only HEAD --)" ]] && return 0
}
__git_cstat () {
local r="$(git config --get branch.$1.remote)"
local m="$(git config --get branch.$1.merge)"
m=${m#refs/heads}
[[ -z "$r" || -z "$m" ]] && return
local l="$(git rev-list --left-right "$1...$r$m" | cut -c1 | sort | uniq -c)"
local n="$(echo "$l" | grep "<" | sed -re 's/[^0-9]*([0-9]+).*/\1/')"
if [ -n "$n" ]; then
printf -- "\[\033[01;35m\]+$n"
fi
local n="$(echo "$l" | grep ">" | sed -re 's/[^0-9]*([0-9]+).*/\1/')"
if [ -n "$n" ]; then
printf -- "\[\033[01;31m\]-$n"
fi
}
__git_prompt () {
local g b c n
g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -z "$g" ]; then
printf "\[\033[01;34m\]\W"
return
fi
if [ "$g" = ".git" ]; then
g="$PWD/$g"
fi
b="$(__git_branch)"
b="${b##refs/heads/}"
if __git_dirty; then
c="\[\033[01;31m\]"
else
c="\[\033[01;32m\]"
fi
n="$(__git_cstat "$b")"
g="${g%/.git}"
printf "\[\033[01;34m\]%s $c(%s)%s\[\033[01;34m\] - %s" "${g##*/}" "$b" "$n" "${PWD##$g}/"
}
__git_prompt_command() {
PS1="$(printf "\[\033[01;0m\][ \[\033[00;32m\]\h\[\033[01;0m\] ] $(__git_prompt) \$\[\033[01;0m\] ")"
}
PS1="\[\033[01;0m\][ \[\033[00;32m\]\h\[\033[01;0m\] ] \[\033[01;34m\]\W \$\[\033[01;0m\] "
PROMPT_COMMAND=__git_prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment