#!/bin/bash

echo "Running pre commit codestyle check"
# Find all Python files in the commit. ACM is for "added, created, modified"
# First find all the cached files, than all the uncached files
python_files=($(git diff --cached --name-only --diff-filter=ACM -- '*.py'))
python_files+=($(git diff --name-only --diff-filter=ACM -- '*.py'))
[[ ${#python_files[*]} -eq 0 ]] && exit 0

#Remove duplicates from the list
python_files=$(echo ${python_files[*]} | tr ' ' '\n' | sort | uniq | tr '\n' ' ')

# Run pycodestyle on each Python file
if ! pycodestyle ${python_files}; then
    echo "ERROR: some files aren't styled properly"
    exit 1
fi

exit 0
