diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..3e1e8c0169 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,14 @@ +repos: +- repo: local + hooks: + - id: clang-format + name: clang-format + entry: clang-format-10 -i --style=file + language: system + types_or: [c++, inc] + - id: copyright-year-checker + name: copyright-year-checker + entry: script/check_copyright_year.sh + verbose: false + language: script + types: [c++] diff --git a/README.md b/README.md index a45f61a37d..fd6f7e37c2 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,24 @@ make install Instructions for using CK as a pre-built kernel library are under [client_example](/client_example) +## Contributing + +When you contribute to Composable Kernel, make sure to run `clang-format` on all the changed files. We highly recommend using git hooks that are managed by the `pre-commit` framework. To install hooks, run: + +```bash +sudo script/install_precommit.sh +``` + +This way, `pre-commit` will add the appropriate hooks to your local repository and automatically run `clang-format` (and possibly additional checks) before any commit is created. + +If you need to uninstall hooks from the repository, you can do so by running the following command: + +```bash +script/uninstall_precommit.sh +``` + +If for any reason, you need to temporarily disable precommit hooks, you can add the `--no-verify` option to the `git commit` command. + ## Caveat ### Kernel Timing and Verification diff --git a/script/check_copyright_year.sh b/script/check_copyright_year.sh new file mode 100755 index 0000000000..f7709472ef --- /dev/null +++ b/script/check_copyright_year.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +current_year=$(date +%Y) +exit_code=0 + +for file in $@; do + if grep -q "Copyright (c)" $file + then + if ! grep -q "Copyright (c).*$current_year" $file + then + echo "ERROR: File $file has a copyright notice without the current year ($current_year)." + exit_code=1 + fi + fi +done + +exit $exit_code diff --git a/script/install_precommit.sh b/script/install_precommit.sh new file mode 100755 index 0000000000..daa5cb29ae --- /dev/null +++ b/script/install_precommit.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +run_and_check() { + "$@" + status=$? + if [ $status -ne 0 ]; then + echo "Error with \"$@\": Exited with status $status" + exit $status + fi + return $status +} + +echo "I: Installing tools required for pre-commit checks..." +run_and_check apt install clang-format-10 + +echo "I: Installing pre-commit itself..." +run_and_check pip3 install pre-commit +run_and_check pre-commit install + +echo "I: Installation successful." diff --git a/script/uninstall_precommit.sh b/script/uninstall_precommit.sh new file mode 100755 index 0000000000..b0d4d15166 --- /dev/null +++ b/script/uninstall_precommit.sh @@ -0,0 +1 @@ +pre-commit uninstall