Created
March 12, 2021 06:27
-
-
Save the-bass/eebf9d2318f043267901adbc34971147 to your computer and use it in GitHub Desktop.
pre-commit hook for Rust projects
This file contains 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
#!/bin/sh | |
# Assigns stdin to keyboard - otherwise we're not able to listen to user input below. | |
exec < /dev/tty | |
message_prefix="[GIT HOOK | pre-commit]" | |
echo "$message_prefix Running clippy." | |
if ! cargo clippy | |
then | |
read -n 1 -s -p "$message_prefix clippy still has complaints. Press [f] to run --fix or [i] to continue anyway..."$'\n' pressed_key | |
if [ "$pressed_key" = "f" ]; then | |
cargo clippy --fix -Z unstable-options # Only available on the nightly channel. | |
echo $message_prefix" Exiting so you can add relevant fixes to the commit." | |
exit 1 | |
elif ! [ "$pressed_key" = "i" ]; then | |
exit 1 | |
fi | |
fi | |
echo "$message_prefix Running fmt." | |
if ! cargo fmt -- --check | |
then | |
read -n 1 -s -p "$message_prefix fmt still has complaints. Press [f] to run --fix or [i] to continue anyway..."$'\n' pressed_key | |
if [ "$pressed_key" = "f" ]; then | |
cargo fmt | |
echo $message_prefix" Exiting so you can add relevant changes to the commit." | |
exit 1 | |
elif ! [ "$pressed_key" = "i" ]; then | |
exit 1 | |
fi | |
fi | |
echo "$message_prefix Running tests." | |
if ! cargo test | |
then | |
read -n 1 -s -p "$message_prefix Some tests are still failing. Press [i] if you don't give a fuck..."$'\n' pressed_key | |
if ! [ "$pressed_key" = "i" ]; then | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment