| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Copy me to .git/hooks/pre-commit |
| 4 | |
| 5 | set -euo pipefail |
| 6 | |
| 7 | tmpdir= |
| 8 | cleanup_tmpdir() { |
| 9 | if [[ "$tmpdir" && -e "$tmpdir" ]];then |
| 10 | rm -rf "$tmpdir" |
| 11 | fi |
| 12 | } |
| 13 | trap cleanup_tmpdir EXIT |
| 14 | |
| 15 | # Check out the git index (what's about to be committed) in a temporary |
| 16 | # directory & run the supplied command there. |
| 17 | in_git_index_in_tmpdir() { |
| 18 | tmpdir=$(mktemp -d) |
| 19 | [[ "$tmpdir" && -d "$tmpdir" ]] |
| 20 | start_index=$(sha256sum "${GIT_INDEX_FILE:-.git/index}") |
| 21 | git checkout-index --prefix="$tmpdir/" -a |
| 22 | pushd "$tmpdir" |
| 23 | "$@" |
| 24 | popd |
| 25 | end_index=$(sha256sum "${GIT_INDEX_FILE:-.git/index}") |
| 26 | if [[ "$start_index" != "$end_index" ]];then |
| 27 | echo "Index changed while pre-commit tests were running. Aborting!" |
| 28 | exit 1 |
| 29 | fi |
| 30 | } |
| 31 | |
| 32 | verify() { |
| 33 | cargo test --offline |
| 34 | cargo clippy -- -D warnings -W clippy::pedantic |
| 35 | rustfmt --check src/*.rs |
| 36 | } |
| 37 | |
| 38 | in_git_index_in_tmpdir verify |