<p>What I have so far is </p>

<pre><code>#!/bin/sh

php_syntax_check()
{
    retval=0
    for i in $(git-diff-index --name-only --cached HEAD -- | grep -e '\.php$'); do
        if [ -f $i ]; then
            output=$(php -l $i)
            retval=$?
            if [ $retval -gt 0 ]; then
                echo "=============================================================================="
                echo "Unstaging $i for the commit due to the follow parse errors"
                echo "$output"
                git reset -q HEAD $i
            fi
        fi
    done

    if [ $retval -gt 0 ]; then
        exit $retval
    fi
}
php_syntax_check
</code></pre>

[{'answer_id': 173878, 'author': 'Anonymous', 'author_id': 15073, 'author_profile': 'https://Stackoverflow.com/users/15073', 'pm_score': 2, 'selected': False, 'text': "<p>I'm sorry if it's offtopic, but aren't you supposed to run some kind of automated tests (which would imply that the code has no syntax errors) before doing a commit?</p>\n"}, {'answer_id': 547489, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>If you've got the php5-cli installed you can write your pre-commit in PHP and use the syntax your more familiar with.</p>\n\n<p>Just do something more like.</p>\n\n<pre><code>#!/usr/bin/php\n&lt;?php /* Your pre-commit check. */ ?&gt;\n</code></pre>\n"}, {'answer_id': 3069175, 'author': 'LarryH', 'author_id': 13923, 'author_profile': 'https://Stackoverflow.com/users/13923', 'pm_score': 3, 'selected': True, 'text': '<p>If the commit is a partial commit (not all the changes in the working tree are committed), then this make give incorrect results since it tests the working copy and not the staged copy.</p>\n\n<p>One way to do this could be:</p>\n\n<pre><code>git diff --cached --name-only --diff-filter=ACMR | xargs git checkout-index --prefix=$TMPDIR/ --\nfind $TMPDIR -name \'*.php\' -print | xargs -n 1 php -l\n</code></pre>\n\n<p>Which would make a copy of the staged images into a scratch space and then run the test command on them there. If any of the files include other files in the build then you may have to recreate the whole staged image in the test tree and then test the changed files there (See: <a href="https://stackoverflow.com/questions/2412450/git-pre-commit-hook-changed-added-files/3068990#3068990">Git pre-commit hook : changed/added files</a>).</p>\n'}, {'answer_id': 48700175, 'author': 'Sudheesh.M.S', 'author_id': 1295321, 'author_profile': 'https://Stackoverflow.com/users/1295321', 'pm_score': 0, 'selected': False, 'text': '<p>My PHP implementation of the pre-commit hook checks whether the modified files in git are \'error free\' and are as per PSR2 standard using either \'php-code-sniffer\' or \'php-cs-fixer\' </p>\n\n<pre><code>#!/usr/local/bin/php\n&lt;?php\n    /**\n         * Collect all files which have been added, copied or\n         * modified and store them in an array - output\n         */\n        exec(\'git diff --cached --name-only --diff-filter=ACM\', $output);\n\n        $isViolated = 0;\n        $violatedFiles = array();\n        // $php_cs_path = "/usr/local/bin/php-cs-fixer";\n        $php_cs_path = "~/.composer/vendor/bin/phpcs";\n\n        foreach ($output as $fileName) {\n            // Consider only PHP file for processing\n            if (pathinfo($fileName, PATHINFO_EXTENSION) == "php") {\n                $psr_output = array();\n\n                // Put the changes to be made in $psr_output, if not as per PSR2 standard\n\n                // php-cs-fixer\n                // exec("{$php_cs_path} fix {$fileName} --rules=@PSR2 --dry-run --diff", $psr_output, $return);\n\n                // php-code-sniffer\n                exec("{$php_cs_path} --standard=PSR2 --colors -n {$fileName}", $psr_output, $return);\n\n                if ($return != 0) {\n                    $isViolated = 1;\n                    $violatedFiles[] = $fileName;\n                    echo implode("\\n", $psr_output), "\\n";\n                }\n            }\n        }\n        if ($isViolated == 1) {\n            echo "\\n---------------------------- IMPORTANT --------------------------------\\n";\n            echo "\\nPlease use the suggestions above to fix the code in the following file: \\n";\n            echo " =&gt; " . implode("\\n =&gt; ", $violatedFiles);\n            echo "\\n-----------------------------------------------------------------------\\n\\n\\n";\n            exit(1);\n        } else {\n            echo "\\n =&gt; Committed Successfully :-)\\n\\n";\n            exit(0);\n        }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/43374', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4342/']