#!/bin/bash

_git () {
  if git branch --show-current > /dev/null 2>&1
  then
    _BRANCH="$(git branch --show-current)"
    _MODIFIED="$(grep -o "M" <<< "$(git status --short)" | grep -c .)"
    _UNTRACKED="$(grep -o "??" <<< "$(git status --short)" | grep -c .)"
    _ORIGIN="$(git remote show)"
    _AHEAD=$(git rev-list --left-right --count "$_BRANCH"..."$_ORIGIN"/"$_BRANCH" 2> /dev/null | cut -f 1)
    _BEHIND=$(git rev-list --left-right --count "$_BRANCH"..."$_ORIGIN"/"$_BRANCH" 2> /dev/null | cut -f 2)
    printf " %s " "$(git branch --show-current)"
    if (( _MODIFIED == 0 )) && (( _UNTRACKED == 0 )) && (( _AHEAD == 0 )) && (( _BEHIND == 0 ))
    then
      printf " "
      exit 0
    fi
    if (( _MODIFIED > 0 ))
    then
      printf "✗%s " "$_MODIFIED"
    fi
    if (( _UNTRACKED > 0 ))
    then
      printf "★%s " "$_UNTRACKED"
    fi
    if (( _AHEAD > 0 )); then
      printf "%s " "$_AHEAD"
    fi
    if (( _BEHIND > 0 )); then
      printf "%s " "$_BEHIND"
    fi
  fi
}
