-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit
executable file
·71 lines (60 loc) · 1.69 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
#!/bin/bash
function fail() {
echo "$@"
exit 1
}
tmpfile=/tmp/.effdumpprecommit
if test "$1" = install; then
ln -rs pre-commit .git/hooks/ || fail "error: pre-commit install failed."
echo "success: pre-commit installed."
exit
fi
allfiles="$(git diff-index --name-only --diff-filter=d --cached HEAD)"
gofiles="$(git diff-index --name-only --diff-filter=d --cached HEAD | grep '\.go$' || true)"
if test -z "$allfiles"; then
# No changed files, no checks to do.
exit
fi
if test -z "$noxx" && grep -Hni xx""x $allfiles; then
echo "Error: found xx""x comment, check if it was intended to commit, use noxx=1 envvar to skip."
exit 1
fi
if test -n "$gofiles"; then
# Go specific tests in this branch.
echo -n 'formatting...'
gofmt -l $gofiles >"$tmpfile" 2>&1
echo -en "\r\e[K"
if test -s "$tmpfile"; then
echo "Error: gofmt failed for these files:"
cat "$tmpfile"
exit 1
fi
echo -n 'linting...'
revive ./... >"$tmpfile" 2>&1
echo -en "\r\e[K"
if test -s "$tmpfile"; then
echo "Error: revive failed:"
cat "$tmpfile"
exit 1
fi
echo -n 'testing...'
go test ./... >"$tmpfile" 2>&1
result="$?"
echo -en "\r\e[K"
if test "$result" -ne 0; then
echo 'Error: tests failed, go test ./...:'
cat "$tmpfile"
exit 1
fi
fi
echo -n 'hashing...'
wanthash=$(cat internal/effdumptest/hash)
gothash=$(go run github.com/ypsu/effdump/internal/effdumptest hash)
echo -en "\r\e[K"
if test "$gothash" != "$wanthash"; then
echo "Error got hash $gothash, want $wanthash."
echo "$gothash" >internal/effdumptest/hash
echo "Expectation auto-updated, check 'go run github.com/ypsu/effdump/internal/effdumptest diff' and rerun git commit."
exit 1
fi
rm -f "$tmpfile"