Debian Codegraph @

tools/pre-commit-composite

raw
1 #!/bin/bash
2 # Fenrir Composite Pre-commit Hook
3 #
4 # This hook combines version management and code quality validation.
5 # It first runs the version management logic, then runs validation.
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 BLUE='\033[1;34m'
14 NC='\033[0m' # No Color
15
16 echo -e "${BLUE}Fenrir Pre-commit Validation${NC}"
17 echo "=================================="
18
19 # Get the repository root
20 REPO_ROOT=$(git rev-parse --show-toplevel)
21 cd "$REPO_ROOT"
22
23 # ============================================================================
24 # PART 1: VERSION MANAGEMENT (existing logic)
25 # ============================================================================
26
27 echo -e "\n${YELLOW}1. Version Management...${NC}"
28
29 # Check if SKIP_VERSION_UPDATE is set
30 if [[ "${SKIP_VERSION_UPDATE}" = "1" ]]; then
31 echo -e "${YELLOW}Notice: Skipping version update due to SKIP_VERSION_UPDATE=1${NC}"
32 else
33 # Verify .git/versionpath exists
34 if [[ ! -f ".git/versionpath" ]]; then
35 echo -e "${RED}Error: .git/versionpath not found. Please create it with contents:${NC}"
36 echo -e "${YELLOW}versionFile=\"path/to/your/version/file\"${NC}"
37 exit 1
38 fi
39
40 # Source the version path file
41 source ".git/versionpath"
42
43 # Validate that versionFile variable was set
44 if [[ -z "$versionFile" ]]; then
45 echo -e "${RED}Error: versionFile variable not set in .git/versionpath${NC}"
46 exit 1
47 fi
48
49 # Get current date components
50 year=$(date +%Y)
51 month=$(date +%m)
52 day=$(date +%d)
53
54 # Create new version string
55 newVersion="$year.$month.$day"
56
57 # Get current branch name
58 branchName=$(git rev-parse --abbrev-ref HEAD)
59
60 # Check if we're in the middle of a merge
61 if [[ -f ".git/MERGE_HEAD" ]]; then
62 echo -e "${YELLOW}Warning: In the middle of a merge. Skipping version update.${NC}"
63 else
64 # Check if file exists relative to git root
65 if [[ ! -f "$versionFile" ]]; then
66 echo -e "${RED}Error: Version file not found at $versionFile${NC}"
67 exit 1
68 fi
69
70 # Store original version file content
71 originalContent=$(cat "$versionFile")
72
73 # Check if version actually needs updating
74 if ! grep -q "version = \"$newVersion\"" "$versionFile"; then
75 # Update the version in the file
76 sed -i "s/version = [\"']\{0,1\}[0-9.]\+[\"']\{0,1\}/version = \"$newVersion\"/" "$versionFile"
77 fi
78
79 # Check if code_name exists and isn't "stable"
80 if grep -q "code_name.*=.*\"stable\"" "$versionFile"; then
81 # Don't modify stable code_name
82 :
83 elif grep -q "code_name.*=.*\"$branchName\"" "$versionFile"; then
84 # code_name already matches branch name, no need to update
85 :
86 elif grep -q "code_name" "$versionFile"; then
87 # Update existing code_name
88 sed -i "s/code_name = [\"']\{0,1\}[^\"']*[\"']\{0,1\}/code_name = \"$branchName\"/" "$versionFile"
89 else
90 # Add code_name after the version line
91 sed -i "/version = / a\code_name = \"$branchName\"" "$versionFile"
92 fi
93
94 # Check if the file was actually modified
95 if [[ "$(cat "$versionFile")" != "$originalContent" ]]; then
96 echo -e "${GREEN}✓ Version file updated to $newVersion${NC}"
97 if ! git diff --cached --quiet "$versionFile"; then
98 echo -e "${YELLOW}Notice: Version file was already staged, updates made to staged version${NC}"
99 else
100 git add "$versionFile"
101 echo -e "${YELLOW}Notice: Version file has been staged${NC}"
102 fi
103 else
104 echo -e "${GREEN}✓ No version updates needed${NC}"
105 fi
106 fi
107 fi
108
109 # ============================================================================
110 # PART 2: CODE QUALITY VALIDATION (our new logic)
111 # ============================================================================
112
113 echo -e "\n${YELLOW}2. Code Quality Validation...${NC}"
114
115 # Track validation results
116 VALIDATION_FAILED=0
117
118 # 2a. Python Syntax Validation
119 echo -e "\n${YELLOW} 2a. Validating Python syntax...${NC}"
120 if python3 tools/validate_syntax.py --check-only >/dev/null 2>&1; then
121 echo -e "${GREEN} ✓ Syntax validation passed${NC}"
122 else
123 echo -e "${RED} ✗ Syntax validation failed${NC}"
124 echo " Run: python3 tools/validate_syntax.py --fix"
125 VALIDATION_FAILED=1
126 fi
127
128 # 2a2. PEP8/flake8 Validation (for staged Python files only)
129 echo -e "\n${YELLOW} 2a2. Checking PEP8 compliance...${NC}"
130 if command -v flake8 >/dev/null 2>&1 && [ -n "$STAGED_PYTHON_FILES" ]; then
131 PEP8_ISSUES=0
132
133 # Check staged Python files with flake8
134 # Focus on critical issues, ignore cosmetic ones for pre-commit
135 FLAKE8_SELECT="E9,F63,F7,F82" # Critical syntax/import errors only
136 FLAKE8_IGNORE="E501,W503,E203" # Ignore line length and some formatting
137
138 for file in $STAGED_PYTHON_FILES; do
139 if [ -f "$file" ]; then
140 flake8_output=$(flake8 --select="$FLAKE8_SELECT" --ignore="$FLAKE8_IGNORE" "$file" 2>/dev/null || true)
141 if [ -n "$flake8_output" ]; then
142 if [ $PEP8_ISSUES -eq 0 ]; then
143 echo -e "${RED} ✗ Critical PEP8 issues found:${NC}"
144 fi
145 echo -e "${RED} $file:${NC}"
146 echo "$flake8_output" | sed 's/^/ /'
147 PEP8_ISSUES=1
148 fi
149 fi
150 done
151
152 if [ $PEP8_ISSUES -eq 0 ]; then
153 echo -e "${GREEN} ✓ No critical PEP8 issues in staged files${NC}"
154 else
155 echo -e "${RED} ✗ Critical PEP8 issues found${NC}"
156 echo -e "${YELLOW} Run: flake8 --select=E9,F63,F7,F82 <file> for details${NC}"
157 VALIDATION_FAILED=1
158 fi
159 elif [ -n "$STAGED_PYTHON_FILES" ]; then
160 echo -e "${YELLOW} ⚠ flake8 not available (install with: pip install flake8)${NC}"
161 else
162 echo -e "${GREEN} ✓ No Python files to check${NC}"
163 fi
164
165 # 2b. Check for common issues in modified files
166 echo -e "\n${YELLOW} 2b. Checking modified files for common issues...${NC}"
167
168 # Get list of staged files (all types)
169 STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM || true)
170 STAGED_PYTHON_FILES=$(echo "$STAGED_FILES" | grep '\.py$' || true)
171
172 if [ -n "$STAGED_FILES" ]; then
173 ISSUES_FOUND=0
174
175 # Check for cache files being committed
176 CACHE_FILES=$(echo "$STAGED_FILES" | grep -E '(__pycache__|\.pyc$)' || true)
177 if [ -n "$CACHE_FILES" ]; then
178 echo -e "${RED} ✗ Python cache files staged for commit:${NC}"
179 echo "$CACHE_FILES" | while read cache_file; do
180 echo -e "${RED} $cache_file${NC}"
181 done
182 echo -e "${RED} ✗ Run: python3 tools/cleanup_cache.py --remove${NC}"
183 ISSUES_FOUND=1
184 fi
185
186 # Check Python files for specific issues
187 if [ -n "$STAGED_PYTHON_FILES" ]; then
188 for file in $STAGED_PYTHON_FILES; do
189 if [ -f "$file" ]; then
190 # Check for unterminated strings (the main issue from the email)
191 if grep -n 'f".*{$' "$file" >/dev/null 2>&1; then
192 echo -e "${RED} ✗ $file: Potential unterminated f-string${NC}"
193 ISSUES_FOUND=1
194 fi
195
196 # Check for missing imports that are commonly used
197 if grep -q 'debug\.DebugLevel\.' "$file" && ! grep -q 'from.*debug' "$file" && ! grep -q 'import.*debug' "$file"; then
198 echo -e "${YELLOW} ⚠ $file: Uses debug.DebugLevel but no debug import found${NC}"
199 fi
200 fi
201 done
202 fi
203
204 if [ $ISSUES_FOUND -eq 0 ]; then
205 echo -e "${GREEN} ✓ No common issues found in staged files${NC}"
206 else
207 echo -e "${RED} ✗ Common issues found in staged files${NC}"
208 VALIDATION_FAILED=1
209 fi
210 else
211 echo -e "${GREEN} ✓ No files staged for commit${NC}"
212 fi
213
214 # 2c. Quick import test for core modules (informational only)
215 echo -e "\n${YELLOW} 2c. Testing core module imports...${NC}"
216 IMPORT_WARNINGS=0
217
218 # Test core imports that are critical (but don't fail on import issues - might be dependency related)
219 CORE_MODULES=(
220 "src.fenrirscreenreader.core.fenrirManager"
221 "src.fenrirscreenreader.core.commandManager"
222 "src.fenrirscreenreader.core.eventManager"
223 )
224
225 cd src
226 for module in "${CORE_MODULES[@]}"; do
227 if python3 -c "import $module" 2>/dev/null; then
228 echo -e "${GREEN} ✓ $module${NC}"
229 else
230 echo -e "${YELLOW} ⚠ $module (import failed - might be dependency related)${NC}"
231 IMPORT_WARNINGS=1
232 fi
233 done
234 cd "$REPO_ROOT"
235
236 if [ $IMPORT_WARNINGS -eq 1 ]; then
237 echo -e "${YELLOW} ⚠ Some core module imports failed (non-blocking)${NC}"
238 echo -e "${YELLOW} This may be due to missing runtime dependencies${NC}"
239 else
240 echo -e "${GREEN} ✓ Core module imports successful${NC}"
241 fi
242
243 # ============================================================================
244 # FINAL SUMMARY
245 # ============================================================================
246
247 echo -e "\n============================================================"
248 if [ $VALIDATION_FAILED -eq 0 ]; then
249 echo -e "${GREEN}✓ All pre-commit validations passed${NC}"
250 echo -e "${GREEN}✓ Version management completed${NC}"
251 echo -e "${GREEN}✓ Code quality checks passed${NC}"
252 echo -e "${GREEN}Commit allowed to proceed${NC}"
253
254 # Show skip option
255 echo -e "\n${BLUE}Tip: You can skip version updates with SKIP_VERSION_UPDATE=1${NC}"
256 exit 0
257 else
258 echo -e "${RED}✗ Pre-commit validation failed${NC}"
259 echo -e "${RED}Commit blocked - please fix issues above${NC}"
260 echo ""
261 echo "Quick fixes:"
262 echo " • Python syntax: python3 tools/validate_syntax.py --fix"
263 echo " • Review flagged files manually"
264 echo " • Re-run commit after fixes"
265 echo ""
266 echo -e "${BLUE}Note: Version management completed successfully${NC}"
267 exit 1
268 fi