Skip to content

Instantly share code, notes, and snippets.

@matan7890
Created July 10, 2022 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matan7890/cad40abe5bb82e90c9e5a3cbaeccbf33 to your computer and use it in GitHub Desktop.
Save matan7890/cad40abe5bb82e90c9e5a3cbaeccbf33 to your computer and use it in GitHub Desktop.
git pre-commit hook for lint-checking
#!/bin/sh
# Place me in ".git/hooks", replacing the original file if one exists.
# Stashing all non-staged changes so the lint will only run on staged changes.
# Assuming the already-commited code is always linted before commiting,
# this code assures that the upcoming commits will also be linted.
git stash -k -u > /dev/null
# This code assumes here you have some `make` commands for checking/reporting/fixing the linting of the code.
# You can change these to the commands of your choice.
# It is recommended to NOT lint the code automatically, but let the user decide what
# to do if the code is not linted to prevent automatically-added bugs
# and to allow the `git stash pop` command to always succeed.
if ! make check-lint; then
make report-lint
echo "Can't commit changes - there are lint errors. Run 'make fix-lint' to fix issues and try again." >&2
EXIT_CODE=1
else
EXIT_CODE=0
fi
git stash pop > /dev/null
exit $EXIT_CODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment