-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·60 lines (50 loc) · 1.28 KB
/
pre-commit
File metadata and controls
executable file
·60 lines (50 loc) · 1.28 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
#!/bin/bash
function fail() {
echo "$@"
exit 1
}
tmpfile=/tmp/.textarprecommit
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
rm -f "$tmpfile"