-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit-hook.sh
More file actions
52 lines (42 loc) · 1.37 KB
/
pre-commit-hook.sh
File metadata and controls
52 lines (42 loc) · 1.37 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
#!/bin/bash
# Go Code Style Pre-commit Hook
# This script runs the Go linter before each commit
set -e
echo "🔍 Running Go code style checks..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check if golinter binary exists
if ! command -v golinter &> /dev/null; then
echo -e "${YELLOW}⚠️ golinter not found...${NC}"
echo "please run command : go install github.com/yaza-putu/golinter@latest"
fi
# Check if config file exists
if [ ! -f ".go.linter.json" ]; then
echo -e "${YELLOW}⚠️ .go.linter.json not found. Generating default config...${NC}"
golinter init
fi
# Get staged Go files
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' || true)
if [ -z "$STAGED_GO_FILES" ]; then
echo -e "${GREEN}✅ No Go files to check${NC}"
exit 0
fi
echo -e "${YELLOW}📂 Staged Go files:${NC}"
echo "$STAGED_GO_FILES" | while read file; do
echo " - $file"
done
echo
# Run the linter on the project
echo -e "${YELLOW}🔍 Running linter...${NC}"
if golinter lint .; then
echo -e "${GREEN}✅ All code style checks passed!${NC}"
exit 0
else
echo -e "${RED}❌ Code style checks failed!${NC}"
echo -e "${YELLOW}💡 Please fix the issues above before committing.${NC}"
echo -e "${YELLOW}💡 You can customize the rules in .go.linter.json${NC}"
exit 1
fi