#!/bin/sh
#
# Pre-commit hooks.
#

# Exit on any error.
#set -e

# We'll only runchecks on changes that are a part of this commit.
# So let's stash others.
git stash -q --keep-index

# First, we check code quality.
echo 'Checking PHP code style...'
vendor/bin/phpcs -p --standard=PSR2 src tests
PHPCS_EC=$?

# Then, we run tests.
echo 'Running tests...'
vendor/bin/phpunit
PHPUNIT_EC=$?

# We're done with checks, we can unstash changes.
git stash pop -q

# Exit if any error codes.
let "ERROR = $PHPCS_EC + $PHPUNIT_EC"
if [ "${ERROR}" -ne "0" ]
then
  echo "Commit aborted."
  exit ${ERROR}
fi

echo "All good!"
