Skip to content

Instantly share code, notes, and snippets.

@SJShaw
Last active June 14, 2024 11:27
Show Gist options
  • Save SJShaw/727a64d78b7c1b602ecc897cb0509ad8 to your computer and use it in GitHub Desktop.
Save SJShaw/727a64d78b7c1b602ecc897cb0509ad8 to your computer and use it in GitHub Desktop.
pre-commit hook
#!/bin/bash
echo "Checking for blockers..."
for line in `git diff --cached --name-only --diff-filter=ACM | grep [.]py$`; do
grep -i "do not commit" $line > /dev/null 2>&1
if [ "$?" == "0" ] ; then
echo "Aborting commit due to 'do not commit' entry in file $line"
exit 1
fi
done
echo "Checking for debug prints..."
for line in `git diff --cached --name-only --diff-filter=ACM | grep [.]py$ | grep -vE '(test|integration)_.*\.py$'`; do
git diff --cached $line | grep "^+" | grep "print(" > /dev/null 2>&1
if [ "$?" == "0" ] ; then
echo "Aborting commit due to 'print()' entry in file $line"
exit 1
fi
done
echo "Checking for trailing whitespace..."
git diff --cached --check --color | grep -v EOF | grep 'py:'
if [ "$?" == "0" ] ; then
echo "Aborting commit due to trailing whitespace"
exit 1
fi
echo "Checking for hardcoded local paths..."
for line in `git diff --cached --name-only --diff-filter=ACM`; do
grep -e "/$USER/" -e "/home/" -e "/Users/" $line > /dev/null 2>&1
if [ "$?" == "0" ] ; then
echo "Aborting commit due to hardcoded paths in file $line"
exit 1
fi
done
echo "Checking for errors with pylint..."
ESCALATE_TO_ERRORS="--fail-on=unused-variable,unused-argument,comparison-with-itself,self-assigning-variable"
PYLINT_OPTS="--reports=n --score=n --disable=all --enable=E $ESCALATE_TO_ERRORS"
if [ ! -z "$PRE_COMMIT_PREVIOUS_COMMIT" ] ; then
files="$(git show --pretty="" --name-only)"
else
files="$(git diff --cached --name-only --diff-filter=ACM | grep [.]py$)"
fi
if [ ! -z "$files" ] ; then
# the grep is necessary because files can re-enable warnings
errors="$(pylint $PYLINT_OPTS $files | grep -v "'Config' has no")"
if [ ! -z "$errors" ] ; then
echo "Aborting commit due to pylint reporting errors"
echo "$errors"
exit 1
fi
fi
echo "Checking for errors with mypy..."
mypy run_antismash.py antismash
if [ "$?" == "1" ] ; then
echo "Aborting commit due to mypy reporting errors"
exit 1
fi
echo "Running unit tests..."
make > /dev/null 2>&1
if [ "$?" != "0" ] ; then
echo "Aborting commit due to unit test failures"
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment