|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Pre-commit hook to enforce Windows compatibility and file size limits |
| 4 | +# This hook prevents commits that would fail on Windows systems or that contain overly large files |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# ANSI color codes for better output readability |
| 9 | +RED='\033[0;31m' |
| 10 | +YELLOW='\033[1;33m' |
| 11 | +NC='\033[0m' # No Color |
| 12 | + |
| 13 | +# Track if we found any errors |
| 14 | +ERRORS_FOUND=0 |
| 15 | + |
| 16 | +echo "Running pre-commit checks..." |
| 17 | + |
| 18 | +# Check 1: Windows-incompatible filenames |
| 19 | +echo " Checking for Windows-incompatible filenames..." |
| 20 | + |
| 21 | +# Windows reserved characters: < > : " | ? * \ |
| 22 | +# Note: We escape the backslash in the regex pattern |
| 23 | +INVALID_CHARS='[<>:"|?*\\]' |
| 24 | + |
| 25 | +# Get list of staged files |
| 26 | +STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACR) |
| 27 | + |
| 28 | +if [ -n "$STAGED_FILES" ]; then |
| 29 | + # Check each staged file for invalid characters |
| 30 | + INVALID_FILES=$(echo "$STAGED_FILES" | grep -E "$INVALID_CHARS" || true) |
| 31 | + |
| 32 | + if [ -n "$INVALID_FILES" ]; then |
| 33 | + echo -e "${RED}Error: The following files have Windows-incompatible characters in their names:${NC}" |
| 34 | + echo "$INVALID_FILES" | while read -r file; do |
| 35 | + echo " - $file" |
| 36 | + done |
| 37 | + echo -e "${YELLOW}Please rename these files to remove characters: < > : \" | ? * \\${NC}" |
| 38 | + echo -e "${YELLOW}For ISO timestamps, replace colons with hyphens (e.g., 08:40:24 -> 08-40-24)${NC}" |
| 39 | + ERRORS_FOUND=1 |
| 40 | + fi |
| 41 | +fi |
| 42 | + |
| 43 | +# Check 2: Files larger than 10MB |
| 44 | +echo " Checking for files larger than 10MB..." |
| 45 | + |
| 46 | +MAX_SIZE=$((10 * 1024 * 1024)) # 10 MB in bytes |
| 47 | +LARGE_FILES="" |
| 48 | + |
| 49 | +if [ -n "$STAGED_FILES" ]; then |
| 50 | + while IFS= read -r file; do |
| 51 | + if [ -f "$file" ]; then |
| 52 | + size=$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null || echo 0) |
| 53 | + if [ "$size" -gt "$MAX_SIZE" ]; then |
| 54 | + # Format size in human-readable format |
| 55 | + size_mb=$(echo "scale=2; $size / 1024 / 1024" | bc) |
| 56 | + LARGE_FILES="${LARGE_FILES} - $file (${size_mb} MB)\n" |
| 57 | + fi |
| 58 | + fi |
| 59 | + done <<< "$STAGED_FILES" |
| 60 | +fi |
| 61 | + |
| 62 | +if [ -n "$LARGE_FILES" ]; then |
| 63 | + echo -e "${RED}Error: The following files exceed 10MB:${NC}" |
| 64 | + echo -e "$LARGE_FILES" |
| 65 | + echo -e "${YELLOW}Please consider one of these options:${NC}" |
| 66 | + echo -e "${YELLOW} 1. Split the file into smaller parts with suffixes .part1, .part2, etc.${NC}" |
| 67 | + echo -e "${YELLOW} 2. Use Git Large File Storage (Git LFS): https://git-lfs.github.com/${NC}" |
| 68 | + echo -e "${YELLOW} 3. Remove unnecessary content from the file${NC}" |
| 69 | + ERRORS_FOUND=1 |
| 70 | +fi |
| 71 | + |
| 72 | +# Exit with error if any checks failed |
| 73 | +if [ "$ERRORS_FOUND" -eq 1 ]; then |
| 74 | + echo -e "${RED}Pre-commit checks failed. Please fix the issues above and try again.${NC}" |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +echo " All pre-commit checks passed!" |
| 79 | +exit 0 |
0 commit comments