#!/usr/bin/env bash


# Check if pycodestyle is installed
if ! command -v pycodestyle &> /dev/null ; then
    echo "PEP8 checking is not available. Please run:"
    echo "    pip install pycodestyle"
    exit 1
fi

# This commit hook forces PEP8 compliance for any py files about to be committed.

modified_files="$(git diff --cached --name-only --diff-filter=ACM)"
modified_files="$(find $modified_files -name '*.py')"
if [ -z "$modified_files" ]; then
    exit 0
fi

# The following error codes are ignored in the check:

# E731 do not assign a lambda expression, use a def
# W291 trailing whitespace
# W503 line break before binary operator
# W504 line break after binary operator

ignore_codes='E731,W291,W503,W504'
pycodestyle --ignore $ignore_codes --max-line-length 100 $modified_files

if [ $? -eq 0 ]; then
    exit 0
else
    echo
    echo 'PEP8 style violations have been detected above - please fix them.'
    echo 'This check can be skipped with the --no-verify flag.'
    exit 1
fi