Skip to content

Instantly share code, notes, and snippets.

@ozwaldorf
Last active December 9, 2023 12:48
Show Gist options
  • Save ozwaldorf/22dc681cef0fe7f13b66890b74dbb588 to your computer and use it in GitHub Desktop.
Save ozwaldorf/22dc681cef0fe7f13b66890b74dbb588 to your computer and use it in GitHub Desktop.
Rust Git Hook
#!/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
@ozwaldorf
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment