#!/usr/bin/env bash
set -uo pipefail
IFS=$'\n\t'

#
# Improvements from dahjelle/pre-commit.sh:
# - does not lint deleted files,
# - lints all staged files before exiting with an error code,
# - handles spaces and other unusual chars in file names.
#
# Based also on @jancimajek's one liner in that Gist.
#

# ESLint staged changes only
git diff --diff-filter=d --cached --name-only -z -- '*.js' '*.jsx' \
  | xargs -0 -I % sh -c 'git show ":%" | npx eslint --stdin --stdin-filename "%";'
eslint_exit=$?

if [ ${eslint_exit} -eq 0 ]; then
  echo "✓ ESLint passed"
else
  echo "✘ ESLint failed!" 1>&2
  exit ${eslint_exit}
fi

# Solhint staged changes only
git diff --diff-filter=d --cached --name-only -z -- '*.sol' \
  | xargs -0 -I % sh -c 'git show ":%" | npx solium --stdin;'
solhint_exit=$?

if [ ${solhint_exit} -eq 0 ]; then
  echo "✓ Solhint passed"
else
  echo "✘ Solhint failed!" 1>&2
  exit ${solhint_exit}
fi
