59 lines
1.7 KiB
Bash
Executable File
59 lines
1.7 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
# Pre-commit hook that automatically fixes trailing whitespace and missing newline at EOF
|
|
# This hook only fixes issues and never blocks commits
|
|
|
|
# Color codes for output
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[0;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Get list of staged files
|
|
staged_files=$(git diff --cached --name-only --diff-filter=ACM)
|
|
|
|
if [ -z "$staged_files" ]; then
|
|
exit 0
|
|
fi
|
|
|
|
files_fixed=0
|
|
|
|
# Process each staged file
|
|
for file in $staged_files; do
|
|
if [ -f "$file" ]; then
|
|
# Skip binary files
|
|
if file -b --mime "$file" 2>/dev/null | grep -q "text"; then
|
|
file_modified=0
|
|
|
|
# Remove trailing whitespace
|
|
if grep -q '[[:space:]]$' "$file" 2>/dev/null; then
|
|
echo "${YELLOW}→${NC} Removing trailing whitespace from: $file"
|
|
sed -i 's/[[:space:]]*$//' "$file" 2>/dev/null || true
|
|
file_modified=1
|
|
fi
|
|
|
|
# Add newline at end of file if missing
|
|
if [ -n "$(tail -c 1 "$file" 2>/dev/null)" ]; then
|
|
echo "${YELLOW}→${NC} Adding newline at end of: $file"
|
|
echo >> "$file" 2>/dev/null || true
|
|
file_modified=1
|
|
fi
|
|
|
|
# Re-stage the file if it was modified
|
|
if [ $file_modified -eq 1 ]; then
|
|
git add "$file" 2>/dev/null || true
|
|
files_fixed=$((files_fixed + 1))
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
|
|
# Report results
|
|
if [ $files_fixed -gt 0 ]; then
|
|
echo "${GREEN}✓${NC} Fixed $files_fixed file(s) - changes have been staged"
|
|
else
|
|
echo "${GREEN}✓${NC} No fixes needed - all files are clean"
|
|
fi
|
|
|
|
# Always exit successfully to never block commits
|
|
exit 0
|