-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
292 lines (236 loc) · 9.49 KB
/
pre-commit
File metadata and controls
292 lines (236 loc) · 9.49 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
# Pre-commit hook for code quality checks
# Runs custom commands from .dev-hooks.yml (lint, type check, format, etc.)
# Optionally filtered by file types
# Colors optimized for macOS Terminal
RED='\x1b[31m'
GREEN='\x1b[32m'
YELLOW='\x1b[33m'
BLUE='\x1b[34m'
MAGENTA='\x1b[35m'
CYAN='\x1b[36m'
WHITE='\x1b[37m'
BOLD='\x1b[1m'
DIM='\x1b[2m'
NC='\x1b[0m'
# Get project root (where .git is located)
PROJECT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -z "$PROJECT_ROOT" ]; then
echo -e "${RED}Error: Not a git repository${NC}"
exit 1
fi
CONFIG_FILE="$PROJECT_ROOT/.dev-hooks.yml"
# ============================================================================
# YAML Parser (simple, no dependencies)
# ============================================================================
yaml_get() {
local key="$1"
local file="$2"
if [ ! -f "$file" ]; then
echo ""
return
fi
if [[ "$key" == *"."* ]]; then
local parent="${key%%.*}"
local child="${key#*.}"
awk -v parent="$parent" -v child="$child" '
$0 ~ "^"parent":" { in_section=1; next }
in_section && /^[a-zA-Z]/ { in_section=0 }
in_section && $0 ~ "^ "child":" {
gsub(/^ [a-zA-Z_-]+:[ ]*/, "")
gsub(/^[ \t]+|[ \t]+$/, "")
gsub(/^["'\'']|["'\'']$/, "")
gsub(/^[ \t]+|[ \t]+$/, "")
print
exit
}
' "$file"
else
awk -v key="$key" '
$0 ~ "^"key":" {
gsub(/^[a-zA-Z_-]+:[ ]*/, "")
gsub(/^[ \t]+|[ \t]+$/, "")
gsub(/^["'\'']|["'\'']$/, "")
gsub(/^[ \t]+|[ \t]+$/, "")
print
exit
}
' "$file"
fi
}
yaml_get_commands() {
local section="$1"
local file="$2"
if [ ! -f "$file" ]; then
return
fi
awk -v section="$section" '
$0 ~ "^"section":" { in_section=1; next }
in_section && /^[a-zA-Z]/ { in_section=0 }
in_section && /^ commands:/ { in_commands=1; next }
in_commands && /^ [a-zA-Z]/ { in_commands=0 }
in_commands && /^ - name:/ {
gsub(/^ - name:[ ]*/, "")
gsub(/^["'\'']|["'\'']$/, "")
name=$0
}
in_commands && /^ run:/ {
gsub(/^ run:[ ]*/, "")
gsub(/^["'\'']|["'\'']$/, "")
print name "|" $0
}
' "$file"
}
# ============================================================================
# File Filter Check
# ============================================================================
has_matching_staged_files() {
local filter="$1"
if [ -z "$filter" ]; then
return 0
fi
# Get staged files
local staged_files=$(git diff --cached --name-only --diff-filter=ACMR)
if [ -z "$staged_files" ]; then
return 1
fi
# Check if any file matches the filter pattern
IFS=',' read -ra patterns <<< "$filter"
for pattern in "${patterns[@]}"; do
pattern=$(echo "$pattern" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
local regex=$(echo "$pattern" | sed 's/\./\\./g; s/\*/.*/g')
if echo "$staged_files" | grep -qE "$regex"; then
return 0
fi
done
return 1
}
# ============================================================================
# Django Test Detection
# ============================================================================
check_django_tests() {
local check_enabled=$(yaml_get "pre-commit.django_check_tests" "$CONFIG_FILE")
# Default to true if not specified
if [ "$check_enabled" = "false" ]; then
return 0
fi
# Get staged Python files (excluding tests, migrations, __init__.py)
local staged_py_files=$(git diff --cached --name-only --diff-filter=ACMR | grep '\.py$' | grep -v 'test' | grep -v 'migrations' | grep -v '__init__.py' | grep -v 'conftest.py' | grep -v 'setup.py' | grep -v 'manage.py')
if [ -z "$staged_py_files" ]; then
return 0
fi
local missing_tests=()
while IFS= read -r file; do
[ -z "$file" ] && continue
# Get the directory and filename
local dir=$(dirname "$file")
local filename=$(basename "$file" .py)
# Skip if it's already a test file
if [[ "$filename" == test_* ]] || [[ "$filename" == *_test ]]; then
continue
fi
# Look for corresponding test file
local test_file_1="${dir}/test_${filename}.py"
local test_file_2="${dir}/tests/test_${filename}.py"
local test_file_3="${dir}/${filename}_test.py"
local test_file_4="${dir}/tests.py"
# Check if any test file exists
if [ ! -f "$PROJECT_ROOT/$test_file_1" ] && \
[ ! -f "$PROJECT_ROOT/$test_file_2" ] && \
[ ! -f "$PROJECT_ROOT/$test_file_3" ] && \
[ ! -f "$PROJECT_ROOT/$test_file_4" ]; then
missing_tests+=("$file")
fi
done <<< "$staged_py_files"
if [ ${#missing_tests[@]} -gt 0 ]; then
echo ""
echo -e "${YELLOW}${BOLD}┌──────────────────────────────────────────────────────────────┐${NC}"
echo -e "${YELLOW}${BOLD}│ ⚠ WARNING: Modified files without corresponding tests │${NC}"
echo -e "${YELLOW}${BOLD}└──────────────────────────────────────────────────────────────┘${NC}"
echo ""
for file in "${missing_tests[@]}"; do
echo -e " ${DIM}•${NC} ${WHITE}${file}${NC}"
done
echo ""
echo -e "${DIM}Consider adding tests for these files.${NC}"
echo ""
fi
return 0
}
# ============================================================================
# Run Commands from .dev-hooks.yml
# ============================================================================
run_commands() {
if [ ! -f "$CONFIG_FILE" ]; then
exit 0
fi
local enabled=$(yaml_get "pre-commit.enabled" "$CONFIG_FILE")
if [ "$enabled" = "false" ]; then
exit 0
fi
# Check file filter
local only_for_files=$(yaml_get "pre-commit.only_for_files" "$CONFIG_FILE")
if [ -n "$only_for_files" ]; then
if ! has_matching_staged_files "$only_for_files"; then
echo -e "${DIM}Skipping pre-commit commands (no matching files: ${only_for_files})${NC}"
exit 0
fi
fi
# Check docker settings
local docker_enabled=$(yaml_get "docker.enabled" "$CONFIG_FILE")
local docker_compose=$(yaml_get "docker.compose" "$CONFIG_FILE")
local docker_container=$(yaml_get "docker.container" "$CONFIG_FILE")
local compose_file=$(yaml_get "docker.compose_file" "$CONFIG_FILE")
[ -z "$compose_file" ] && compose_file="docker-compose.yml"
local commands=$(yaml_get_commands "pre-commit" "$CONFIG_FILE")
if [ -z "$commands" ]; then
exit 0
fi
echo ""
echo -e "${CYAN}${BOLD}┌──────────────────────────────────────────────────────────────┐${NC}"
echo -e "${CYAN}${BOLD}│ Running pre-commit checks... │${NC}"
echo -e "${CYAN}${BOLD}└──────────────────────────────────────────────────────────────┘${NC}"
echo ""
local failed=0
while IFS= read -r line; do
[ -z "$line" ] && continue
local name="${line%%|*}"
local cmd="${line#*|}"
echo -e "${BLUE}▶${NC} ${WHITE}${BOLD}${name}${NC}"
echo -e " ${DIM}${cmd}${NC}"
local full_cmd="$cmd"
if [ "$docker_enabled" = "true" ]; then
if [ "$docker_compose" = "true" ]; then
full_cmd="docker-compose -f $compose_file exec -T $docker_container $cmd"
else
full_cmd="docker exec $docker_container $cmd"
fi
fi
cd "$PROJECT_ROOT" || exit 1
if eval "$full_cmd" </dev/null; then
echo -e " ${GREEN}✔ Passed${NC}"
else
echo -e " ${RED}✖ Failed${NC}"
failed=1
fi
echo ""
done <<< "$commands"
if [ $failed -eq 1 ]; then
echo -e "${RED}${BOLD}┌──────────────────────────────────────────────────────────────┐${NC}"
echo -e "${RED}${BOLD}│ ✖ COMMIT REJECTED - Pre-commit checks failed │${NC}"
echo -e "${RED}${BOLD}└──────────────────────────────────────────────────────────────┘${NC}"
echo ""
echo -e "${YELLOW}Fix the issues above and try again.${NC}"
echo ""
exit 1
fi
echo -e "${GREEN}${BOLD}✔ All pre-commit checks passed${NC}"
echo ""
}
# ============================================================================
# Main
# ============================================================================
# Check for missing tests (warning only)
check_django_tests
run_commands
exit 0