Skip to content

Instantly share code, notes, and snippets.

@1stvamp
Forked from bassdread/Version Display
Created October 13, 2011 15:26
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 1stvamp/1284519 to your computer and use it in GitHub Desktop.
Save 1stvamp/1284519 to your computer and use it in GitHub Desktop.
Display git branch and version in your prompt
# Show current git branch or SVN subfolder in prompt.
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
GRAY="\[\033[1;30m\]"
LIGHT_BLUE="\[\033[1;34m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_OFF="\[\e[0m\]"
LAST_GIT_DIR="!"
function prompt_func() {
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
branch_pattern="\* ([^$IFS]*)"
if [[ $PWD == $LAST_GIT_DIR* ]] || [[ -d "./.git" ]] && [[ "$(git branch --no-color 2> /dev/null)" =~ $branch_pattern ]]; then
LAST_GIT_DIR="$PWD"
branch="${BASH_REMATCH[1]}"
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
PS1+=":$GRAY$branch$LIGHT_GRAY@$revision$COLOR_OFF\$ "
elif [[ -d "./.svn" ]]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
path_pattern="URL: ([^$IFS]*).*Repository Root: ([^$IFS]*).*Revision: ([0-9]*)"
if [[ "$(svn info 2> /dev/null)" =~ ${path_pattern} ]]; then
branch=${BASH_REMATCH[1]##`expr ${BASH_REMATCH[2]}`}
revision="${BASH_REMATCH[3]}"
PS1+=":$GRAY$branch$LIGHT_GRAY@$revision$COLOR_OFF\$ "
fi
fi
}
PROMPT_COMMAND=prompt_func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment