Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
86 lines (66 sloc) 2.18 KB
function parse_git_branch() {
local s='';
local branchName='';
# Check if the current directory is in a Git repository.
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") == '0' ]; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
# Ensure the index is up to date.
git update-index --really-refresh -q &>/dev/null;
# Check for uncommitted changes in the index.
if ! $(git diff --quiet --ignore-submodules --cached); then
s+='+';
fi;
# Check for unstaged changes.
if ! $(git diff-files --quiet --ignore-submodules --); then
s+='!';
fi;
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s+='?';
fi;
# Check for stashed files.
if $(git rev-parse --verify refs/stash &>/dev/null); then
s+='$';
fi;
fi;
# Get the short symbolic ref.
# If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
# Otherwise, just give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')";
echo -e "[${1}${branchName}${blue}${s}]";
else
return;
fi;
}
function parse_hg_dirty() {
hg status 2> /dev/null | awk '$1 == "?" { print "?" } $1 != "?" { print "!" }' | sort | uniq | head -c1
}
function parse_hg_branch {
hg branch 2> /dev/null | sed -e "s/\(.*\)/[\1$(parse_hg_dirty)]/"
}
function last_two_dirs {
pwd |rev| awk -F / '{print $1,$2}' | rev | sed s_\ _/_
}
function parse_hg_git_branch {
([ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1) && is_repo_git=true || is_repo_git=false
([ -d .hg ] || hg branch > /dev/null 2>&1) && is_repo_hg=true || is_repo_hg=false
if $is_repo_git && $is_repo_hg
then
echo $(parse_hg_branch)/$(parse_git_branch)
elif $is_repo_hg
then
echo $(parse_hg_branch)
elif $is_repo_git
then
echo $(parse_git_branch)
fi
}
#Customize the prompt
PS1="\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\$(last_two_dirs)\[\033[00m\]\$(parse_hg_git_branch)"
case `id -u` in
0) PS1="${PS1}# ";;
*) PS1="${PS1}$ ";;
esac