Is there a better way to write a git pre-commit hook to check any php file in commit for parsing errors?

What am i still

#!/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 
+6
git version-control php
source share
4 answers

If the commit is a partial commit (not all changes in the working tree are committed), then this gives incorrect results, because it checks the working copy, not the phased copy.

One way to do this is to:

 git diff --cached --name-only --diff-filter=ACMR | xargs git checkout-index --prefix=$TMPDIR/ -- find $TMPDIR -name '*.php' -print | xargs -n 1 php -l 

To make a copy of phased images into the scratch space, and then run a test command there. If any of the files contains other files in the assembly, you may need to recreate the entire phased image in the test tree and then test the modified files there (see Git commit hook: modified / added files ).

+1
source share

I'm sorry if this is offtopic, but shouldn't you run some kind of automatic tests (which would imply that the code has no syntax errors) before committing?

+3
source share

If you have php5-cli installed, you can write a preliminary commit in PHP and use a syntax that is more familiar to you.

Just do something more similar.

 #!/usr/bin/php <?php /* Your pre-commit check. */ ?> 
0
source share

My pre-commit PHP implementation checks if the modified files in git are "error free" and conform to the PSR2 standard using either "php-code-sniffer" or "php-cs-fixer"

 #!/usr/local/bin/php <?php /** * Collect all files which have been added, copied or * modified and store them in an array - output */ exec('git diff --cached --name-only --diff-filter=ACM', $output); $isViolated = 0; $violatedFiles = array(); // $php_cs_path = "/usr/local/bin/php-cs-fixer"; $php_cs_path = "~/.composer/vendor/bin/phpcs"; foreach ($output as $fileName) { // Consider only PHP file for processing if (pathinfo($fileName, PATHINFO_EXTENSION) == "php") { $psr_output = array(); // Put the changes to be made in $psr_output, if not as per PSR2 standard // php-cs-fixer // exec("{$php_cs_path} fix {$fileName} --rules=@PSR2 --dry-run --diff", $psr_output, $return); // php-code-sniffer exec("{$php_cs_path} --standard=PSR2 --colors -n {$fileName}", $psr_output, $return); if ($return != 0) { $isViolated = 1; $violatedFiles[] = $fileName; echo implode("\n", $psr_output), "\n"; } } } if ($isViolated == 1) { echo "\n---------------------------- IMPORTANT --------------------------------\n"; echo "\nPlease use the suggestions above to fix the code in the following file: \n"; echo " => " . implode("\n => ", $violatedFiles); echo "\n-----------------------------------------------------------------------\n\n\n"; exit(1); } else { echo "\n => Committed Successfully :-)\n\n"; exit(0); } 
0
source share

All Articles