Add whitespace cleanup to pre-commit hook
* modified: git/hooks/pre-commit
This commit is contained in:
@@ -1,17 +1,13 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# Pre-commit hook to check for trailing whitespace and missing newline at EOF
|
# Pre-commit hook that automatically fixes trailing whitespace and missing newline at EOF
|
||||||
|
# This hook only fixes issues and never blocks commits
|
||||||
# Exit on first error
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Color codes for output
|
# Color codes for output
|
||||||
RED='\033[0;31m'
|
|
||||||
GREEN='\033[0;32m'
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[0;33m'
|
||||||
NC='\033[0m' # No Color
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
errors_found=0
|
|
||||||
|
|
||||||
# Get list of staged files
|
# Get list of staged files
|
||||||
staged_files=$(git diff --cached --name-only --diff-filter=ACM)
|
staged_files=$(git diff --cached --name-only --diff-filter=ACM)
|
||||||
|
|
||||||
@@ -19,47 +15,44 @@ if [ -z "$staged_files" ]; then
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check for trailing whitespace
|
files_fixed=0
|
||||||
echo "Checking for trailing whitespace..."
|
|
||||||
for file in $staged_files; do
|
|
||||||
if [ -f "$file" ]; then
|
|
||||||
# Check if file has trailing whitespace
|
|
||||||
if grep -q '[[:space:]]$' "$file"; then
|
|
||||||
echo "${RED}✗${NC} Trailing whitespace found in: $file"
|
|
||||||
# Show lines with trailing whitespace
|
|
||||||
grep -n '[[:space:]]$' "$file" | while IFS=: read -r line_num line_content; do
|
|
||||||
echo " Line $line_num: '${line_content}'"
|
|
||||||
done
|
|
||||||
errors_found=1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
# Check for missing newline at end of file
|
# Process each staged file
|
||||||
echo "Checking for newline at end of files..."
|
|
||||||
for file in $staged_files; do
|
for file in $staged_files; do
|
||||||
if [ -f "$file" ]; then
|
if [ -f "$file" ]; then
|
||||||
# Check if file ends with a newline
|
# Skip binary files
|
||||||
if [ -n "$(tail -c 1 "$file")" ]; then
|
if file -b --mime "$file" 2>/dev/null | grep -q "text"; then
|
||||||
echo "${RED}✗${NC} Missing newline at end of file: $file"
|
file_modified=0
|
||||||
errors_found=1
|
|
||||||
|
# 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
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
# Report results
|
# Report results
|
||||||
if [ $errors_found -eq 0 ]; then
|
if [ $files_fixed -gt 0 ]; then
|
||||||
echo "${GREEN}✓${NC} All checks passed!"
|
echo "${GREEN}✓${NC} Fixed $files_fixed file(s) - changes have been staged"
|
||||||
exit 0
|
|
||||||
else
|
else
|
||||||
echo ""
|
echo "${GREEN}✓${NC} No fixes needed - all files are clean"
|
||||||
echo "${RED}Pre-commit checks failed!${NC}"
|
|
||||||
echo "Please fix the issues above and try again."
|
|
||||||
echo ""
|
|
||||||
echo "To fix trailing whitespace, you can use:"
|
|
||||||
echo " sed -i 's/[[:space:]]*$//' <filename>"
|
|
||||||
echo ""
|
|
||||||
echo "To add missing newline at EOF, you can use:"
|
|
||||||
echo " echo >> <filename>"
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Always exit successfully to never block commits
|
||||||
|
exit 0
|
||||||
|
|||||||
Reference in New Issue
Block a user