From 33b3fb23d6201dbd6921f570660ccff667540cf6 Mon Sep 17 00:00:00 2001 From: Nicolas Potier Date: Wed, 27 Dec 2017 13:59:31 +0100 Subject: [PATCH 1/3] - change yaml format - listen to exit status in order to validate / abort commit - update doc --- README.md | 44 +++++++++++++++++++++++++++++++++++++++----- src/Handler.php | 27 +++++++++++++++++++++------ 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3af8f5b..4fef334 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,54 @@ # git-hook-handler +## Installation +Since this lib is a fork of `aequasi/git-hook-handler`, you must register this repository in your `composer.json` file + +```json +# composer.json + ... + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/acseo/git-hook-handler" + } + ], + ... + "require-dev": { + "aequasi/git-hook-handler": "^1.0", + ... +``` + +## 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 pre-commit: - - bldr run ci - - phpunit - - bin/phpcs + 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/src/Handler.php b/src/Handler.php index 37a428f..bf66fc8 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -62,14 +62,16 @@ 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 "); + foreach ($this->categories[$this->hook] as $group => $groupData) { - $process = new Process($command, __DIR__ . '../../'); + $output->writeln(['', " $group hook : ".$groupData['description']]); + + $process = new Process($groupData['command'], __DIR__ . '../../'); $process->setTimeout(null); - $output->write($command."......"); + $output->writeln([" Executed command :",'']); + $output->writeln(" ".str_replace('&&', "&& \\ \n ", $groupData['command'])); if ($process->run() === 1) { - $output->writeln(' Failed.'); $output->writeln("{$command} failed"); $output->writeln($process->getOutput()); @@ -77,7 +79,20 @@ public function doRun(InputInterface $input, OutputInterface $output) return 1; } - $output->writeln(' Success.'); + + $output->writeln([" Command Result :",'']); + + $output->writeln(" ".$process->getOutput()); + + $exitCode = $process->getExitCode(); + if (isset($groupData['exitcode']) && $groupData['exitcode'] != $exitCode) { + $output->writeln("Result is different than expected. Exiting"); + return -1; + } + else { + $output->writeln(' Success.'); + return 0; + } } $output->writeln( From a6c5e7037c48d5cf1cdfa68f72d3b6c7c3046b63 Mon Sep 17 00:00:00 2001 From: Nicolas Potier Date: Wed, 27 Dec 2017 14:48:54 +0100 Subject: [PATCH 2/3] commit on error --- README.md | 21 ++------------------- scripts/hook | 9 +++++++-- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 4fef334..dd64cde 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,12 @@ # git-hook-handler -## Installation - -Since this lib is a fork of `aequasi/git-hook-handler`, you must register this repository in your `composer.json` file - -```json -# composer.json - ... - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/acseo/git-hook-handler" - } - ], - ... - "require-dev": { - "aequasi/git-hook-handler": "^1.0", - ... -``` - ## 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: ```yaml # git-hook.yml +config: + commit-on-error : true # or false pre-commit: phpunit: description : 'Run PHPUnit' 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); From 683eeb573ac5c7282b2e6b193314e172fa7c2cc8 Mon Sep 17 00:00:00 2001 From: Nicolas Potier Date: Wed, 2 May 2018 17:18:34 +0200 Subject: [PATCH 3/3] ensure BC for Handler --- README.md | 5 ++++- src/Handler.php | 42 +++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index dd64cde..5af617d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,10 @@ Just create a `git-hooks.yml` file in your projects base directory, and fill it config: commit-on-error : true # or false pre-commit: - phpunit: + # Simple command + - bin/phpcs + # or more complex command + - phpunit: description : 'Run PHPUnit' command : phpunit exitcode : 0 diff --git a/src/Handler.php b/src/Handler.php index bf66fc8..59f813c 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -63,7 +63,19 @@ public function doRun(InputInterface $input, OutputInterface $output) } $output->writeln("Running $this->hook hook "); - foreach ($this->categories[$this->hook] as $group => $groupData) { + $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 + ]; + } $output->writeln(['', " $group hook : ".$groupData['description']]); @@ -71,30 +83,21 @@ public function doRun(InputInterface $input, OutputInterface $output) $process->setTimeout(null); $output->writeln([" Executed command :",'']); $output->writeln(" ".str_replace('&&', "&& \\ \n ", $groupData['command'])); - if ($process->run() === 1) { - $output->writeln(' Failed.'); - $output->writeln("{$command} failed"); - $output->writeln($process->getOutput()); - $output->writeln("{$command} failed"); - - return 1; - } + $process->run(); $output->writeln([" Command Result :",'']); - $output->writeln(" ".$process->getOutput()); + $output->writeln($process->getOutput()); $exitCode = $process->getExitCode(); if (isset($groupData['exitcode']) && $groupData['exitcode'] != $exitCode) { - $output->writeln("Result is different than expected. Exiting"); - return -1; + $output->writeln(" $group : Exit Code for Hook ($exitCode) is different than expected (".$groupData['exitcode'].")"); + $error = true; + } else { + $output->writeln(" $group : Success."); } - else { - $output->writeln(' Success.'); - return 0; - } - } + } $output->writeln( [ '', @@ -103,5 +106,10 @@ public function doRun(InputInterface $input, OutputInterface $output) '' ] ); + + if ($error) { + return -1; + } + return 0; } }