Last active
December 9, 2023 12:48
-
-
Save ozwaldorf/22dc681cef0fe7f13b66890b74dbb588 to your computer and use it in GitHub Desktop.
Rust Git Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
HOOK="$(basename "$0")" | |
FILETYPE=".rs\|Cargo.toml\|Cargo.lock" | |
# comment and uncomment the steps that should be ran depending on commit/push/etc | |
STEPS=( | |
"cargo fmt --check" | |
"cargo clippy --all-features --all-targets -- -Dclippy::all -Dwarnings" | |
"cargo test --all" | |
) | |
FILES="$(git diff --cached --name-only "@{u}") | grep $FILETYPE" | |
if [[ "$FILES" ]]; then | |
printf "Running %s hook...\n\n" "$HOOK" | |
for cmd in "${STEPS[@]}"; do | |
printf "%s\n" "$cmd" | |
if ! $cmd; then | |
printf "\033[0;31m\nFailed during:\n\t%s\n\033[0m" "$cmd" >&2 | |
exit 1 | |
fi | |
done | |
printf "\033[0;32m\nAll checks passed!\n\n\033[0m" | |
else | |
printf "\033[0;32mSkipping %s hook, no rust changes\n\n\033[0m" "$HOOK" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Git hook to check fmt, clippy, and tests. Only runs if the diff between the cached commits and the current head contains rust files, otherwise skips.