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
This commit is contained in:
Josh Holtrop 2016-04-06 17:23:55 -04:00
parent 18ce273e18
commit 7309c0b32c

View File

@ -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]}"
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 [[ "$branch_out" =~ $re ]]; then
if [[ "$git_branch_out" =~ $re ]]; then
current_branch="$current_branch: ${BASH_REMATCH[1]}"
fi
echo " [${current_branch}${ahead_behind}]"
fi
done <<< "$git_branch_out"
fi
}