-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·83 lines (74 loc) · 2.46 KB
/
pre-commit
File metadata and controls
executable file
·83 lines (74 loc) · 2.46 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
#!/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)"
RUST_FMT_CFG="imports_granularity=Crate,group_imports=StdExternalCrate"
# Some sanity checking.
set -e
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
# Stash unstaged changes
if [[ "$1" != "all" ]]; then
stashdir="$(mktemp -d "$root"/.pre-commit.stashXXXXXX)"
msg="pre-commit stash @$(git rev-parse --short @) ${stashdir##*.stash}"
gitdir="$(git rev-parse --git-dir 2>/dev/null)"
stash() {
# Move MERGE_[HEAD|MODE|MSG] files to the root directory, and let `git stash push` save them.
find "$gitdir" -maxdepth 1 -name 'MERGE_*' -exec mv \{\} "$stashdir" \;
git stash push -k -u -q -m "$msg"
}
unstash() {
git stash list -1 --format="format:%s" | grep -q "$msg" && git stash pop -q
# Moves MERGE files restored by `git stash pop` back into .git/ directory.
if [[ -d "$stashdir" ]]; then
find "$stashdir" -exec mv -n \{\} "$gitdir" \;
rmdir "$stashdir"
fi
}
trap unstash EXIT
stash
fi
# Check formatting
if ! errors=($(cargo fmt -- --check --config "$RUST_FMT_CFG" -l)); then
echo "Formatting errors found in:"
for err in "${errors[@]}"; do
echo " $err"
done
echo "To fix, run \`cargo fmt -- --config $RUST_FMT_CFG\`"
exit 1
fi
check() {
msg="$1"
shift
if ! "$@"; then
echo "${msg}: Failed command:"
echo " ${@@Q}"
exit 1
fi
}
versions=(stable)
if [[ "$1" == "all" ]]; then
versions+=($(sed -ne 's/^rust-version *= *"//;T;s/".*$//;p' Cargo.toml))
fi
for v in "${versions[@]}"; do
check "clippy" cargo "+$v" clippy --tests -F stream
check "test" cargo "+$v" test -F stream
if [[ -n "$NSS_DIR" ]]; then
check "clippy(NSS)" cargo "+$v" clippy --tests --no-default-features --features nss,http,client,server,stream
check "test(NSS)" cargo "+$v" test --no-default-features --features nss,http,client,server,stream
fi
done