google_forms/.husky/pre-commit

61 lines
1.6 KiB
Plaintext
Raw Normal View History

2024-08-08 06:12:40 +00:00
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
2024-08-08 11:30:15 +00:00
# Get the list of staged PHP files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.php$')
# If there are no staged PHP files, exit
if [ -z "$STAGED_FILES" ]; then
echo "No PHP files staged for commit."
exit 0
fi
2024-08-08 06:12:40 +00:00
# Function to display errors
display_errors() {
local errors="$1"
echo "Errors detected:"
echo "---------------------------------------"
2024-08-08 11:30:15 +00:00
echo "$errors" | while IFS='|' read -r file line error; do
printf "%-60s | %-10s | %s\n" "$file" "$line" "$error"
done
2024-08-08 06:12:40 +00:00
echo "---------------------------------------"
}
2024-08-08 11:30:15 +00:00
# Run PHP lint to check for syntax errors
SYNTAX_ERRORS=0
echo "Checking PHP syntax errors..."
for FILE in $STAGED_FILES; do
php -l "$FILE"
if [ $? -ne 0 ]; then
SYNTAX_ERRORS=1
2024-08-08 06:12:40 +00:00
fi
2024-08-08 11:30:15 +00:00
done
2024-08-08 06:12:40 +00:00
2024-08-08 11:30:15 +00:00
if [ $SYNTAX_ERRORS -ne 0 ]; then
echo "Syntax errors detected. Please fix them before committing."
exit 1
fi
2024-08-08 06:12:40 +00:00
2024-08-08 11:30:15 +00:00
# Run PHPCBF to auto-fix issues
echo "Running PHPCBF..."
for FILE in $STAGED_FILES; do
/home/aissel/.config/composer/vendor/bin/phpcbf --standard=/var/www/html/google_forms/phpcs.xml "$FILE" || true
done
2024-08-08 06:12:40 +00:00
2024-08-08 11:30:15 +00:00
# Run PHP CS Fixer to auto-fix issues
echo "Running PHP CS Fixer..."
for FILE in $STAGED_FILES; do
/home/aissel/.config/composer/vendor/bin/php-cs-fixer fix "$FILE"
done
2024-08-08 06:12:40 +00:00
2024-08-08 11:30:15 +00:00
# Re-run PHPCS to check for unresolved coding standard violations
echo "Running PHPCS..."
echo "$STAGED_FILES" | xargs -n 1 /home/aissel/.config/composer/vendor/bin/phpcs --standard=/var/www/html/google_forms/phpcs.xml
2024-08-08 06:12:40 +00:00
# Add the fixed files back to the staging area
for FILE in $STAGED_FILES; do
git add "$FILE"
done
echo "Pre-commit checks completed."