Skip to content

Instantly share code, notes, and snippets.

@the-bass
Created March 12, 2021 06:27
Show Gist options
  • Save the-bass/eebf9d2318f043267901adbc34971147 to your computer and use it in GitHub Desktop.
Save the-bass/eebf9d2318f043267901adbc34971147 to your computer and use it in GitHub Desktop.
pre-commit hook for Rust projects
#!/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