From 7309c0b32ccc5547de84a306fa28bf7e9eb9fc7b Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 6 Apr 2016 17:23:55 -0400 Subject: [PATCH] bash_aliases: rework prompt_ps1_git_branch again - avoid pipe to grep to find current branch line - use local variables - update to support git 2.x format for current branch detached-HEAD format --- bash_aliases | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/bash_aliases b/bash_aliases index 840b1a9..82ca42d 100755 --- a/bash_aliases +++ b/bash_aliases @@ -14,20 +14,24 @@ fi function prompt_ps1_git_branch() { + local git_branch_out + local git_branch_out_line if [ -e .git ]; then - branch_out=$(command git branch -vv 2>/dev/null | grep '^\*') - if [[ "$branch_out" == "" ]]; then + git_branch_out=$(command git branch -vv 2>/dev/null) + if [[ "$git_branch_out" == "" ]]; then return fi - re="^\\* (\\((detached|no.branch)[^)]*\\)|[^ ]*)" - if [[ "$branch_out" =~ $re ]]; then - current_branch="${BASH_REMATCH[1]}" - re="((ahead|behind)[^]]*)\\]" - if [[ "$branch_out" =~ $re ]]; then - current_branch="$current_branch: ${BASH_REMATCH[1]}" + local re="^\\* (\\(([^)]*)\\)|([^ ]*))" + while read -r git_branch_out_line; do + if [[ "$git_branch_out_line" =~ $re ]]; then + local current_branch="${BASH_REMATCH[2]:-${BASH_REMATCH[3]}}" + re="((ahead|behind)[^]]*)\\]" + if [[ "$git_branch_out" =~ $re ]]; then + current_branch="$current_branch: ${BASH_REMATCH[1]}" + fi + echo " [${current_branch}${ahead_behind}]" fi - echo " [${current_branch}${ahead_behind}]" - fi + done <<< "$git_branch_out" fi }