2010-04-03 02:12:21 +01:00
|
|
|
#!/usr/bin/env bash
|
2010-03-10 20:07:10 +01:00
|
|
|
#
|
|
|
|
# A hook to disallow php syntax errors to be committed
|
|
|
|
# by running php -l (lint) on them. It requires php-cli
|
|
|
|
# to be installed.
|
|
|
|
#
|
|
|
|
# This is a pre-commit hook.
|
|
|
|
#
|
|
|
|
# To install this you can either copy or symlink it to
|
|
|
|
# $GIT_DIR/hooks, example:
|
|
|
|
#
|
|
|
|
# ln -s ../../git-tools/hooks/pre-commit \\
|
|
|
|
# .git/hooks/pre-commit
|
|
|
|
|
2010-03-11 19:44:21 +01:00
|
|
|
PHP_BIN=php
|
|
|
|
|
2010-03-10 20:07:10 +01:00
|
|
|
# necessary check for initial commit
|
|
|
|
if git rev-parse --verify HEAD >/dev/null 2>&1
|
|
|
|
then
|
|
|
|
against=HEAD
|
|
|
|
else
|
|
|
|
# Initial commit: diff against an empty tree object
|
|
|
|
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
|
|
|
|
fi
|
|
|
|
|
|
|
|
error=0
|
2010-04-03 01:53:12 +01:00
|
|
|
errors=""
|
2010-03-10 20:07:10 +01:00
|
|
|
|
2010-03-17 21:04:54 +01:00
|
|
|
IFS=$'\n'
|
|
|
|
# get a list of staged files
|
|
|
|
for line in $(git diff-index --cached --full-index $against)
|
2010-03-10 20:07:10 +01:00
|
|
|
do
|
2010-03-17 21:04:54 +01:00
|
|
|
# split needed values
|
|
|
|
sha=$(echo $line | cut -d' ' -f4)
|
|
|
|
temp=$(echo $line | cut -d' ' -f5)
|
|
|
|
status=$(echo $temp | cut -d' ' -f1)
|
|
|
|
filename=$(echo $temp | cut -d' ' -f2)
|
|
|
|
|
|
|
|
# file extension
|
|
|
|
ext=$(echo $filename | sed 's/^.*\.//')
|
|
|
|
|
|
|
|
# only check files with php extension
|
|
|
|
if [ $ext != "php" ]
|
|
|
|
then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# do not check deleted files
|
|
|
|
if [ $status = "D" ]
|
|
|
|
then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check the staged file content for syntax errors
|
|
|
|
# using php -l (lint)
|
2010-04-03 21:53:09 +02:00
|
|
|
result=$(git cat-file -p $sha | /usr/bin/env $PHP_BIN -l 2>/dev/null)
|
2010-03-17 21:04:54 +01:00
|
|
|
if [ $? -ne 0 ]
|
2010-03-10 20:07:10 +01:00
|
|
|
then
|
|
|
|
error=1
|
2010-04-03 01:53:12 +01:00
|
|
|
# Swap back in correct filenames
|
|
|
|
errors+=${result//in - on/"$filename"}
|
2010-03-10 20:07:10 +01:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
unset IFS
|
|
|
|
|
|
|
|
if [ $error -eq 1 ]
|
|
|
|
then
|
2010-04-03 01:53:12 +01:00
|
|
|
echo -e "PHP Syntax check failed:";
|
|
|
|
echo -e "$errors" | grep "^Parse error:"
|
2010-03-10 20:07:10 +01:00
|
|
|
exit 1
|
|
|
|
fi
|