diff --git a/README.md b/README.md index 3af8f5b..5af617d 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,40 @@ # git-hook-handler - +## Usage Just create a `git-hooks.yml` file in your projects base directory, and fill it with an array of commands you want to run: -```yml +```yaml +# git-hook.yml +config: + commit-on-error : true # or false pre-commit: - - bldr run ci - - phpunit + # Simple command - bin/phpcs + # or more complex command + - phpunit: + description : 'Run PHPUnit' + command : phpunit + exitcode : 0 + phpcs-fixer: + description : 'Checking PHP Syntax with PHP-CS-FIXER' + exitcode : 0 + command : >4 + COMMIT_RANGE='HEAD~..HEAD' && + CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}") && + if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php_cs(\\.dist)?|composer\\.lock)$"; then IFS=$'\n' EXTRA_ARGS=('--path-mode=intersection' '--' ${CHANGED_FILES[@]}); fi + && ./vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --using-cache=no "${EXTRA_ARGS[@]}" + post-merge: - - bldr run compileAssets + command_name: + description : 'lorem ipsum' + command : 'mycommand' + exitcode : 0 ``` and then place the following in your composer.json, then run `composer install` or `composer update` ```json +# composer.json "scripts": { "pre-update-cmd": "Aequasi\\HookHandler\\HookScript::install", "pre-install-cmd": "Aequasi\\HookHandler\\HookScript::install" diff --git a/scripts/hook b/scripts/hook index c9d77b8..ce6633d 100755 --- a/scripts/hook +++ b/scripts/hook @@ -7,7 +7,6 @@ require_once $baseDir . '/vendor/autoload.php'; use Symfony\Component\Yaml\Yaml; - $config = $baseDir.'/git-hooks.yml'; $commands = []; if (file_exists($config)) { @@ -16,4 +15,10 @@ if (file_exists($config)) { $handler = new \Aequasi\HookHandler\Handler($commands, basename(__FILE__)); -$handler->run(); +$exitCode = $handler->run(); + +if (isset($commands['config']) && isset($command['config']['commit-on-error']) && $command['config']['commit-on-error']) { + exit(0); +} + +exit($exitCode); diff --git a/src/Handler.php b/src/Handler.php index 6841887..c7b12d9 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -62,24 +62,42 @@ public function doRun(InputInterface $input, OutputInterface $output) $this->categories[$this->hook] = []; } - foreach ($this->categories[$this->hook] as $command) { - $output->writeln(" Running $this->hook hook "); + $output->writeln("Running $this->hook hook "); + $error = false; + foreach ($this->categories[$this->hook] as $hookItem) { + if (is_array($hookItem)) { + $group = key($hookItem); + $groupData = $hookItem[$group]; + } + else { + $group = "Unnamed"; + $groupData = [ + 'description' => 'This hook was not named in git-hooks.yml file.', + 'command' => $hookItem + ]; + } - $process = new Process($command, __DIR__ . '/../../../../'); + $output->writeln(['', " $group hook : ".$groupData['description']]); + + $process = new Process($groupData['command'], __DIR__ . '../../../../'); $process->setTimeout(null); - $output->write($command."......"); - if ($process->run() === 1) { + $output->writeln([" Executed command :",'']); + $output->writeln(" ".str_replace('&&', "&& \\ \n ", $groupData['command'])); + $process->run(); + + $output->writeln([" Command Result :",'']); - $output->writeln(' Failed.'); - $output->writeln("{$command} failed"); - $output->writeln($process->getOutput()); - $output->writeln("{$command} failed"); + $output->writeln($process->getOutput()); - return 1; + $exitCode = $process->getExitCode(); + if (isset($groupData['exitcode']) && $groupData['exitcode'] != $exitCode) { + $output->writeln(" $group : Exit Code for Hook ($exitCode) is different than expected (".$groupData['exitcode'].")"); + $error = true; + } else { + $output->writeln(" $group : Success."); } - $output->writeln(' Success.'); - } + } $output->writeln( [ '', @@ -88,5 +106,10 @@ public function doRun(InputInterface $input, OutputInterface $output) '' ] ); + + if ($error) { + return -1; + } + return 0; } }