-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·28 lines (24 loc) · 901 Bytes
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env bash
# Usage: copy this file to .git/hooks/
# Exit at first error
set -Eeu
# To handle partially committed files, we must copy the staged changes to a
# separate location
# See also https://stackoverflow.com/a/36793330
TEMPDIR=$(mktemp -d)
trap 'rm -rf "$TEMPDIR"' EXIT SIGHUP SIGINT SIGQUIT SIGTERM
git checkout-index --prefix="$TEMPDIR/" -af
# keep using the same target/ directory, not a new one in the temporary
# directory this avoids re-parsing everything from scratch every time we run
# the script
GIT_ROOT=$(git rev-parse --show-toplevel)
# lint Rust
if ! git diff --cached --name-only --diff-filter=AM --quiet -- src Cargo.toml Cargo.lock; then
pushd $TEMPDIR >/dev/null
export CARGO_TARGET_DIR="${GIT_ROOT}/target"
echo "Running cargo fmt"
cargo fmt --check
echo "Running cargo clippy"
cargo clippy --all -- --deny warnings
popd >/dev/null
fi