Skip to content

Instantly share code, notes, and snippets.

@rajiv
Created May 5, 2011 14:51
Show Gist options
  • Save rajiv/957181 to your computer and use it in GitHub Desktop.
Save rajiv/957181 to your computer and use it in GitHub Desktop.
version control info in your bash shell prompt
# based on http://pastie.org/230805
# http://gist.github.com/3829/
# http://pastie.org/1868906
# from http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
_bold=$(tput bold)
_normal=$(tput sgr0)
__vcs_dir() {
local vcs base_dir sub_dir ref
git_dir() {
local t
base_dir="./$(git rev-parse --show-cdup 2>/dev/null)" || return 1
if [ -n "$base_dir" ]; then
base_dir=`cd $base_dir; pwd`
else
base_dir=$PWD
fi
sub_dir=$(git rev-parse --show-prefix)
[ -n "${sub_dir}" ] && sub_dir="/${sub_dir%/}"
ref=$(git symbolic-ref -q HEAD || git name-rev --name-only HEAD 2>/dev/null)
ref=${ref#refs/heads/}
t=$(git describe --tags 2>/dev/null)
[ ${?} -eq 0 ] && ref=${ref}:${t%-*}
vcs="git"
alias pull="git pull"
alias f="git fetch"
alias commit="git commit -a"
alias push="commit && git push"
alias revert="git checkout"
alias gl="git log --oneline --decorate --graph"
alias ds="git diff --staged"
}
svn_dir() {
[ -d ".svn" ] || return 1
base_dir="."
while [ -d "${base_dir}"/../.svn ]; do
base_dir="${base_dir}/.."
done
base_dir=`cd $base_dir; pwd`
sub_dir="${PWD#${base_dir}}"
ref=$(svn info "${base_dir}" \
| awk '/^URL/ { sub(".*/","",$0); r=$0 } /^Revision/ { sub("[^0-9]*","",$0); print r":"$0 }')
vcs="svn"
alias pull="svn up"
alias commit="svn commit"
alias push="svn ci"
alias revert="svn revert"
}
cvs_dir() {
[ -d "CVS" ] || return 1
base_dir="."
while [ -d "$base_dir/../CVS" ]; do
base_dir="$base_dir/.."
done
base_dir=`cd $base_dir; pwd`
sub_dir="${PWD#${base_dir}}"
if [ -e "CVS/Tag" ]; then
ref="$(cut -c 2- CVS/Tag)"
else
ref="trunk"
fi
vcs="cvs"
alias pull="cvs update"
alias commit="cvs commit"
alias push="cvs commit"
}
cvs_dir || svn_dir || git_dir
# from http://wiki.archlinux.org/index.php/Color_Bash_Prompt
local NONE="\[\033[0m\]" # unsets color to term's fg color
# regular colors
local K="\[\033[0;30m\]" # black
local R="\[\033[0;31m\]" # red
local G="\[\033[0;32m\]" # green
local Y="\[\033[0;33m\]" # yellow
local B="\[\033[0;34m\]" # blue
local M="\[\033[0;35m\]" # magenta
local C="\[\033[0;36m\]" # cyan
local W="\[\033[0;37m\]" # white
# emphasized (bolded) colors
local EMK="\[\033[1;30m\]"
local EMR="\[\033[1;31m\]"
local EMG="\[\033[1;32m\]"
local EMY="\[\033[1;33m\]"
local EMB="\[\033[1;34m\]"
local EMM="\[\033[1;35m\]"
local EMC="\[\033[1;36m\]"
local EMW="\[\033[1;37m\]"
# background colors
local BGK="\[\033[40m\]"
local BGR="\[\033[41m\]"
local BGG="\[\033[42m\]"
local BGY="\[\033[43m\]"
local BGB="\[\033[44m\]"
local BGM="\[\033[45m\]"
local BGC="\[\033[46m\]"
local BGW="\[\033[47m\]"
if [ -n "$vcs" ]; then
__vcs_prefix="$vcs:"
__vcs_base_dir="${EMC}${base_dir/$HOME/~}"
__vcs_ref="[$ref]"
__vcs_sub_dir="${sub_dir}"
alias st="$vcs status"
alias d="$vcs diff"
alias up="pull"
else
__vcs_prefix=''
__vcs_base_dir="${EMB}${PWD/$HOME/~}"
__vcs_ref=''
__vcs_sub_dir=''
unalias st 2> /dev/null
unalias d 2> /dev/null
unalias up 2> /dev/null
unalias pull 2> /dev/null
unalias commit 2> /dev/null
unalias push 2> /dev/null
unalias revert 2> /dev/null
unalias f 2> /dev/null
unalias gl 2> /dev/null
unalias ds 2> /dev/null
fi
export PS1="${EMG}\u@\h ${C}${__vcs_prefix}${__vcs_base_dir}${C}${__vcs_ref}${EMC}${__vcs_sub_dir} \$${NONE} "
}
export PROMPT_COMMAND=__vcs_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment