Skip to content

Instantly share code, notes, and snippets.

@R1j1t
Last active April 19, 2024 22:32
Show Gist options
  • Save R1j1t/933cbf34e0054a4dfbcacb9fc5ed6206 to your computer and use it in GitHub Desktop.
Save R1j1t/933cbf34e0054a4dfbcacb9fc5ed6206 to your computer and use it in GitHub Desktop.
Pre-commit hook for python repo: Instead of getting to know whether any lint checks failed in CI, I created this pre-commit hook in git for my python repo. Git Hooks is a powerful utility. You read from the official documentation: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks. Please ⭑ this Gist if you find it useful! Feel free to co…
#!/bin/sh
# This is a pre-commit hook for python. It checks if black is passed and then if flake8 is passed.
# Feel free to share your updates. It will help use these git feature!
if black . --check; then
echo "Everything is all right in black"
else
echo ""
echo "Auto correcting with black"
echo ""
black .
echo ""
echo ""
echo "Error in above files removed. plese try commit again"
exit 1
fi
fs=$(flake8 . --count --exit-zero --statistics)
echo $fs
# echo ${fs:${#f2}-1:${#fs}}
if [ "${fs:${#f2}-1:${#fs}}" = "0" ]; then
echo "Everything all right flake8"
else
echo "flake8 issue found"
echo run flake8 . --count --exit-zero --statistics
echo thanks to git hooks
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment