Skip to content

Instantly share code, notes, and snippets.

@PtaxLaine
Forked from zofrex/pre-commit
Last active October 7, 2018 03:54
Show Gist options
  • Save PtaxLaine/8107f50dbe6902c9c0890df6d2f38d26 to your computer and use it in GitHub Desktop.
Save PtaxLaine/8107f50dbe6902c9c0890df6d2f38d26 to your computer and use it in GitHub Desktop.
A git pre-commit hook to make sure your Rust code is properly formatted and the tests pass, so you never commit bad code
#!/bin/bash
check_char='\xE2\x9C\x93'
cross_char='\xE2\x9D\x8C'
green='\033[0;32m'
red='\033[0;31m'
nc='\033[0m'
check="$green$check_char$nc"
cross="$red$cross_char$nc"
errors=0
echo -n "Checking formatting... "
if diff=$(cargo fmt -- --check --color always 2>&1); then
echo -e "$check"
else
echo -e "$cross"
echo "Rust formatting checking failed. Show checking report? (y/n)"
read printrep
if [ "$printrep" == "y" ]; then
echo -e "$diff"
fi
errors=1
fi
echo -n "Running tests... "
if result=$(cargo test --color always 2>&1); then
echo -e "$check"
else
echo -e "$cross"
echo "$result"
errors=1
fi
if [ "$errors" != "0" ]; then
echo "Failed"
exit -1
else
echo "OK"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment