From 5be51b8cf6f98512cf94e280fdd8dc290fc17754 Mon Sep 17 00:00:00 2001 From: Martin Gondermann Date: Wed, 29 Jan 2014 11:08:57 +0100 Subject: [PATCH] removed need to use temporary file in gitstatus.sh --- gitstatus.sh | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/gitstatus.sh b/gitstatus.sh index 1feeb8a..357fd79 100755 --- a/gitstatus.sh +++ b/gitstatus.sh @@ -5,6 +5,10 @@ # # Alan K. Stebbens [http://github.com/aks] +# helper functions +count_lines() { echo "$1" | egrep -c "^$2" ; } +all_lines() { echo "$1" | grep -v "^$" | wc -l ; } + # change those symbols to whatever you prefer symbols_ahead='↑·' symbols_behind='↓·' @@ -18,24 +22,17 @@ case "$gitsym" in fatal*) exit 0 ;; esac # the current branch is the tail end of the symbolic reference branch="${gitsym##*/}" # get the basename after "refs/head/" -tmp="/tmp/$$-gitstatus.out" -trap "rm -f \"$tmp\"" EXIT - -status=`git diff --name-status >$tmp` +gitstatus=`git diff --name-status 2>&1` # if the diff is fatal, exit now -if grep -s "^fatal:" $tmp 2>/dev/null ; then - exit -fi +case "$gitstatus" in fatal*) exit 0 ;; esac -# count_lines U -count_lines() { egrep -c "^$1" <$tmp ; } -num_changed=$(( `wc -l <$tmp` - `count_lines U` )) +staged_files=`git diff --staged --name-status` -staged_files=`git diff --staged --name-status >$tmp` -num_conflicts=`count_lines U` -num_staged=$(( `wc -l <$tmp` - num_conflicts )) +num_changed=$(( `all_lines "$gitstatus"` - `count_lines "$gitstatus" U` )) +num_conflicts=`count_lines "$staged_files" U` +num_staged=$(( `all_lines "$staged_files"` - num_conflicts )) num_untracked=`git status -s -uall | grep -c "^??"` num_stashed=`git stash list | wc -l` @@ -55,21 +52,24 @@ if [[ -z "$branch" ]]; then fi else remote_name=`git config branch.${branch}.remote` + if [[ -n "$remote_name" ]]; then merge_name=`git config branch.${branch}.merge` else remote_name='origin' merge_name="refs/heads/${branch}" fi + if [[ "$remote_name" == '.' ]]; then remote_ref="$merge_name" else remote_ref="refs/remotes/$remote_name/${merge_name##*/}" fi + # get the revision list, and count the leading "<" and ">" - revgit=`git rev-list --left-right ${remote_ref}...HEAD >$tmp` - num_revs=`wc -l <$tmp` - num_ahead=`count_lines "^>"` + revgit=`git rev-list --left-right ${remote_ref}...HEAD` + num_revs=`all_lines "$revgit"` + num_ahead=`count_lines "$revgit" "^>"` num_behind=$(( num_revs - num_ahead )) if (( num_behind > 0 )) ; then remote="${remote}${symbols_behind}${num_behind}"