harbor run -t openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-4628harbor download openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-4628You need to implement a pre-commit hook functionality that checks for syntax errors in PHP files before allowing a commit.
Identify Changed Files: The script should identify any new, modified, or deleted files in the Git staging area that have extensions .php, .module, .inc, or .install.
Syntax Checking: For each identified file, it must use the PHP command line tool to check for parsing syntax errors. If a file has any syntax errors, these should be recorded.
Error Reporting: If any syntax errors are detected, they should be displayed in a readable format, including the file names where errors are found and the error messages themselves. The output colors for messages should follow the specifications mentioned (e.g., yellow for starting the check, red for error messages, and green for success messages).
Prevention of Commit: If any errors are detected, the script should terminate the commit process and output a message indicating that errors must be fixed before committing. If no errors are found, it should allow the commit to proceed.
If syntax errors are found, the output will include:
If no errors are found, the output will indicate a successful check with the message, "Okay".
php-linter located in /app/.The following test script will be used to verify your solution:
#!/bin/bash
ROOT_DIR="$(pwd)/"
LIST=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD)
ERRORS_BUFFER=""
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"0;31m"
COL_GREEN=$ESC_SEQ"0;32m"
COL_YELLOW=$ESC_SEQ"0;33m"
COL_BLUE=$ESC_SEQ"0;34m"
COL_MAGENTA=$ESC_SEQ"0;35m"
COL_CYAN=$ESC_SEQ"0;36m"
echo
printf "$COL_YELLOW%s$COL_RESET\n" "Running pre-commit hook: \"php-linter\""
for file in $LIST
do
EXTENSION=$(echo "$file" | grep -E ".php$|.module$|.inc$|.install$")
if [ "$EXTENSION" != "" ]; then
ERRORS=$(php -l $ROOT_DIR$file 2>&1 | grep "Parse error")
if [ "$ERRORS" != "" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
echo "Syntax errors found in file: $file "
fi
fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
echo
echo "These errors were found in try-to-commit files: "
echo -e $ERRORS_BUFFER
echo
printf "$COL_RED%s$COL_RESET\r\n\r\n" "Can't commit, fix errors first."
exit 1
else
echo "Okay"
exit 0
fi