This is a followup from my two previous posts on php and js git pre-commit syntax check where I had mentioned how to check for php and js syntax independently using pre-commit with git.
But what if we wanted to check for both php and js syntax at same time while committing. So, I used the scripts used for both and combined them. Below is the final result.
#!/bin/bash
Give it execute permission: $ chmod +x .git/hooks/pre-commit and it should be good to go :)
Sample output when I try to commit two php files and two js files with syntax errors:
Filenames with code content:
error.php
But what if we wanted to check for both php and js syntax at same time while committing. So, I used the scripts used for both and combined them. Below is the final result.
#!/bin/bash
commit_error=false
ROOT_DIR="$(pwd)/"
LIST=$(git diff-index --cached --name-only --diff-filter=ACMR HEAD)
ERRORS_BUFFER=""
for file in $LIST
do
EXTENSION=$(echo "$file" | grep ".php$")
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
# Check for xdebug statments
ERRORS=$(grep -nH xdebug_ "$ROOT_DIR$file" | \
sed -e 's/^/Found XDebug Statment : /')
if [ "$ERRORS" != "" ]; then
if [ "$ERRORS_BUFFER" != "" ]; then
ERRORS_BUFFER="$ERRORS_BUFFER\n$ERRORS"
else
ERRORS_BUFFER="$ERRORS"
fi
fi
fi
done
if [ "$ERRORS_BUFFER" != "" ]; then
echo
echo "Found PHP parse errors: "
echo -e $ERRORS_BUFFER
echo
echo "PHP parse errors found. Fix errors and commit again."
commit_error=true
else
echo "No PHP parse errors found. Committed successfully."
fi
files=$(git diff --cached --name-only --diff-filter=ACM | grep "\.js$")
if [ "$files" = "" ]; then
exit 0
fi
pass=true
JS_ERRORS_BUFFER=""
echo -e "\nValidating JavaScript:\n"
for file in ${files}; do
result=$(jslint ${file} | grep "${file} is OK")
if [ "$result" != "" ]; then
echo -e "\t\033[32mJSLint Passed: ${file}\033[0m"
else
JS_ERRORS=$(jslint ${file})
JS_ERRORS_BUFFER="$JS_ERRORS_BUFFER\n$JS_ERRORS"
echo -e "\t\033[31mJSLint Failed: ${file}\033[0m"
pass=false
fi
done
echo -e "\nJavaScript validation complete\n"
if ! $pass; then
echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass JSLint but do not. Please fix the JSLint errors and try again."
echo -e $JS_ERRORS_BUFFER
echo
commit_error=true
else
echo -e "\033[42mCOMMIT SUCCEEDED\033[0m\n"
fi
if $commit_error; then
exit 1
fi
Store it in .git/hooks/pre-commit
Sample output when I try to commit two php files and two js files with syntax errors:
Filenames with code content:
error.php
<?php
$array = ;
$array = ;
another_error.php
<?php
var_dump(');
error.js
i =
another_error.js
i = k;
$ git commit -m 'Error Commit' error.php another_error.php error.js another_error.js
Output from above error commit:
Syntax errors found in file: another_error.php
Syntax errors found in file: error.php
Found PHP parse errors:
PHP Parse error: syntax error, unexpected ';' in /var/www/html/error.php on line 2
PHP Parse error: syntax error, unexpected '');' (T_ENCAPSED_AND_WHITESPACE) in /var/www/html/another_error.php on line 2
PHP parse errors found. Fix errors and commit again.
Validating JavaScript:
JSLint Failed: error.js
JSLint Failed: another_error.js
JavaScript validation complete
COMMIT FAILED: Your commit contains files that should pass JSLint but do not. Please fix the JSLint errors and try again.
error.js #1 'i' was used before it was defined. i = // Line 1, Pos 1 #2 Unexpected character '(space)'. i = // Line 1, Pos 4 #3 Unexpected '(end)'. i = // Line 1, Pos 3 #4 Stopping. (50% scanned). // Line 1, Pos 3
another_error.js #1 'i' was used before it was defined. i = k; // Line 1, Pos 1 #2 'k' was used before it was defined. i = k; // Line 1, Pos 5
Above script can be further modified to format the output better but is helpful for basic php and js syntax issues and js standard checks in code :)
Above script can be further modified to format the output better but is helpful for basic php and js syntax issues and js standard checks in code :)
No comments:
Post a Comment