Forked from mardix/php-cs-fixer-pre-commit.php
Last active
September 15, 2015 10:12
-
-
Save kujiy/0b00d6b4cc087e5d421f to your computer and use it in GitHub Desktop.
A pre-commit hook to make PHP code PSR-2 compliant, check for syntax error and make Javascript code beautify with Esformatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* .git/hooks/pre-commit | |
* original -> https://gist.github.com/mardix/3623562 | |
*/ | |
/** | |
* collect all files which have been added, copied or | |
* modified and store them in an array called output | |
*/ | |
exec('git diff --cached --name-status --diff-filter=ACM', $output); | |
foreach ($output as $file) { | |
$fileName = trim(substr($file, 1) ); | |
/** | |
* PHP file | |
*/ | |
$ext = pathinfo($fileName,PATHINFO_EXTENSION); | |
if ($ext == "php") { | |
/** | |
* Check for error | |
*/ | |
$lint_output = array(); | |
exec("php -l " . escapeshellarg($fileName), $lint_output, $return); | |
if ($return == 0) { | |
/** | |
* PHP-CS-Fixer && add it back | |
*/ | |
exec("php-cs-fixer fix {$fileName} --level=psr2"); | |
exec("git add {$fileName}"); | |
//exec("echo php-cs-fixer fix {$fileName} --level=psr2 > {$fileName}.txt"); | |
} else { | |
echo "\nYour commit has php syntax error(s).\nYou MUST fix it before commit.\n"; | |
echo "See 'php -l' error massage below.\n\n---------------------"; | |
echo implode("\n", $lint_output), "\n---------------------\n"; | |
exec("echo 'Your commit has php syntax error(s).' > GIT-COMMIT-ERROR.txt"); | |
exit(1); | |
} | |
/** | |
* JS file | |
*/ | |
} elseif ($ext == "js") { | |
/** | |
* esformatter && add it back | |
*/ | |
exec("esformatter -i {$fileName} --config .esformatter"); | |
exec("git add {$fileName}"); | |
} | |
} | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment