From e4c6b0ab9e9895968bd3fc08928eb22b7c850f9f Mon Sep 17 00:00:00 2001 From: Dan Nguyen Date: Wed, 23 Sep 2015 15:09:40 -0500 Subject: [PATCH 1/6] Remove calls to grep/egrep --- gitstatus.sh | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/gitstatus.sh b/gitstatus.sh index df90245..bb98436 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -5,9 +5,6 @@ # # Alan K. Stebbens [http://github.com/aks] -# helper functions -count_lines() { echo "$1" | egrep -c $3 "^$2" ; } - if [ -z "${__GIT_PROMPT_DIR}" ]; then SOURCE="${BASH_SOURCE[0]}" while [ -h "${SOURCE}" ]; do @@ -23,17 +20,27 @@ gitstatus=`LC_ALL=C git status --porcelain --branch` # if the status is fatal, exit now [[ "$?" -ne 0 ]] && exit 0 -num_staged=`count_lines "$gitstatus" "(\?\?|##| )" "-v"` -num_changed=`count_lines "$gitstatus" ".M"` -num_conflicts=`count_lines "$gitstatus" "U"` -num_untracked=`count_lines "$gitstatus" "\?\?"` +num_staged=0 +num_changed=0 +num_conflicts=0 +num_untracked=0 +while IFS='' read -r line || [[ -n "$line" ]]; do + status=${line:0:2} + case "$status" in + \#\#) branch_line="$line" ;; + *M) ((num_changed++)) ;; + U*) ((num_conflicts++)) ;; + \?\?) ((num_untracked++)) ;; + *) ((num_staged++)) ;; + esac +done <<< "$gitstatus" if [[ "$__GIT_PROMPT_IGNORE_STASH" = "1" ]]; then num_stashed=0 else stash_file="`git rev-parse --git-dir`/logs/refs/stash" if [[ -e "${stash_file}" ]]; then - num_stashed=`wc -l "${stash_file}" | cut -d' ' -f 1` + num_stashed=`cat "${stash_file} | wc -l` else num_stashed=0 fi @@ -46,7 +53,6 @@ fi remote= -branch_line=`echo "$gitstatus" | grep "^##"` IFS="." read -ra line <<< "${branch_line/\#\# }" branch="${line[0]}" From 1e2ff372826abd8d51d9bb58c3fa6aff5b0dfb40 Mon Sep 17 00:00:00 2001 From: Dan Nguyen Date: Wed, 23 Sep 2015 18:12:37 -0500 Subject: [PATCH 2/6] Switch case matches from * to ? --- gitstatus.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitstatus.sh b/gitstatus.sh index bb98436..03e1bc1 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -28,8 +28,8 @@ while IFS='' read -r line || [[ -n "$line" ]]; do status=${line:0:2} case "$status" in \#\#) branch_line="$line" ;; - *M) ((num_changed++)) ;; - U*) ((num_conflicts++)) ;; + ?M) ((num_changed++)) ;; + U?) ((num_conflicts++)) ;; \?\?) ((num_untracked++)) ;; *) ((num_staged++)) ;; esac From ff7aa3aa7a547c1555aaca435091ce257f465a8c Mon Sep 17 00:00:00 2001 From: Dan Nguyen Date: Wed, 23 Sep 2015 18:22:35 -0500 Subject: [PATCH 3/6] Fix handling of stash --- gitstatus.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitstatus.sh b/gitstatus.sh index 03e1bc1..57876f7 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -40,7 +40,7 @@ if [[ "$__GIT_PROMPT_IGNORE_STASH" = "1" ]]; then else stash_file="`git rev-parse --git-dir`/logs/refs/stash" if [[ -e "${stash_file}" ]]; then - num_stashed=`cat "${stash_file} | wc -l` + num_stashed=`wc -l "${stash_file}" | cut -d' ' -f1` else num_stashed=0 fi From 545cdce3990f56b7734c4cc0accd6ff3fb4bb12a Mon Sep 17 00:00:00 2001 From: Dan Nguyen Date: Wed, 23 Sep 2015 18:29:44 -0500 Subject: [PATCH 4/6] Use printf instead of echo to display --- gitstatus.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/gitstatus.sh b/gitstatus.sh index 57876f7..e1e395d 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -88,8 +88,13 @@ if [[ -z "$remote" ]] ; then remote='.' fi -for w in "$branch" "$remote" $num_staged $num_conflicts $num_changed $num_untracked $num_stashed $clean ; do - echo "$w" -done +printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \ + "$branch" \ + "$remote" \ + $num_staged \ + $num_conflicts \ + $num_changed \ + $num_untracked \ + $num_stashed $clean exit From 606388df395f6ac166c23e1871cbadffc474b4e3 Mon Sep 17 00:00:00 2001 From: Dan Nguyen Date: Wed, 23 Sep 2015 20:05:13 -0500 Subject: [PATCH 5/6] Fix behind parsing --- gitstatus.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitstatus.sh b/gitstatus.sh index e1e395d..57a8afd 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -79,7 +79,7 @@ else remote="${remote}_AHEAD_${num_ahead}" fi if [[ "${remote_line[1]}" == *behind* ]]; then - num_behind=${remote_line[1]:8} + num_behind=${remote_line[1]:7} remote="${remote}_BEHIND_${num_behind}" fi fi From 220d5c22b126f27eb688cd2c1d68e4c07175d37b Mon Sep 17 00:00:00 2001 From: Dan Nguyen Date: Thu, 24 Sep 2015 09:08:27 -0500 Subject: [PATCH 6/6] Fix parsing when both ahead and behind --- gitstatus.sh | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/gitstatus.sh b/gitstatus.sh index 57a8afd..932804a 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -73,15 +73,17 @@ elif [[ "$branch" == *"Initial commit on"* ]]; then elif [[ "$branch" == *"no branch"* ]]; then branch="_PREHASH_`git rev-parse --short HEAD`" else - IFS="[]" read -ra remote_line <<< "${line[3]}" - if [[ "${remote_line[1]}" == *ahead* ]]; then - num_ahead=${remote_line[1]:6} - remote="${remote}_AHEAD_${num_ahead}" - fi - if [[ "${remote_line[1]}" == *behind* ]]; then - num_behind=${remote_line[1]:7} - remote="${remote}_BEHIND_${num_behind}" - fi + IFS="[,]" read -ra remote_line <<< "${line[3]}" + for rline in "${remote_line[@]}"; do + if [[ "$rline" == *ahead* ]]; then + num_ahead=${rline:6} + remote="${remote}_AHEAD_${num_ahead}" + fi + if [[ "$rline" == *behind* ]]; then + num_behind=${rline:7} + remote="${remote}_BEHIND_${num_behind# }" + fi + done fi if [[ -z "$remote" ]] ; then