#!/bin/sh

PRE_COMMIT="pre-commit:"
NC=""
PREFIX="$PRE_COMMIT"
ERROR_PREFIX="$PRE_COMMIT"
SUCCESS_PREFIX="$PRE_COMMIT"

ERROR=false

########################
#### RUN PRE COMMIT ####
########################


# #### FORMAT ####
echo "running formatter..."
make fmt > /dev/null 2>&1
files=$(git diff --cached --name-only)
if ! [ -z "$files" ]; then
    git add $files
fi

#### LINT ####
echo "running linter..."
make lint > /dev/null 2>&1

if [ $? -ne 0 ]; then
    echo "$ERROR_PREFIX linting failed. Run 'make lint' for more information."
    ERROR=true
fi

# #### TEST ####
echo "running tests..."
make test > /dev/null 2>&1

if [ $? -ne 0 ]; then
    echo "$ERROR_PREFIX testing failed. Run 'make test' for more information."
    ERROR=true
fi

###################
####   EXIT    ####
###################

if [ "$ERROR" = true ]; then
    echo "$ERROR_PREFIX Commit aborted."
    exit 1
fi

exit $?
