#!/bin/bash

usage='gilt [-h] [dir]
Run git status on current or specified dir.

Options:
  -h or -help - print this message'

while [[ $1 == -* ]] ; do
    opt=$1
    shift
    case "$opt" in 
        -h) echo "$usage" > /dev/stderr
            exit 1 ;;
        *)  echo "Error: invalid option: $opt" > /dev/stderr
            echo "$usage" > /dev/stderr
            exit 1 ;;
    esac
done

dir="$1"
if [ "$dir" == "" ]; then 
  dir="."
fi
onbranch=`git branch | grep '^[*]' | sed -e 's/* //'`
mymaster=`git log  HEAD ^origin/master | grep '^commit' | wc -l`
omaster=`git  log ^HEAD  origin/master | grep '^commit' | wc -l`
staged=`git diff --stat --cached`
dirty=`git diff --stat $dir`

echo "On $onbranch branch."

if [ "$mymaster" -gt "0" ] && [ "$omaster" -gt "0" ]; then
    echo "Your branch and 'origin/master' have diverged,"
    echo "and have $mymaster and $omaster different commit(s) each, respectively."
elif [ "$mymaster" -eq "0" ] && [ "$omaster" -gt "0" ]; then
    if [ "$omaster" -eq "1" ]; then
	plural=""
    else
	plural="s"
    fi
    echo "Your branch is behind 'origin/master' by $omaster commit$plural, and can be fast-forwarded."
elif [ "$mymaster" -gt "0" ] && [ "$omaster" -eq "0" ]; then
    if [ "$mymaster" -eq "1" ]; then
	plural=""
    else
	plural="s"
    fi
    echo "Your branch is ahead of 'origin/master' by $mymaster commit$plural."
fi

if [ -n "$staged" ]; then
    echo "staged:"
    echo "$staged"
fi
if [ -n "$dirty" ]; then
    echo "working-dir:"
    echo "$dirty"
fi

