-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre-commit
executable file
·71 lines (59 loc) · 1.84 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
# This is a pre-commit hook that validates code formatting.
#
# Install this by running the script with an argument of "install",
# which installs a symlink to .git/hooks/precommit:
# $ ln -s ../../hooks/pre-commit .git/hooks/pre-commit
root="$(git rev-parse --show-toplevel 2>/dev/null)"
set -e
# Some sanity checking.
hash cargo
[[ -n "$root" ]]
# Installation.
if [[ "$1" == "install" ]]; then
hook="$root"/.git/hooks/pre-commit
if [[ ! -e "$hook" ]]; then
ln -s ../../pre-commit "$hook"
echo "Installed git pre-commit hook at $hook"
else
echo "Hook already installed"
fi
exit
fi
# Check formatting.
stashfile=$(mktemp .pre-commit.stashXXXXXX)
trap 'set +e;git stash pop -q; rm -f "$stashfile"' EXIT
git stash push -k -u -q -m "pre-commit stash"
if ! errors=($(cargo fmt -- --check -l)); then
echo "Formatting errors found."
echo "Run \`cargo fmt\` to fix the following files:"
for err in "${errors[@]}"; do
echo " $err"
done
exit 1
fi
error() {
echo "-----------------------"
echo "$@"
exit 1
}
cargo clippy --tests -- -D warnings --D clippy::pedantic || error "Clippy errors"
cargo test || error "Test failures"
VENV="$root/.venv/bin"
pyrun() {
x="$1"
shift
if ! [[ -d "$VENV" ]]; then
python3 -m venv "${VENV%/*}"
fi
if ! [[ -x "$VENV/$x" ]]; then
"$VENV/pip" install -r "$root/requirements.txt"
elif [[ "$root/requirements.txt" -nt "$VENV/$x" ]]; then
"$VENV/pip" install --upgrade -r "$root/requirements.txt"
fi
echo "Run: $x $@"
"$x" "$@"
}
pyrun flake518 . || error "Python flake8 formatting errors."
pyrun black --check --diff . || error "Python Black formatting errors. Run \`black .\` to fix."
pyrun isort --check --diff . || error "Python isort import ordering errors. Run \`isort .\` to fix."