Add linting pre-commit hook and hook install script #86

Merged
merenber merged 6 commits from feature-80 into master 2023-05-29 00:14:06 -04:00
2 changed files with 35 additions and 33 deletions
Showing only changes of commit 42a74c2bc9 - Show all commits

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".py\{0,1\}$")
@ -6,36 +6,7 @@ if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
PASS=true
docker run --rm -v "$PWD:$PWD:z" -w "$PWD" -e "STAGED_FILES=$STAGED_FILES" python:3.7-buster sh -c './lint-docker.sh "$STAGED_FILES"'
RUN_STATUS=$?
# Check for flake8
which flake8 &> /dev/null
if [[ "$?" == 1 ]]; then
echo -e "\t\033[41mPlease install flake8\033[0m"
exit 1
fi
echo -e "\nLinting Python with Flake8:\n"
for FILE in $STAGED_FILES
do
flake8 "$FILE"
if [[ "$?" == 0 ]]; then
echo -e "\t\033[32mPassed: $FILE\033[0m"
else
echo -e "\t\033[41mFailed: $FILE\033[0m"
PASS=false
fi
done
echo -e "\nPython linting complete!\n"
if ! $PASS; then
echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass flake8 but do not. Please fix the flake8 errors and try again.\n"
exit 1
else
echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?
exit $RUN_STATUS

31
lint-docker.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
. venv/bin/activate
STAGED_FILES="$1"
echo "STAGED_FILES: $STAGED_FILES"
echo -e "\nLinting Python files with Flake8:\n"
PASS=true
while IFS= read -r FILE; do
flake8 "$FILE"
if [[ "$?" != 0 ]]; then
PASS=false
fi
# echo "Linted $FILE"
done <<< "$STAGED_FILES"
echo -e "\nPython linting complete!\n"
if ! $PASS; then
echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass flake8 but do not. Please fix the flake8 errors and try again.\n"
exit 1
else
echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
exit $?