Skip to content

Instantly share code, notes, and snippets.

@607011
Created March 20, 2025 05:18
Show Gist options
  • Save 607011/d7b5ceae3751e46a30157d983a9f6bf9 to your computer and use it in GitHub Desktop.
Save 607011/d7b5ceae3751e46a30157d983a9f6bf9 to your computer and use it in GitHub Desktop.
git pre-commit hook for Rust projects
#!/bin/sh
# Run cargo fmt check
echo "Running cargo fmt check ..."
cargo fmt --all -- --check
if [ $? -ne 0 ]; then
echo "cargo fmt check failed! Fix the errors before committing."
exit 1
fi
# Run cargo clippy
echo "Running cargo clippy ..."
cargo clippy -- -D warnings
if [ $? -ne 0 ]; then
echo "cargo clippy failed! Fix the errors and warnings before committing."
exit 1
fi
# Run cargo test
echo "Running cargo test ..."
cargo test --all-features
if [ $? -ne 0 ]; then
echo "cargo test failed! Investigate the failed tests before committing."
exit 1
fi
# If all checks pass, allow the commit
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment