Pre-commit hooks (#2241)

* add pre-commit hook.
example of hook with fmt check.

* Add `CONTRIBUTING.md`.
This commit is contained in:
Romain Malmain 2024-05-28 11:45:13 +02:00 committed by GitHub
parent bce0f08294
commit 963afc3e5c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 13 deletions

9
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,9 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: local
hooks:
- id: fmt
name: fmt
entry: scripts/fmt_all.sh check
language: script

18
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,18 @@
# How to Contribute to LibAFL
For bugs, feel free to open issues or contact us directly. Thank you for your support. <3
## Pull Request guideline
Even though we will gladly assist you in finishing up your PR, try to
- keep all the crates compiling with *stable* rust (hide the eventual non-stable code under [`cfg`s](https://github.com/AFLplusplus/LibAFL/blob/main/libafl/build.rs#L26))
- run `cargo +nightly fmt` on your code before pushing
- check the output of `cargo clippy --all` or `./clippy.sh`
- run `cargo build --no-default-features` to check for `no_std` compatibility (and possibly add `#[cfg(feature = "std")]`) to hide parts of your code.
Some of the parts in this list may be hard, don't be afraid to open a PR if you cannot fix them by yourself, so we can help.
### Pre-commit hooks
Some of these checks can be performed automatically during commit using [pre-commit](https://pre-commit.com/).
Once the package is installed, simply run `pre-commit install` to enable the hooks, the checks will run automatically before the commit becomes effective.

View File

@ -116,15 +116,7 @@ The best-tested fuzzer is [`./fuzzers/libfuzzer_libpng`](./fuzzers/libfuzzer_lib
## Contributing
For bugs, feel free to open issues or contact us directly. Thank you for your support. <3
Even though we will gladly assist you in finishing up your PR, try to
- keep all the crates compiling with *stable* rust (hide the eventual non-stable code under [`cfg`s](https://github.com/AFLplusplus/LibAFL/blob/main/libafl/build.rs#L26))
- run `cargo +nightly fmt` on your code before pushing
- check the output of `cargo clippy --all` or `./clippy.sh`
- run `cargo build --no-default-features` to check for `no_std` compatibility (and possibly add `#[cfg(feature = "std")]`) to hide parts of your code.
Some of the parts in this list may be hard, don't be afraid to open a PR if you cannot fix them by yourself, so we can help.
Please check out [CONTRIBUTING.md](CONTRIBUTING.md) for the contributing guideline.
## Cite

View File

@ -10,6 +10,10 @@ if ! command -v parallel > /dev/null; then
exit 1
fi
if [ "$1" = "check" ]; then
CHECK=1
fi
# Find main rust crates
CRATES_TO_FMT=$(find "$LIBAFL_DIR" -type d \( -path "*/fuzzers/*" -o -path "*/target/*" -o -path "*/utils/noaslr" -o -path "*/utils/gdb_qemu" -o -path "*/docs/listings/baby_fuzzer/listing-*" \) -prune \
-o -name "Cargo.toml" -print \
@ -21,15 +25,30 @@ CRATES_TO_FMT+=$(find "$LIBAFL_DIR/fuzzers" "$LIBAFL_DIR/fuzzers/backtrace_baby_
echo "Welcome to the happy fmt script. :)"
if [ "$CHECK" ]; then
echo "Running fmt in check mode."
CARGO_FLAGS="--check"
CLANG_FLAGS="--dry-run"
fi
echo "[*] Formatting Rust crates..."
echo "$CRATES_TO_FMT" | parallel "echo '[*] Running fmt for {}'; cargo +nightly fmt $CARGO_FLAGS --manifest-path {}"
if ! echo "$CRATES_TO_FMT" | parallel --halt-on-error 1 "echo '[*] Running fmt for {}'; cargo +nightly fmt $CARGO_FLAGS --manifest-path {}"
then
echo "Rust format failed."
exit 1
fi
if command -v clang-format-18 > /dev/null; then
echo "[*] Formatting C(pp) files"
# shellcheck disable=SC2046
clang-format-18 "$CLANG_FLAGS" -i --style=file $(find . -type f \( -name '*.cpp' -o -iname '*.hpp' -o -name '*.cc' -o -name '*.cxx' -o -name '*.cc' -o -name '*.c' -o -name '*.h' \) | grep -v '/target/' | grep -v 'libpng-1\.6\.37' | grep -v 'stb_image\.h' | grep -v 'dlmalloc\.c' | grep -v 'QEMU-Nyx')
C_FILES=$(find "$LIBAFL_DIR" -type f \( -name '*.cpp' -o -iname '*.hpp' -o -name '*.cc' -o -name '*.cxx' -o -name '*.cc' -o -name '*.c' -o -name '*.h' \) | grep -v '/target/' | grep -v 'libpng-1\.6\.37' | grep -v 'stb_image\.h' | grep -v 'dlmalloc\.c' | grep -v 'QEMU-Nyx')
if ! clang-format-18 "$CLANG_FLAGS" -i --style=file "$C_FILES"
then
echo "C(pp) format failed."
exit 1
fi
else
echo "Warning: clang-format-18 not found. C(pp) files formatting skipped."
fi
echo "[*] Done :)"