removed need to use temporary file in gitstatus.sh

master
Martin Gondermann 12 years ago
parent 4cbef92cf5
commit 5be51b8cf6
  1. 32
      gitstatus.sh

@ -5,6 +5,10 @@
# #
# Alan K. Stebbens <aks@stebbens.org> [http://github.com/aks] # Alan K. Stebbens <aks@stebbens.org> [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 # change those symbols to whatever you prefer
symbols_ahead='↑·' symbols_ahead='↑·'
symbols_behind='↓·' symbols_behind='↓·'
@ -18,24 +22,17 @@ case "$gitsym" in fatal*) exit 0 ;; esac
# the current branch is the tail end of the symbolic reference # the current branch is the tail end of the symbolic reference
branch="${gitsym##*/}" # get the basename after "refs/head/" branch="${gitsym##*/}" # get the basename after "refs/head/"
tmp="/tmp/$$-gitstatus.out" gitstatus=`git diff --name-status 2>&1`
trap "rm -f \"$tmp\"" EXIT
status=`git diff --name-status >$tmp`
# if the diff is fatal, exit now # if the diff is fatal, exit now
if grep -s "^fatal:" $tmp 2>/dev/null ; then case "$gitstatus" in fatal*) exit 0 ;; esac
exit
fi
# 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_changed=$(( `all_lines "$gitstatus"` - `count_lines "$gitstatus" U` ))
num_conflicts=`count_lines U` num_conflicts=`count_lines "$staged_files" U`
num_staged=$(( `wc -l <$tmp` - num_conflicts )) num_staged=$(( `all_lines "$staged_files"` - num_conflicts ))
num_untracked=`git status -s -uall | grep -c "^??"` num_untracked=`git status -s -uall | grep -c "^??"`
num_stashed=`git stash list | wc -l` num_stashed=`git stash list | wc -l`
@ -55,21 +52,24 @@ if [[ -z "$branch" ]]; then
fi fi
else else
remote_name=`git config branch.${branch}.remote` remote_name=`git config branch.${branch}.remote`
if [[ -n "$remote_name" ]]; then if [[ -n "$remote_name" ]]; then
merge_name=`git config branch.${branch}.merge` merge_name=`git config branch.${branch}.merge`
else else
remote_name='origin' remote_name='origin'
merge_name="refs/heads/${branch}" merge_name="refs/heads/${branch}"
fi fi
if [[ "$remote_name" == '.' ]]; then if [[ "$remote_name" == '.' ]]; then
remote_ref="$merge_name" remote_ref="$merge_name"
else else
remote_ref="refs/remotes/$remote_name/${merge_name##*/}" remote_ref="refs/remotes/$remote_name/${merge_name##*/}"
fi fi
# get the revision list, and count the leading "<" and ">" # get the revision list, and count the leading "<" and ">"
revgit=`git rev-list --left-right ${remote_ref}...HEAD >$tmp` revgit=`git rev-list --left-right ${remote_ref}...HEAD`
num_revs=`wc -l <$tmp` num_revs=`all_lines "$revgit"`
num_ahead=`count_lines "^>"` num_ahead=`count_lines "$revgit" "^>"`
num_behind=$(( num_revs - num_ahead )) num_behind=$(( num_revs - num_ahead ))
if (( num_behind > 0 )) ; then if (( num_behind > 0 )) ; then
remote="${remote}${symbols_behind}${num_behind}" remote="${remote}${symbols_behind}${num_behind}"

Loading…
Cancel
Save