forked from martinthomson/ohttp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre-commit
executable file
·53 lines (49 loc) · 1.43 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
#!/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)"
# Some sanity checking.
hash cargo || exit 1
[[ -n "$root" ]] || exit 1
# 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.
if [[ "$1" != "all" ]]; then
msg="pre-commit stash @$(git rev-parse --short @) $RANDOM"
trap 'git stash list -1 --format="format:%s" | grep -q "'"$msg"'" && git stash pop -q' EXIT
git stash push -k -u -q -m "$msg"
fi
if ! errors=($(cargo fmt -- --check --config imports_granularity=crate -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
if ! cargo clippy --tests; then
exit 1
fi
if ! cargo test; then
exit 1
fi
if [[ -n "$NSS_DIR" ]]; then
if ! cargo clippy --tests --no-default-features --features nss; then
exit 1
fi
if ! cargo test --no-default-features --features nss; then
exit 1
fi
fi