Skip to content

Instantly share code, notes, and snippets.

@enkrates
Forked from rajiv/gist:957181
Created August 9, 2011 18:18
Show Gist options
  • Save enkrates/1134773 to your computer and use it in GitHub Desktop.
Save enkrates/1134773 to your computer and use it in GitHub Desktop.
version control info in your bash shell prompt
# based on http://pastie.org/230805
# and http://gist.github.com/3829/
# 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
sub_dir() {
local sub_dir
sub_dir=$(stat -f "${PWD}")
sub_dir=${sub_dir#$1}
echo ${sub_dir#/}
}
git_dir() {
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)
sub_dir="/${sub_dir%/}"
ref=$(git symbolic-ref -q HEAD || git-name-rev --name-only HEAD 2>/dev/null)
ref=${ref#refs/heads/}
# ref=${ref}/$(git describe --tags)
vcs="git"
base_dir="$(basename "${base_dir}")"
alias pull="git pull"
alias commit="git commit -a"
alias push="commit && git push"
alias revert="git checkout"
}
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="/$(sub_dir "${base_dir}")"
ref=$(svn info "$base_dir" | awk '/^URL/ { sub(".*/","",$0); r=$0 } /^Revision/ { sub("[^0-9]*","",$0); print r":"$0 }')
vcs="svn"
base_dir="$(basename "${base_dir}")"
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="/$(sub_dir "${base_dir}")"
if [ -e "CVS/Tag" ]; then
ref="$(cut -c 2- CVS/Tag)"
else
ref="trunk"
fi
vcs="cvs"
base_dir="$(basename "${base_dir}")"
alias pull="cvs update"
alias commit="cvs commit"
alias push="cvs commit"
alias revert=""
}
cvs_dir || svn_dir || git_dir
if [ -n "$vcs" ]; then
__vcs_prefix="$vcs:"
__vcs_base_dir="${base_dir/$HOME/~}"
__vcs_ref="[$ref]"
__vcs_sub_dir="${sub_dir}"
alias st="$vcs status"
alias d="$vcs diff"
alias up="pull"
else
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
__vcs_prefix=''
# __vcs_base_dir_temp=`basename "$PWD"`
# if [ "$PWD" == "$HOME" ]; then
# __vcs_base_dir="~"
# else
# __vcs_base_dir="`basename "$PWD"`"
# fi
__vcs_base_dir=${PWD/$HOME/"~"}
__vcs_ref=''
__vcs_sub_dir=''
fi
# 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\]"
export PS1="${EMG}\u@host ${C}${__vcs_prefix}${EMC}${__vcs_base_dir}${C}${__vcs_ref}${EMC}${__vcs_sub_dir} \$${NONE} "
}
PROMPT_COMMAND=__vcs_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment