From 90af463af4b667e418b596a1db2c5fc06362ff26 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 19 Jan 2012 15:12:59 -0500 Subject: [PATCH] bash_aliases: rework prompt_ps1_git_branch() to show ahead/behind status --- bash_aliases | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/bash_aliases b/bash_aliases index d9ebc2e..6dac37d 100755 --- a/bash_aliases +++ b/bash_aliases @@ -25,12 +25,23 @@ alias cdshowgitstatus='if [[ $CDSHOWGITSTATUS_LAST_WD != $PWD ]]; then if [[ -d #PROMPT_COMMAND="cdshowgitstatus;$PROMPT_COMMAND" function prompt_ps1_git_branch() { - if [[ -e /usr/bin/git && "$PCGB_LAST_WD" != "$PWD" ]]; then - current_git_branch=$(git branch 2>/dev/null | grep '^\*' | sed -e 's/^..//'); - PCGB_LAST_WD=$PWD; - fi; - if [[ "$current_git_branch" != "" ]]; then - echo -e " [${current_git_branch}]"; + which_git=$(which git) + if [[ "$which_git" == "" ]]; then + return + fi + branch_out=$(git branch -vv 2>/dev/null | grep '^\*' | sed -e 's/^..//') + 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 } #put $(prompt_ps1_git_branch) in $PS1 to use it