#!/bin/sh

git status --short --branch # print nothing to do msg
if git diff @\{upstream\} >/dev/null 2>&1; then
  # shellcheck disable=SC2312
  if [ "$(git rev-list --count @\{upstream\}..HEAD)" -eq 0 ]; then
    printf '%b\n' "\e[1;31m**You are not ahead of the upstream remote! Amending seems unwise.**\e[m"
    exit 1
  fi
fi
if [ "$#" -eq 0 ]; then
  if git diff --staged --stat --exit-code >/dev/null; then
    if git diff --stat --exit-code >/dev/null; then
      echo "Nothing to commit, working tree clean"
    else
      printf '%b\n' "Nothing staged, so running \e[1;36mgit commit -a --amend --no-edit\e[m."
      printf '%b\n' "\e[0;35m-----------------------------------------------------------\e[m"
      git commit --all --amend --no-edit
    fi
  else
    printf '%b\n' "\e[0;35m-----------------------------------------------------------\e[m"
    git commit --amend --no-edit
  fi
else
  printf '%b\n' "\e[0;35m-----------------------------------------------------------\e[m"
  git commit --amend "$@"
fi
