Skip to content

Instantly share code, notes, and snippets.

@croaky
Created November 15, 2008 16:07
Show Gist options
  • Save croaky/25267 to your computer and use it in GitHub Desktop.
Save croaky/25267 to your computer and use it in GitHub Desktop.
_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/}
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"
}
git_dir || svn_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
__vcs_prefix=''
__vcs_base_dir="${PWD/$HOME/~}"
__vcs_ref=''
__vcs_sub_dir=''
fi
}
PROMPT_COMMAND=__vcs_dir
PS1='\u:$__vcs_prefix\[${_bold}\]${__vcs_base_dir}\[${_normal}\]${__vcs_ref}\[${_bold}\]${__vcs_sub_dir}\[${_normal}\]\$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment