running flake8 in git’s pre-commit
Posted on Sun 23 October 2011 in software.
I’m forever forgetting to remove debug statements or to run flake8 on my source code. Not to panic though, git hooks to the rescue!
You can just put pre-commit into $PROJECT_DIR/.git/hooks, chmod +x it and be away.
This is the useful part of the file;
ret_code=0
for file in $(git diff --name-only --cached $against); do
if [[ -n "$(grep 'DEBUGS' $file)" ]]; then
echo "DEBUGS in file $file"
ret_code=1
fi
if [[ "${file##*.}" == "py" ]]; then
flake8 $file
if [[ $? != 0 ]]; then
ret_code=1
fi
fi
done
exit $ret_code
$against is the commit to check against (taken from the pre-commit.sample file).
DEBUGS is the comment tag I use to denote debug statements, which is used by geany in the Task addons plugin to list any occurrences of it in the file (along with TODO and FIXME comments)