-
-
Save de1o/6193307d55a545fbb4105c5ddd2d889d to your computer and use it in GitHub Desktop.
git hook for pre-commit to use ESLint and flake8 as a soft guard of code quality (lets things pass if the committer is committed to committing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Uses ESLint as a soft guard of code quality for your repo | |
# allows commits to go through if re-attempted within a pre-set interval | |
# copy to .git/hooks/commit-msg to have things work | |
# Based on: https://gist.github.com/wesbos/8aec9d2ff7f7cf9dd65ca2c20d5dfc23 | |
PRESET_TIME_INTERVAL=20 | |
LAST_RUN_DATE_FILE="$HOME/.eslint.commit-msg-hook.last-failed-run" | |
if [ ! -f $LAST_RUN_DATE_FILE ] ; then | |
echo "0" > $LAST_RUN_DATE_FILE | |
fi | |
# because somehow /usr/local/bin might not be on the path | |
PATH=$PATH:/usr/local/bin:/usr/local/sbin | |
CURR_EPOCH=$(date +%s) | |
LAST_RUN_EPOCH=$(cat $LAST_RUN_DATE_FILE) | |
TIME_DIFFERENCE_IN_SEC=$(expr $CURR_EPOCH - $LAST_RUN_EPOCH) | |
function eslint_js { | |
files=$(git diff --cached --name-only | grep -E '(.js|.jsx)$') | |
eslint_failed=0 | |
# Prevent ESLint help message if no files matched | |
if [[ $files != "" ]] ; then | |
for file in ${files}; do | |
git show :$file | eslint $file | |
if [[ $? != 0 ]] ; then | |
eslint_failed=1 | |
fi | |
done; | |
fi | |
} | |
function flake8_py { | |
files=$(git diff --cached --name-only | grep '\.py\?$') | |
flake8_failed=0 | |
# Prevent ESLint help message if no files matched | |
if [[ $files != "" ]] ; then | |
for file in ${files}; do | |
git show :$file | flake8 $file | |
if [[ $? != 0 ]] ; then | |
flake8_failed=1 | |
fi | |
done; | |
fi | |
} | |
flake8_py; | |
eslint_js; | |
if [[ eslint_failed -gt 0 || flake8_failed -gt 0 ]] ; then | |
failed=1 | |
else | |
failed=0 | |
fi | |
if [[ $failed != 0 ]] ; then | |
echo "💩 💩 💩 ESLint or flake8 failed." | |
if [ "$TIME_DIFFERENCE_IN_SEC" -ge $PRESET_TIME_INTERVAL ]; then | |
echo "🚫 🚫 🚫 git commit denied!" | |
echo "Attempt to commit again within $PRESET_TIME_INTERVAL seconds and the commit will succeed."; | |
echo "... and please ensure that there are no syntax errors if you do so."; | |
date +%s > $LAST_RUN_DATE_FILE | |
exit $failed | |
else | |
echo "Current commit attempt is within $PRESET_TIME_INTERVAL seconds of last failed attempt."; | |
echo "😡 😡 😡 git commit reluctantly allowed."; | |
echo "0" > $LAST_RUN_DATE_FILE | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment