Debian Codegraph @

tools/pre-commit-hook

raw
1 #!/bin/bash
2 # Fenrir Pre-commit Hook
3 #
4 # This hook validates Python syntax and basic code quality before commits.
5 # Install with: ln -sf ../../tools/pre-commit-hook .git/hooks/pre-commit
6
7 set -e
8
9 # Colors for output
10 RED='\033[0;31m'
11 GREEN='\033[0;32m'
12 YELLOW='\033[1;33m'
13 NC='\033[0m' # No Color
14
15 echo -e "${GREEN}Fenrir Pre-commit Validation${NC}"
16 echo "=================================="
17
18 # Get the repository root
19 REPO_ROOT=$(git rev-parse --show-toplevel)
20 cd "$REPO_ROOT"
21
22 # Track validation results
23 VALIDATION_FAILED=0
24
25 # 1. Python Syntax Validation
26 echo -e "\n${YELLOW}1. Validating Python syntax...${NC}"
27 if python3 tools/validate_syntax.py --check-only; then
28 echo -e "${GREEN}✓ Syntax validation passed${NC}"
29 else
30 echo -e "${RED}✗ Syntax validation failed${NC}"
31 echo "Run: python3 tools/validate_syntax.py --fix"
32 VALIDATION_FAILED=1
33 fi
34
35 # 2. Check for common issues in modified files
36 echo -e "\n${YELLOW}2. Checking modified files for common issues...${NC}"
37
38 # Get list of staged Python files
39 STAGED_PYTHON_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$' || true)
40
41 if [ -n "$STAGED_PYTHON_FILES" ]; then
42 ISSUES_FOUND=0
43
44 for file in $STAGED_PYTHON_FILES; do
45 if [ -f "$file" ]; then
46 # Check for broken f-strings (multiline issues that cause syntax errors)
47 # Pattern 1: f-string with opening brace at end of line (likely broken across lines)
48 if grep -n 'f"[^"]*{[^}]*$' "$file" >/dev/null 2>&1; then
49 echo -e "${RED}✗ $file: Potential broken multiline f-string${NC}"
50 grep -n 'f"[^"]*{[^}]*$' "$file" | head -3
51 ISSUES_FOUND=1
52 fi
53
54 # Pattern 2: Lines that end with just an opening brace (common in broken f-strings)
55 if grep -n '^\s*[^#]*{$' "$file" >/dev/null 2>&1; then
56 echo -e "${YELLOW}⚠ $file: Lines ending with lone opening brace (check f-strings)${NC}"
57 fi
58
59 # Check for missing imports that are commonly used
60 if grep -q 'debug\.DebugLevel\.' "$file" && ! grep -q 'from.*debug' "$file" && ! grep -q 'import.*debug' "$file"; then
61 echo -e "${YELLOW}⚠ $file: Uses debug.DebugLevel but no debug import found${NC}"
62 fi
63
64 # Check for extremely long lines (over 120 chars) that might indicate issues
65 if awk 'length($0) > 120 {print NR ": " $0; exit 1}' "$file" >/dev/null 2>&1; then
66 # Only warn, don't fail
67 line_num=$(awk 'length($0) > 120 {print NR; exit}' "$file")
68 echo -e "${YELLOW}⚠ $file:$line_num: Very long line (>120 chars)${NC}"
69 fi
70 fi
71 done
72
73 if [ $ISSUES_FOUND -eq 0 ]; then
74 echo -e "${GREEN}✓ No common issues found in modified files${NC}"
75 else
76 echo -e "${RED}✗ Common issues found in modified files${NC}"
77 VALIDATION_FAILED=1
78 fi
79 else
80 echo -e "${GREEN}✓ No Python files modified${NC}"
81 fi
82
83 # 3. Quick import test for core modules
84 echo -e "\n${YELLOW}3. Testing core module imports...${NC}"
85 IMPORT_FAILED=0
86
87 # Test core imports that are critical
88 CORE_MODULES=(
89 "src.fenrirscreenreader.core.fenrirManager"
90 "src.fenrirscreenreader.core.commandManager"
91 "src.fenrirscreenreader.core.eventManager"
92 )
93
94 cd src
95 for module in "${CORE_MODULES[@]}"; do
96 if python3 -c "import $module" 2>/dev/null; then
97 echo -e "${GREEN}✓ $module${NC}"
98 else
99 echo -e "${RED}✗ $module (import failed)${NC}"
100 IMPORT_FAILED=1
101 fi
102 done
103 cd "$REPO_ROOT"
104
105 if [ $IMPORT_FAILED -eq 1 ]; then
106 echo -e "${RED}✗ Core module import test failed${NC}"
107 VALIDATION_FAILED=1
108 else
109 echo -e "${GREEN}✓ Core module imports successful${NC}"
110 fi
111
112 # 4. Run test suite
113 echo -e "\n${YELLOW}4. Running test suite...${NC}"
114 if command -v pytest >/dev/null 2>&1; then
115 # Run tests quietly, show summary at end
116 if pytest tests/ -q --tb=short 2>&1 | tail -20; then
117 echo -e "${GREEN}✓ All tests passed${NC}"
118 else
119 echo -e "${RED}✗ Test suite failed${NC}"
120 echo "Run: pytest tests/ -v (to see details)"
121 VALIDATION_FAILED=1
122 fi
123 else
124 echo -e "${YELLOW}⚠ pytest not installed - skipping tests${NC}"
125 echo " Install with: pip install pytest"
126 echo " Or full test suite: pip install -r tests/requirements.txt"
127 fi
128
129 # 5. Check for secrets or sensitive data
130 echo -e "\n${YELLOW}5. Checking for potential secrets...${NC}"
131 SECRETS_FOUND=0
132
133 if [ -n "$STAGED_PYTHON_FILES" ]; then
134 for file in $STAGED_PYTHON_FILES; do
135 if [ -f "$file" ]; then
136 # Check for potential passwords, keys, tokens
137 if grep -i -E '(password|passwd|pwd|key|token|secret|api_key).*=.*["'"'"'][^"'"'"']{8,}["'"'"']' "$file" >/dev/null 2>&1; then
138 echo -e "${RED}✗ $file: Potential hardcoded secret detected${NC}"
139 SECRETS_FOUND=1
140 fi
141 fi
142 done
143 fi
144
145 if [ $SECRETS_FOUND -eq 0 ]; then
146 echo -e "${GREEN}✓ No potential secrets found${NC}"
147 else
148 echo -e "${RED}✗ Potential secrets found - please review${NC}"
149 VALIDATION_FAILED=1
150 fi
151
152 # Summary
153 echo -e "\n=================================================="
154 if [ $VALIDATION_FAILED -eq 0 ]; then
155 echo -e "${GREEN}✓ All pre-commit validations passed${NC}"
156 echo -e "${GREEN}Commit allowed to proceed${NC}"
157 exit 0
158 else
159 echo -e "${RED}✗ Pre-commit validation failed${NC}"
160 echo -e "${RED}Commit blocked - please fix issues above${NC}"
161 echo ""
162 echo "Quick fixes:"
163 echo " • Python syntax: python3 tools/validate_syntax.py --fix"
164 echo " • Run tests: pytest tests/ -v"
165 echo " • Review flagged files manually"
166 echo " • Re-run commit after fixes"
167 exit 1
168 fi