bash_aliases: rework prompt_ps1_git_branch() to show ahead/behind status

This commit is contained in:
Josh Holtrop 2012-01-19 15:12:59 -05:00
parent 1052bc4fb3
commit 90af463af4

View File

@ -25,12 +25,23 @@ alias cdshowgitstatus='if [[ $CDSHOWGITSTATUS_LAST_WD != $PWD ]]; then if [[ -d
#PROMPT_COMMAND="cdshowgitstatus;$PROMPT_COMMAND" #PROMPT_COMMAND="cdshowgitstatus;$PROMPT_COMMAND"
function prompt_ps1_git_branch() function prompt_ps1_git_branch()
{ {
if [[ -e /usr/bin/git && "$PCGB_LAST_WD" != "$PWD" ]]; then which_git=$(which git)
current_git_branch=$(git branch 2>/dev/null | grep '^\*' | sed -e 's/^..//'); if [[ "$which_git" == "" ]]; then
PCGB_LAST_WD=$PWD; return
fi; fi
if [[ "$current_git_branch" != "" ]]; then branch_out=$(git branch -vv 2>/dev/null | grep '^\*' | sed -e 's/^..//')
echo -e " [${current_git_branch}]"; if [[ "$branch_out" == "" ]]; then
return
fi
current_branch=$(echo "$branch_out" | awk '{print $1}')
ahead_behind=$(echo "$branch_out" | grep -E '(ahead|behind)' | sed -re 's/^.*(ahead|behind)(.[0-9]+).*$/\1\2/')
if [[ "$ahead_behind" != "" ]]; then
ahead_behind=": ${ahead_behind}"
fi
if [[ "$branch_out" != "" ]]; then
if [[ "$current_branch" != "" ]]; then
echo -e " [${current_branch}${ahead_behind}]"
fi
fi fi
} }
#put $(prompt_ps1_git_branch) in $PS1 to use it #put $(prompt_ps1_git_branch) in $PS1 to use it