Created
December 16, 2024 10:45
-
-
Save aleduca/d8f9d131b30e89b936d449ef8a97355f to your computer and use it in GitHub Desktop.
Git hooks com php
This file contains hidden or 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/env php | |
<?php | |
# Se foi feito um commit | |
exec('git rev-parse --verify HEAD 2>/dev/null', $output, $returnCode); | |
# Se nenhum commit foi feito então faz sem nenhuma verificação(phpunit e phpstan) | |
if($returnCode !== 0) { | |
echo "\033[31m⚠️Nenhum commit feito anteriormente\033[0m\n" . PHP_EOL; | |
echo "\033[32m⚠️Commit sendo executado...\033[0m\n" . PHP_EOL; | |
exit(0); | |
} | |
function runCommand(string $command): int | |
{ | |
echo "Running: $command\n"; | |
// Executa o comando e captura o output | |
exec($command, $output, $returnCode); | |
// Converte o array de output em string | |
$outputText = implode("\n", $output); | |
if ($returnCode !== 0) { | |
// Mensagem em vermelho para erro | |
echo "\033[31mError running: $command\033[0m\n"; | |
echo $outputText . "\n"; | |
echo "Aborting commit...\n"; | |
exit(1); | |
} | |
// Mensagem em verde para sucesso | |
echo "\033[32mSuccess: $command\033[0m\n"; | |
echo $outputText . "\n"; | |
return $returnCode; | |
} | |
// Run PHPUnit | |
runCommand('vendor\bin\phpunit --coverage-text'); | |
// Run PHPStan | |
runCommand('vendor\bin\phpstan'); | |
exit(1) #Não faz o commit | |
# exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment