From f59b8932f253c9ddc58939d47be3a83320ee0701 Mon Sep 17 00:00:00 2001 From: Ethan Uppal <113849268+ethanuppal@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:42:44 -0400 Subject: [PATCH] Add git hooks and setup (#2188) Automatically `cargo clippy` and others before you commit --- .hooks/pre-commit | 24 ++++++++++++++++++++++++ docs/intro.md | 5 +++++ setup_hooks.sh | 7 +++++++ 3 files changed, 36 insertions(+) create mode 100644 .hooks/pre-commit create mode 100644 setup_hooks.sh 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"