diff --git a/.hooks/pre-commit b/.hooks/pre-commit new file mode 100644 index 0000000000..218bdaf7c1 --- /dev/null +++ b/.hooks/pre-commit @@ -0,0 +1,24 @@ +#/bin/sh + +function check { + prog=$1 + shift + "$prog" "$@" + if [ $? -ne 0 ]; then + exit 1 + fi +} + +check cargo clippy + +staged_py_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$') + +if [ -n "$staged_py_files" ]; then + flake8_executable=$(which flake8 || which flake) + if [ -z "$flake8_executable" ]; then + echo "flake is not installed" + exit 1 + else + check "$flake8_executable" $staged_py_files + fi +fi diff --git a/docs/intro.md b/docs/intro.md index 5988c96758..447f2cd20c 100644 --- a/docs/intro.md +++ b/docs/intro.md @@ -56,6 +56,11 @@ cargo build # Builds the compiler ./target/debug/calyx --help # Executes the compiler binary ``` +We recommend installing the git hooks to run linting and formatting checks before each commit: +```shell +/bin/sh setup_hooks.sh +``` + ## Running Core Tests The core test suite tests the Calyx compiler's various passes. diff --git a/setup_hooks.sh b/setup_hooks.sh new file mode 100644 index 0000000000..a087a6e5df --- /dev/null +++ b/setup_hooks.sh @@ -0,0 +1,7 @@ +#/bin/sh + +for hook in .hooks/*; do + hook_name=$(basename "$hook") + cp "$hook" ".git/hooks/$hook_name" && chmod +x ".git/hooks/$hook_name" +done +echo "Done"