Skip to content

Instantly share code, notes, and snippets.

@GRAYgoose124
Last active October 20, 2022 06:16
Show Gist options
  • Save GRAYgoose124/24be6efbe34a9a5a079692d4b8666bf9 to your computer and use it in GitHub Desktop.
Save GRAYgoose124/24be6efbe34a9a5a079692d4b8666bf9 to your computer and use it in GitHub Desktop.
Cargo CI git hook, place in <repo>/.git/hooks/ as `pre-commit`.
#!/bin/bash
# From https://deaddabe.fr/blog/2021/09/29/git-pre-commit-hook-for-rust-projects/
set -eu
TODO="$(git rev-parse --show-toplevel)/TODO"
if [ -s $TODO ]
then
if [ -z $(cat $TODO | grep "TODO_RECHECK") ]
then
echo "TODO_RECHECK" >> $TODO
exit 1
else
rm $TODO
fi
fi
fmt=$(cargo fmt --all -- --check)
result=$?
if [[ ${result} -ne 0 ]]; then
echo "There are some code style issues."
echo "Run 'cargo fmt -- --all.'" >> $TODO
exit 1
else
echo "Code style is good."
fi
clippy_stderr=$(cargo clippy --all-targets -- -Dwarnings)
result=$?
if [[ ${result} -ne 0 ]]; then
echo "There are some clippy issues."
echo "Run 'cargo clippy --all-targets --'" >> $TODO
exit 1
else
echo "Clippy is good."
fi
gentest=$(cargo test --all)
result=$?
if [[ ${result} -ne 0 ]]; then
echo "There are some test issues."
echo "Run 'cargo test'" >> $TODO
exit 1
else
echo "Tests are good."
fi
vmtest=$(cargo test -p vm6502)
result=$?
if [[ ${result} -ne 0 ]]; then
echo "There are some issues with the vm6502 tests."
echo "Run 'cargo test -p vm6502'" >> $TODO
exit 1
else
echo "vm6502 tests are good."
fi
diffcheck=$(git diff --cached --shortstat)
if [ -n "$diffcheck" ]
then
exit 0
else
echo "Nothing staged, did you forget 'git add'?"
exit 1
fi
function usercontinue() {
#This hooks prevents unintentional committing to a branch.
#Customize PATTERN to suit your needs.
PATTERN="master"
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ $BRANCH = *$PATTERN* ]]; then
echo "----------------------------------------------------------------"
echo "WARNING: You are committing to the master branch:" $BRANCH
echo "----------------------------------------------------------------"
else
exit 0
fi
while true; do
read -p "Do you really want to commit? y/n " yn < /dev/tty
case $yn in
[Yy]* ) exit 0; break;;
[Nn]* ) exit 1;;
* ) echo "Please answer y or n.";;
esac
done
exit 1
}
usercontinue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment