google_forms/.husky/pre-commit

81 lines
2.1 KiB
Plaintext
Raw Permalink Normal View History

2024-08-08 06:12:40 +00:00
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
2024-08-21 06:34:30 +00:00
2024-08-09 12:04:48 +00:00
# Run JS/CSS checks
.husky/pre-commit-js-css
if [ $? -ne 0 ]; then
echo "JS/CSS checks failed"
exit 1
fi
2024-08-21 06:34:30 +00:00
2024-08-09 12:04:48 +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-09 12:04:48 +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-09 12:04:48 +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-09 12:04:48 +00:00
done
2024-08-08 06:12:40 +00:00
2024-08-09 12:04:48 +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-09 12:04:48 +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-21 06:34:30 +00:00
# Run PHPCS to check for unresolv//cheks for the file path & the nampespaceed coding standard violations
2024-08-09 12:04:48 +00:00
echo "Running PHPCS..."
2024-08-21 06:34:30 +00:00
ERROR_FILE="/var/www/html/google_forms/phpcs_errors.json" # Specify your error file path and format here
# Run PHPCS, display errors in the terminal, and store them in the JSON file
echo "$STAGED_FILES" | xargs -n 1 /home/aissel/.config/composer/vendor/bin/phpcs --standard=/var/www/html/google_forms/phpcs.xml --report=json | tee "$ERROR_FILE"
# Additionally, run PHPCS to show output in terminal in standard format
2024-08-09 12:04:48 +00:00
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
2024-08-21 06:34:30 +00:00
# Check if there were any errors reported
if grep -q '"errors":' "$ERROR_FILE"; then
echo "PHPCS errors detected. Please fix them before committing."
exit 1
fi
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."