Merge branch 'porcelain-remove-grep' into porcelain

* porcelain-remove-grep:
  Fix parsing when both ahead and behind
  Fix behind parsing
  Use printf instead of echo to display
  Fix handling of stash
  Switch case matches from * to ?
  Remove calls to grep/egrep
master
Dan Nguyen 10 years ago
commit 0e6b70f8e3
  1. 55
      gitstatus.sh

@ -5,9 +5,6 @@
#
# Alan K. Stebbens <aks@stebbens.org> [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=`wc -l "${stash_file}" | cut -d' ' -f1`
else
num_stashed=0
fi
@ -67,23 +74,31 @@ 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
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

Loading…
Cancel
Save