-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpre-commit.sh
More file actions
executable file
·60 lines (53 loc) · 1.27 KB
/
pre-commit.sh
File metadata and controls
executable file
·60 lines (53 loc) · 1.27 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
#!/usr/bin/env bash
# pre-commit hook for td
# Install: make install-hooks (or: ln -sf ../../scripts/pre-commit.sh .git/hooks/pre-commit)
set -euo pipefail
PASS=0
FAIL=0
echo "🪡 pre-commit checks"
# --- gofmt: only staged .go files ---
STAGED_GO=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' || true)
if [[ -n "$STAGED_GO" ]]; then
printf " %-20s" "gofmt"
UNFORMATTED=$(echo "$STAGED_GO" | xargs gofmt -l 2>&1)
if [[ -z "$UNFORMATTED" ]]; then
echo "✓"
PASS=$((PASS+1))
else
echo "✗ FAILED — run: gofmt -w ."
echo "$UNFORMATTED" | sed 's/^/ /'
FAIL=$((FAIL+1))
fi
else
printf " %-20s" "gofmt"
echo "– (no .go files staged)"
fi
# --- go vet ---
printf " %-20s" "go vet"
VET_OUT=$(go vet ./... 2>&1)
if [[ $? -eq 0 ]]; then
echo "✓"
PASS=$((PASS+1))
else
echo "✗ FAILED"
echo "$VET_OUT" | sed 's/^/ /'
FAIL=$((FAIL+1))
fi
# --- go build ---
printf " %-20s" "go build"
BUILD_OUT=$(go build ./... 2>&1)
if [[ $? -eq 0 ]]; then
echo "✓"
PASS=$((PASS+1))
else
echo "✗ FAILED"
echo "$BUILD_OUT" | sed 's/^/ /'
FAIL=$((FAIL+1))
fi
echo ""
if [[ $FAIL -gt 0 ]]; then
echo "❌ $FAIL check(s) failed. Fix issues or use --no-verify to skip."
exit 1
else
echo "✅ All checks passed ($PASS)"
fi