From c6b46497a964b5fbc52b303411ab1612a2c3d84a Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Tue, 21 May 2024 21:47:13 +0200 Subject: [PATCH 01/11] Create a pre-push command Create a pre-push command --- resources/config/console.yml | 7 ++ resources/hooks/local/pre-push | 13 +++ src/Console/Command/Git/PrePushCommand.php | 114 +++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 resources/hooks/local/pre-push create mode 100644 src/Console/Command/Git/PrePushCommand.php diff --git a/resources/config/console.yml b/resources/config/console.yml index 31e33315f..160db0cf9 100644 --- a/resources/config/console.yml +++ b/resources/config/console.yml @@ -65,6 +65,13 @@ services: - '@GrumPHP\IO\IOInterface' tags: - { name: 'console.command' } + GrumPHP\Console\Command\Git\PrePushCommand: + arguments: + - '@GrumPHP\Collection\TestSuiteCollection' + - '@GrumPHP\Runner\TaskRunner' + - '@GrumPHP\Git\GitRepository' + tags: + - { name: 'console.command' } # This one is loaded through the TestSuiteCompilerPass GrumPHP\Collection\TestSuiteCollection: diff --git a/resources/hooks/local/pre-push b/resources/hooks/local/pre-push new file mode 100644 index 000000000..333d115c8 --- /dev/null +++ b/resources/hooks/local/pre-push @@ -0,0 +1,13 @@ +#!/bin/sh + +# +# Run the hook command. +# Note: this will be replaced by the real command during copy. +# + +# Grumphp env vars +$(ENV) +export GRUMPHP_GIT_WORKING_DIR="$(git rev-parse --show-toplevel)" + +# Run GrumPHP +cd "${HOOK_EXEC_PATH}" && $(EXEC_GRUMPHP_COMMAND) $(HOOK_COMMAND) --skip-success-output diff --git a/src/Console/Command/Git/PrePushCommand.php b/src/Console/Command/Git/PrePushCommand.php new file mode 100644 index 000000000..43e66402b --- /dev/null +++ b/src/Console/Command/Git/PrePushCommand.php @@ -0,0 +1,114 @@ +setDescription('Executed by the pre-push hook'); + $this->addOption( + 'skip-success-output', + null, + InputOption::VALUE_NONE, + 'Skips the success output. This will be shown by another command in the git commit hook chain.' + ); + } + + public function execute(InputInterface $input, OutputInterface $output): int + { + $files = $this->getChangedFiles(); + + $context = ( + new TaskRunnerContext( + new GitPreCommitContext($files), + $this->testSuites->getOptional('git_pre_push') + ) + )->withSkippedSuccessOutput((bool) $input->getOption('skip-success-output')); + + $output->writeln('GrumPHP detected a pre-push command.'); + + $results = $this->taskRunner->run($context); + + return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK; + } + + protected function getChangedFiles(): FilesCollection + { + $fileCollection = new FilesCollection([]); + $fullCheck = false; + + // Loop over the commits. + while ($commit = trim((string) fgets(STDIN))) { + [$localRef, $localSha, , $remoteSha] = explode(' ', $commit); + + // Skip if we are deleting a branch or if there is no local branch. + if ($localRef === '(delete)' || $localSha === self::SHA1_EMPTY) { + return $fileCollection; + } + + // Do a full check if this is a new branch. + if ($remoteSha === self::SHA1_EMPTY) { + $fullCheck = true; + break; + } + + $command = (string) $this->repository->run('diff-tree', [ + '--no-commit-id', + '--name-only', + '-r', + $localSha, + $remoteSha, + ]); + + // Filter out empty lines. + $files = array_filter(array_map('trim', explode("\n", $command))); + foreach ($files as $file) { + // Avoid missing files and duplicates. + if (file_exists($file) && !$fileCollection->containsKey($file)) { + $fileCollection->set($file, new \SplFileInfo($file)); + } + } + } + + if ($fileCollection->isEmpty() && !$fullCheck) { + return $fileCollection; + } + + return $fileCollection; + } +} From b9603ec0c9445b0365d9647e800920a95d389993 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Tue, 21 May 2024 22:48:19 +0200 Subject: [PATCH 02/11] Allow to configure which hooks to run --- resources/config/config.yml | 1 + src/Configuration/Configuration.php | 9 +++++++++ src/Configuration/Model/HooksConfig.php | 8 +++++++- src/Console/Command/Git/DeInitCommand.php | 1 + src/Console/Command/Git/InitCommand.php | 4 ++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/resources/config/config.yml b/resources/config/config.yml index c6b668703..d2ee7c320 100644 --- a/resources/config/config.yml +++ b/resources/config/config.yml @@ -20,6 +20,7 @@ services: - '%hooks_dir%' - '%hooks_preset%' - '%git_hook_variables%' + - '%git_hooks%' GrumPHP\Configuration\Model\ParallelConfig: factory: ['GrumPHP\Configuration\Model\ParallelConfig', 'fromArray'] arguments: diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index 2f96fdaa7..ac88c6092 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -4,6 +4,7 @@ namespace GrumPHP\Configuration; +use GrumPHP\Console\Command\Git\InitCommand; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -70,6 +71,14 @@ public function getConfigTreeBuilder(): TreeBuilder $ascii->children()->variableNode('failed')->defaultValue('grumphp-grumpy.txt'); $ascii->children()->variableNode('succeeded')->defaultValue('grumphp-happy.txt'); + $rootNode->children()->arrayNode('git_hooks')->defaultValue(InitCommand::$hooks) + ->prototype('scalar') + ->validate() + ->ifNotInArray(InitCommand::$hooks) + ->thenInvalid('Invalid item %s') + ->end() + ->end(); + // parallel $parallel = $rootNode->children()->arrayNode('parallel'); $parallel->canBeDisabled(); diff --git a/src/Configuration/Model/HooksConfig.php b/src/Configuration/Model/HooksConfig.php index 941dd8b1f..687c26aa8 100644 --- a/src/Configuration/Model/HooksConfig.php +++ b/src/Configuration/Model/HooksConfig.php @@ -27,7 +27,8 @@ class HooksConfig public function __construct( ?string $dir, string $preset, - array $variables + array $variables, + private array $gitHooks, ) { $this->dir = $dir; $this->preset = $preset; @@ -48,4 +49,9 @@ public function getVariables(): array { return $this->variables; } + + public function getGitHooks(): array + { + return $this->gitHooks; + } } diff --git a/src/Console/Command/Git/DeInitCommand.php b/src/Console/Command/Git/DeInitCommand.php index ea77d1426..04ec1b786 100644 --- a/src/Console/Command/Git/DeInitCommand.php +++ b/src/Console/Command/Git/DeInitCommand.php @@ -22,6 +22,7 @@ class DeInitCommand extends Command */ protected static $hooks = [ 'pre-commit', + 'pre-push', 'commit-msg', ]; diff --git a/src/Console/Command/Git/InitCommand.php b/src/Console/Command/Git/InitCommand.php index 1aadf49bd..58cb41e36 100644 --- a/src/Console/Command/Git/InitCommand.php +++ b/src/Console/Command/Git/InitCommand.php @@ -27,6 +27,7 @@ class InitCommand extends Command */ public static $hooks = [ 'pre-commit', + 'pre-push', 'commit-msg', ]; @@ -100,6 +101,9 @@ public function execute(InputInterface $input, OutputInterface $output): int } foreach (self::$hooks as $hook) { + if (!in_array($hook, $this->hooksConfig->getGitHooks(), true)) { + continue; + } $gitHook = $this->filesystem->buildPath($gitHooksPath, $hook); $hookTemplate = $this->filesystem->guessFile( [ From 028c6d332b7e60bd0e6e54777f020bf3c81c633f Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 12:23:37 +0200 Subject: [PATCH 03/11] Add git_pre_push testsuite --- grumphp.yml.dist | 2 ++ 1 file changed, 2 insertions(+) diff --git a/grumphp.yml.dist b/grumphp.yml.dist index 769f24d9b..493c43810 100644 --- a/grumphp.yml.dist +++ b/grumphp.yml.dist @@ -39,6 +39,8 @@ grumphp: testsuites: git_pre_commit: tasks: [phpcs, phpspec, phpunit, composer, composer_normalize, yamllint, phplint, phpparser, psalm] + git_pre_push: + tasks: [phpcs, phpspec, phpunit, composer, composer_normalize, yamllint, phplint, phpparser, psalm] # On CI, we run paratest separately. For some reason this currently fails in GitHub actions. ci: tasks: [phpcs, phpspec, phpunit, composer, composer_normalize, yamllint, phplint, phpparser, psalm] From 0c1245eaefa3729a67acf119f881aea9da1b8380 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 12:24:19 +0200 Subject: [PATCH 04/11] Pre-push has its own context --- src/Console/Command/Git/PrePushCommand.php | 4 ++-- src/Task/Context/GitPrePushContext.php | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/Task/Context/GitPrePushContext.php diff --git a/src/Console/Command/Git/PrePushCommand.php b/src/Console/Command/Git/PrePushCommand.php index 43e66402b..62cdce7e5 100644 --- a/src/Console/Command/Git/PrePushCommand.php +++ b/src/Console/Command/Git/PrePushCommand.php @@ -9,7 +9,7 @@ use GrumPHP\Git\GitRepository; use GrumPHP\Runner\TaskRunner; use GrumPHP\Runner\TaskRunnerContext; -use GrumPHP\Task\Context\GitPreCommitContext; +use GrumPHP\Task\Context\GitPrePushContext; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -55,7 +55,7 @@ public function execute(InputInterface $input, OutputInterface $output): int $context = ( new TaskRunnerContext( - new GitPreCommitContext($files), + new GitPrePushContext($files), $this->testSuites->getOptional('git_pre_push') ) )->withSkippedSuccessOutput((bool) $input->getOption('skip-success-output')); diff --git a/src/Task/Context/GitPrePushContext.php b/src/Task/Context/GitPrePushContext.php new file mode 100644 index 000000000..8bd63ef54 --- /dev/null +++ b/src/Task/Context/GitPrePushContext.php @@ -0,0 +1,22 @@ +files = $files; + } + + public function getFiles(): FilesCollection + { + return $this->files; + } +} From 426a6a58d07d40173022cbf31531715fe64d3308 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 13:02:34 +0200 Subject: [PATCH 05/11] Revert "Allow to configure which hooks to run" This reverts commit ee7707e2adf117239fede12bee85842f22f4a712. --- resources/config/config.yml | 1 - src/Configuration/Configuration.php | 9 --------- src/Configuration/Model/HooksConfig.php | 8 +------- src/Console/Command/Git/DeInitCommand.php | 1 - src/Console/Command/Git/InitCommand.php | 4 ---- 5 files changed, 1 insertion(+), 22 deletions(-) diff --git a/resources/config/config.yml b/resources/config/config.yml index d2ee7c320..c6b668703 100644 --- a/resources/config/config.yml +++ b/resources/config/config.yml @@ -20,7 +20,6 @@ services: - '%hooks_dir%' - '%hooks_preset%' - '%git_hook_variables%' - - '%git_hooks%' GrumPHP\Configuration\Model\ParallelConfig: factory: ['GrumPHP\Configuration\Model\ParallelConfig', 'fromArray'] arguments: diff --git a/src/Configuration/Configuration.php b/src/Configuration/Configuration.php index ac88c6092..2f96fdaa7 100644 --- a/src/Configuration/Configuration.php +++ b/src/Configuration/Configuration.php @@ -4,7 +4,6 @@ namespace GrumPHP\Configuration; -use GrumPHP\Console\Command\Git\InitCommand; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -71,14 +70,6 @@ public function getConfigTreeBuilder(): TreeBuilder $ascii->children()->variableNode('failed')->defaultValue('grumphp-grumpy.txt'); $ascii->children()->variableNode('succeeded')->defaultValue('grumphp-happy.txt'); - $rootNode->children()->arrayNode('git_hooks')->defaultValue(InitCommand::$hooks) - ->prototype('scalar') - ->validate() - ->ifNotInArray(InitCommand::$hooks) - ->thenInvalid('Invalid item %s') - ->end() - ->end(); - // parallel $parallel = $rootNode->children()->arrayNode('parallel'); $parallel->canBeDisabled(); diff --git a/src/Configuration/Model/HooksConfig.php b/src/Configuration/Model/HooksConfig.php index 687c26aa8..941dd8b1f 100644 --- a/src/Configuration/Model/HooksConfig.php +++ b/src/Configuration/Model/HooksConfig.php @@ -27,8 +27,7 @@ class HooksConfig public function __construct( ?string $dir, string $preset, - array $variables, - private array $gitHooks, + array $variables ) { $this->dir = $dir; $this->preset = $preset; @@ -49,9 +48,4 @@ public function getVariables(): array { return $this->variables; } - - public function getGitHooks(): array - { - return $this->gitHooks; - } } diff --git a/src/Console/Command/Git/DeInitCommand.php b/src/Console/Command/Git/DeInitCommand.php index 04ec1b786..ea77d1426 100644 --- a/src/Console/Command/Git/DeInitCommand.php +++ b/src/Console/Command/Git/DeInitCommand.php @@ -22,7 +22,6 @@ class DeInitCommand extends Command */ protected static $hooks = [ 'pre-commit', - 'pre-push', 'commit-msg', ]; diff --git a/src/Console/Command/Git/InitCommand.php b/src/Console/Command/Git/InitCommand.php index 58cb41e36..1aadf49bd 100644 --- a/src/Console/Command/Git/InitCommand.php +++ b/src/Console/Command/Git/InitCommand.php @@ -27,7 +27,6 @@ class InitCommand extends Command */ public static $hooks = [ 'pre-commit', - 'pre-push', 'commit-msg', ]; @@ -101,9 +100,6 @@ public function execute(InputInterface $input, OutputInterface $output): int } foreach (self::$hooks as $hook) { - if (!in_array($hook, $this->hooksConfig->getGitHooks(), true)) { - continue; - } $gitHook = $this->filesystem->buildPath($gitHooksPath, $hook); $hookTemplate = $this->filesystem->guessFile( [ From 508ad782ae6561971c66edbf20f987986953ba4e Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 13:08:28 +0200 Subject: [PATCH 06/11] Add git_stage to metadata --- src/Task/Config/Metadata.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Task/Config/Metadata.php b/src/Task/Config/Metadata.php index 134499a7b..09e414ac8 100644 --- a/src/Task/Config/Metadata.php +++ b/src/Task/Config/Metadata.php @@ -29,6 +29,7 @@ public static function getConfigurableOptions(): \GrumPHP\Task\Config\ConfigOpti 'enabled' => true, 'task' => '', 'label' => '', + 'git_stage' => ['pre-commit', 'pre-push'], ]); } @@ -60,6 +61,11 @@ public function label(): string return (string) $this->metadata['label']; } + public function gitStages(): array + { + return $this->metadata['git_stage']; + } + public function toArray(): array { return $this->metadata; From 7c297e89ea82b84a0584b282ce595af07b8a3e2d Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 13:30:41 +0200 Subject: [PATCH 07/11] Trait to check if a given context is allowed in git_stage --- src/Task/GitContextTrait.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/Task/GitContextTrait.php diff --git a/src/Task/GitContextTrait.php b/src/Task/GitContextTrait.php new file mode 100644 index 000000000..4dc1fe365 --- /dev/null +++ b/src/Task/GitContextTrait.php @@ -0,0 +1,23 @@ + 'pre-commit', + $context instanceof GitPrePushContext => 'pre-push', + default => null, + }; + return in_array($gitStage, $this->getConfig()->getMetadata()->gitStages(), true); + } +} From 3216120b9643824cce11cb7ba0d0615f0ebcbc49 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 13:31:31 +0200 Subject: [PATCH 08/11] Use the new trait --- src/Task/AbstractExternalTask.php | 2 ++ src/Task/AbstractLinterTask.php | 2 ++ src/Task/AbstractParserTask.php | 2 ++ src/Task/Ant.php | 3 +-- src/Task/Atoum.php | 3 +-- src/Task/Behat.php | 3 +-- src/Task/Brunch.php | 3 +-- src/Task/CloverCoverage.php | 5 +++-- src/Task/Codeception.php | 3 +-- src/Task/Composer.php | 3 +-- src/Task/ComposerNormalize.php | 3 +-- src/Task/ComposerRequireChecker.php | 3 +-- src/Task/ComposerScript.php | 3 +-- src/Task/Deptrac.php | 3 +-- src/Task/DoctrineOrm.php | 3 +-- src/Task/ESLint.php | 3 +-- src/Task/Ecs.php | 5 ++--- src/Task/FileSize.php | 5 +++-- src/Task/Gherkin.php | 3 +-- src/Task/Git/Blacklist.php | 3 +-- src/Task/Git/BranchName.php | 6 ++++-- src/Task/Grunt.php | 3 +-- src/Task/Gulp.php | 3 +-- src/Task/Infection.php | 5 ++--- src/Task/JsonLint.php | 3 +-- src/Task/Kahlan.php | 3 +-- src/Task/Make.php | 3 +-- src/Task/NpmScript.php | 3 +-- src/Task/Paratest.php | 3 +-- src/Task/Pest.php | 3 +-- src/Task/Phan.php | 3 +-- src/Task/Phing.php | 3 +-- src/Task/Php7cc.php | 3 +-- src/Task/PhpArkitect.php | 3 +-- src/Task/PhpCpd.php | 3 +-- src/Task/PhpCsFixer.php | 5 ++--- src/Task/PhpLint.php | 3 +-- src/Task/PhpMd.php | 3 +-- src/Task/PhpMnd.php | 3 +-- src/Task/PhpParser.php | 3 +-- src/Task/PhpStan.php | 3 +-- src/Task/PhpVersion.php | 5 +++-- src/Task/Phpcs.php | 3 +-- src/Task/Phpspec.php | 3 +-- src/Task/Phpunit.php | 3 +-- src/Task/PhpunitBridge.php | 3 +-- src/Task/Progpilot.php | 3 +-- src/Task/Psalm.php | 5 ++--- src/Task/Rector.php | 5 ++--- src/Task/Robo.php | 3 +-- src/Task/SecurityChecker.php | 3 +-- src/Task/SecurityCheckerComposeraudit.php | 3 +-- src/Task/SecurityCheckerEnlightn.php | 3 +-- src/Task/SecurityCheckerLocal.php | 3 +-- src/Task/SecurityCheckerRoave.php | 3 +-- src/Task/SecurityCheckerSymfony.php | 3 +-- src/Task/Shell.php | 3 +-- src/Task/Stylelint.php | 3 +-- src/Task/Tester.php | 3 +-- src/Task/TwigCs.php | 5 ++--- src/Task/TwigCsFixer.php | 5 ++--- src/Task/XmlLint.php | 3 +-- src/Task/YamlLint.php | 3 +-- 63 files changed, 82 insertions(+), 127 deletions(-) diff --git a/src/Task/AbstractExternalTask.php b/src/Task/AbstractExternalTask.php index 5aa8c8fb3..c5a49a06e 100644 --- a/src/Task/AbstractExternalTask.php +++ b/src/Task/AbstractExternalTask.php @@ -14,6 +14,8 @@ */ abstract class AbstractExternalTask implements TaskInterface { + use GitContextTrait; + /** * @var TaskConfigInterface */ diff --git a/src/Task/AbstractLinterTask.php b/src/Task/AbstractLinterTask.php index 4a3b31948..92208a450 100644 --- a/src/Task/AbstractLinterTask.php +++ b/src/Task/AbstractLinterTask.php @@ -17,6 +17,8 @@ */ abstract class AbstractLinterTask implements TaskInterface { + use GitContextTrait; + /** * @var TaskConfigInterface */ diff --git a/src/Task/AbstractParserTask.php b/src/Task/AbstractParserTask.php index c40c4b90a..04203898a 100644 --- a/src/Task/AbstractParserTask.php +++ b/src/Task/AbstractParserTask.php @@ -17,6 +17,8 @@ */ abstract class AbstractParserTask implements TaskInterface { + use GitContextTrait; + /** * @var TaskConfigInterface */ diff --git a/src/Task/Ant.php b/src/Task/Ant.php index f6d506535..55cc0a7aa 100644 --- a/src/Task/Ant.php +++ b/src/Task/Ant.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -36,7 +35,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Atoum.php b/src/Task/Atoum.php index 4ae47dd42..97551722a 100644 --- a/src/Task/Atoum.php +++ b/src/Task/Atoum.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -47,7 +46,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Behat.php b/src/Task/Behat.php index 50988de8c..a7efeef5b 100644 --- a/src/Task/Behat.php +++ b/src/Task/Behat.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -43,7 +42,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Brunch.php b/src/Task/Brunch.php index d60f6614f..fbd27cc8c 100644 --- a/src/Task/Brunch.php +++ b/src/Task/Brunch.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -43,7 +42,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/CloverCoverage.php b/src/Task/CloverCoverage.php index 9315a3604..85053cef5 100644 --- a/src/Task/CloverCoverage.php +++ b/src/Task/CloverCoverage.php @@ -10,7 +10,6 @@ use GrumPHP\Task\Config\EmptyTaskConfig; use GrumPHP\Task\Config\TaskConfigInterface; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use GrumPHP\Util\Filesystem; use SimpleXMLElement; @@ -19,6 +18,8 @@ class CloverCoverage implements TaskInterface { + use GitContextTrait; + /** * @var Filesystem */ @@ -72,7 +73,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Codeception.php b/src/Task/Codeception.php index 2a228959b..b78df8222 100644 --- a/src/Task/Codeception.php +++ b/src/Task/Codeception.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -45,7 +44,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Composer.php b/src/Task/Composer.php index 9290e0ce9..e36e46344 100644 --- a/src/Task/Composer.php +++ b/src/Task/Composer.php @@ -10,7 +10,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use GrumPHP\Util\Filesystem; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -61,7 +60,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/ComposerNormalize.php b/src/Task/ComposerNormalize.php index e2af71616..468c6784b 100644 --- a/src/Task/ComposerNormalize.php +++ b/src/Task/ComposerNormalize.php @@ -8,7 +8,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Process\Process; @@ -20,7 +19,7 @@ class ComposerNormalize extends AbstractExternalTask { public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public static function getConfigurableOptions(): ConfigOptionsResolver diff --git a/src/Task/ComposerRequireChecker.php b/src/Task/ComposerRequireChecker.php index 46ed1ce66..febbdca58 100644 --- a/src/Task/ComposerRequireChecker.php +++ b/src/Task/ComposerRequireChecker.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -41,7 +40,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/ComposerScript.php b/src/Task/ComposerScript.php index 1def9a99d..a1cc6595c 100644 --- a/src/Task/ComposerScript.php +++ b/src/Task/ComposerScript.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Deptrac.php b/src/Task/Deptrac.php index 3c407833f..39482611e 100644 --- a/src/Task/Deptrac.php +++ b/src/Task/Deptrac.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -38,7 +37,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/DoctrineOrm.php b/src/Task/DoctrineOrm.php index 356e85a3a..1325d2f20 100644 --- a/src/Task/DoctrineOrm.php +++ b/src/Task/DoctrineOrm.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/ESLint.php b/src/Task/ESLint.php index 51816c3d2..283c89dcb 100644 --- a/src/Task/ESLint.php +++ b/src/Task/ESLint.php @@ -11,7 +11,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -60,7 +59,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return ($context instanceof GitPreCommitContext || $context instanceof RunContext); + return ($this->isGitContextAllowed($context) || $context instanceof RunContext); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Ecs.php b/src/Task/Ecs.php index 93d703537..0fc8d0593 100644 --- a/src/Task/Ecs.php +++ b/src/Task/Ecs.php @@ -12,7 +12,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Process\Process; @@ -48,7 +47,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface @@ -102,7 +101,7 @@ private function addPaths( FilesCollection $files, array $config ): void { - if ($context instanceof GitPreCommitContext && $config['files_on_pre_commit']) { + if ($this->isGitContextAllowed($context) && $config['files_on_pre_commit']) { $arguments->addFiles($files); return; } diff --git a/src/Task/FileSize.php b/src/Task/FileSize.php index fd85a3135..305aff6dd 100644 --- a/src/Task/FileSize.php +++ b/src/Task/FileSize.php @@ -10,12 +10,13 @@ use GrumPHP\Task\Config\EmptyTaskConfig; use GrumPHP\Task\Config\TaskConfigInterface; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; class FileSize implements TaskInterface { + use GitContextTrait; + /** * @var TaskConfigInterface */ @@ -55,7 +56,7 @@ public function withConfig(TaskConfigInterface $config): TaskInterface public function canRunInContext(ContextInterface $context): bool { - return $context instanceof RunContext || $context instanceof GitPreCommitContext; + return $context instanceof RunContext || $this->isGitContextAllowed($context); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Gherkin.php b/src/Task/Gherkin.php index 00fc4cd1c..824ca6edb 100644 --- a/src/Task/Gherkin.php +++ b/src/Task/Gherkin.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -38,7 +37,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Git/Blacklist.php b/src/Task/Git/Blacklist.php index f30793f8a..a43b24333 100644 --- a/src/Task/Git/Blacklist.php +++ b/src/Task/Git/Blacklist.php @@ -12,7 +12,6 @@ use GrumPHP\Task\AbstractExternalTask; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -63,7 +62,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext; + return $this->isGitContextAllowed($context); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Git/BranchName.php b/src/Task/Git/BranchName.php index 0084f1c1a..816eb5530 100644 --- a/src/Task/Git/BranchName.php +++ b/src/Task/Git/BranchName.php @@ -12,14 +12,16 @@ use GrumPHP\Task\Config\EmptyTaskConfig; use GrumPHP\Task\Config\TaskConfigInterface; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; +use GrumPHP\Task\GitContextTrait; use GrumPHP\Util\Regex; use GrumPHP\Task\TaskInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class BranchName implements TaskInterface { + use GitContextTrait; + /** * @var TaskConfigInterface */ @@ -69,7 +71,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof RunContext || $context instanceof GitPreCommitContext; + return $context instanceof RunContext || $this->isGitContextAllowed($context); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Grunt.php b/src/Task/Grunt.php index c284887ba..c89ccd178 100644 --- a/src/Task/Grunt.php +++ b/src/Task/Grunt.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Gulp.php b/src/Task/Gulp.php index 215384e1a..a2442ae30 100644 --- a/src/Task/Gulp.php +++ b/src/Task/Gulp.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Infection.php b/src/Task/Infection.php index 65d83152d..0a0b311c5 100644 --- a/src/Task/Infection.php +++ b/src/Task/Infection.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -56,7 +55,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** @@ -86,7 +85,7 @@ public function run(ContextInterface $context): TaskResultInterface $arguments->addOptionalArgument('--min-covered-msi=%s', $config['min_covered_msi']); $arguments->addOptionalCommaSeparatedArgument('--mutators=%s', $config['mutators']); - if ($context instanceof GitPreCommitContext) { + if ($this->isGitContextAllowed($context)) { $arguments->addArgumentWithCommaSeparatedFiles('--filter=%s', $files); } diff --git a/src/Task/JsonLint.php b/src/Task/JsonLint.php index d928df9e2..4792d6863 100644 --- a/src/Task/JsonLint.php +++ b/src/Task/JsonLint.php @@ -10,7 +10,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -41,7 +40,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Kahlan.php b/src/Task/Kahlan.php index c4262adfb..6120e7794 100644 --- a/src/Task/Kahlan.php +++ b/src/Task/Kahlan.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -72,7 +71,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Make.php b/src/Task/Make.php index d8750f07d..b46fd1776 100644 --- a/src/Task/Make.php +++ b/src/Task/Make.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/NpmScript.php b/src/Task/NpmScript.php index 88e7a5361..c0846ddae 100644 --- a/src/Task/NpmScript.php +++ b/src/Task/NpmScript.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -43,7 +42,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Paratest.php b/src/Task/Paratest.php index 602809cb8..768174ce6 100644 --- a/src/Task/Paratest.php +++ b/src/Task/Paratest.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -60,7 +59,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Pest.php b/src/Task/Pest.php index e1c33a293..23744f547 100644 --- a/src/Task/Pest.php +++ b/src/Task/Pest.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -38,7 +37,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Phan.php b/src/Task/Phan.php index a42a8e4ec..138ef4ea6 100644 --- a/src/Task/Phan.php +++ b/src/Task/Phan.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -43,7 +42,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Phing.php b/src/Task/Phing.php index 7ccb23585..0cda73be3 100644 --- a/src/Task/Phing.php +++ b/src/Task/Phing.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Php7cc.php b/src/Task/Php7cc.php index 21186a4a3..c156c0069 100644 --- a/src/Task/Php7cc.php +++ b/src/Task/Php7cc.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof RunContext || $context instanceof GitPreCommitContext; + return $context instanceof RunContext || $this->isGitContextAllowed($context); } /** diff --git a/src/Task/PhpArkitect.php b/src/Task/PhpArkitect.php index 5a35785cd..bd9addfc6 100644 --- a/src/Task/PhpArkitect.php +++ b/src/Task/PhpArkitect.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/PhpCpd.php b/src/Task/PhpCpd.php index 87476dc5b..a0a1bcdb6 100644 --- a/src/Task/PhpCpd.php +++ b/src/Task/PhpCpd.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -45,7 +44,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/PhpCsFixer.php b/src/Task/PhpCsFixer.php index cfec6ae9b..93555c9c2 100644 --- a/src/Task/PhpCsFixer.php +++ b/src/Task/PhpCsFixer.php @@ -10,7 +10,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Process\Process; @@ -58,7 +57,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** @@ -97,7 +96,7 @@ public function run(ContextInterface $context): TaskResultInterface $arguments->addOptionalArgument('--diff', $config['diff']); $arguments->add('fix'); - if ($context instanceof GitPreCommitContext || !$config['config_contains_finder']) { + if ($this->isGitContextAllowed($context) || !$config['config_contains_finder']) { $arguments->addFiles($files); } diff --git a/src/Task/PhpLint.php b/src/Task/PhpLint.php index 108c1953a..21e98d242 100644 --- a/src/Task/PhpLint.php +++ b/src/Task/PhpLint.php @@ -10,7 +10,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Process\Process; @@ -42,7 +41,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof RunContext || $context instanceof GitPreCommitContext; + return $context instanceof RunContext || $this->isGitContextAllowed($context); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/PhpMd.php b/src/Task/PhpMd.php index 1aeccdaa7..88edd9bac 100644 --- a/src/Task/PhpMd.php +++ b/src/Task/PhpMd.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -44,7 +43,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/PhpMnd.php b/src/Task/PhpMnd.php index 00caa55a4..28e8606c2 100644 --- a/src/Task/PhpMnd.php +++ b/src/Task/PhpMnd.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -57,7 +56,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/PhpParser.php b/src/Task/PhpParser.php index 7fef195c6..b1ae4cf56 100644 --- a/src/Task/PhpParser.php +++ b/src/Task/PhpParser.php @@ -6,7 +6,6 @@ use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use GrumPHP\Runner\TaskResult; use GrumPHP\Runner\TaskResultInterface; @@ -44,7 +43,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/PhpStan.php b/src/Task/PhpStan.php index 0fa048bd7..c7c3de43f 100644 --- a/src/Task/PhpStan.php +++ b/src/Task/PhpStan.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -60,7 +59,7 @@ function ($value) { */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/PhpVersion.php b/src/Task/PhpVersion.php index 26393535a..58c6a935f 100644 --- a/src/Task/PhpVersion.php +++ b/src/Task/PhpVersion.php @@ -11,13 +11,14 @@ use GrumPHP\Task\Config\EmptyTaskConfig; use GrumPHP\Task\Config\TaskConfigInterface; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use GrumPHP\Util\PhpVersion as PhpVersionUtility; use Symfony\Component\OptionsResolver\OptionsResolver; class PhpVersion implements TaskInterface { + use GitContextTrait; + /** * @var TaskConfigInterface */ @@ -36,7 +37,7 @@ public function __construct(PhpVersionUtility $phpVersionUtility) public function canRunInContext(ContextInterface $context): bool { - return $context instanceof RunContext || $context instanceof GitPreCommitContext; + return $context instanceof RunContext || $this->isGitContextAllowed($context); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Phpcs.php b/src/Task/Phpcs.php index c16320b31..14bd395c9 100644 --- a/src/Task/Phpcs.php +++ b/src/Task/Phpcs.php @@ -14,7 +14,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\Console\Exception\CommandNotFoundException; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -70,7 +69,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Phpspec.php b/src/Task/Phpspec.php index c5ae99883..f85f1e111 100644 --- a/src/Task/Phpspec.php +++ b/src/Task/Phpspec.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -38,7 +37,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Phpunit.php b/src/Task/Phpunit.php index 857950bd5..f86a893f2 100644 --- a/src/Task/Phpunit.php +++ b/src/Task/Phpunit.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -42,7 +41,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/PhpunitBridge.php b/src/Task/PhpunitBridge.php index 5c1472f8c..51d07fb68 100644 --- a/src/Task/PhpunitBridge.php +++ b/src/Task/PhpunitBridge.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -42,7 +41,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return ($context instanceof GitPreCommitContext || $context instanceof RunContext); + return ($this->isGitContextAllowed($context) || $context instanceof RunContext); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Progpilot.php b/src/Task/Progpilot.php index d5162c9cf..b6179ec57 100644 --- a/src/Task/Progpilot.php +++ b/src/Task/Progpilot.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -34,7 +33,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return ($context instanceof GitPreCommitContext || $context instanceof RunContext); + return ($this->isGitContextAllowed($context) || $context instanceof RunContext); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Psalm.php b/src/Task/Psalm.php index b4415a21f..9941ddfae 100644 --- a/src/Task/Psalm.php +++ b/src/Task/Psalm.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -54,7 +53,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return ($context instanceof GitPreCommitContext || $context instanceof RunContext); + return ($this->isGitContextAllowed($context) || $context instanceof RunContext); } public function run(ContextInterface $context): TaskResultInterface @@ -78,7 +77,7 @@ public function run(ContextInterface $context): TaskResultInterface $arguments->addOptionalArgument('--threads=%d', $config['threads']); $arguments->addOptionalBooleanArgument('--show-info=%s', $config['show_info'], 'true', 'false'); - if ($context instanceof GitPreCommitContext) { + if ($this->isGitContextAllowed($context)) { $arguments->addFiles($files); } diff --git a/src/Task/Rector.php b/src/Task/Rector.php index ac23c9e7a..7f2a346ec 100644 --- a/src/Task/Rector.php +++ b/src/Task/Rector.php @@ -10,7 +10,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Process\Process; @@ -42,7 +41,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof RunContext || $context instanceof GitPreCommitContext; + return $context instanceof RunContext || $this->isGitContextAllowed($context); } public function run(ContextInterface $context): TaskResultInterface @@ -68,7 +67,7 @@ public function run(ContextInterface $context): TaskResultInterface $arguments->addOptionalArgument('--clear-cache', $config['clear_cache']); $arguments->addOptionalArgument('--no-diffs', $config['no_diffs']); - if ($context instanceof GitPreCommitContext) { + if ($this->isGitContextAllowed($context)) { $arguments->addFiles($files); } diff --git a/src/Task/Robo.php b/src/Task/Robo.php index d33e43140..2a1686c47 100644 --- a/src/Task/Robo.php +++ b/src/Task/Robo.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,7 +38,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/SecurityChecker.php b/src/Task/SecurityChecker.php index 4937f74b7..cbd4fa0e7 100644 --- a/src/Task/SecurityChecker.php +++ b/src/Task/SecurityChecker.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -40,7 +39,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/SecurityCheckerComposeraudit.php b/src/Task/SecurityCheckerComposeraudit.php index d57dc1a69..798b38ce8 100644 --- a/src/Task/SecurityCheckerComposeraudit.php +++ b/src/Task/SecurityCheckerComposeraudit.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -40,7 +39,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/SecurityCheckerEnlightn.php b/src/Task/SecurityCheckerEnlightn.php index 614e41ec2..4cb562fe7 100644 --- a/src/Task/SecurityCheckerEnlightn.php +++ b/src/Task/SecurityCheckerEnlightn.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -34,7 +33,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/SecurityCheckerLocal.php b/src/Task/SecurityCheckerLocal.php index 5858a400a..672f9e195 100644 --- a/src/Task/SecurityCheckerLocal.php +++ b/src/Task/SecurityCheckerLocal.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -38,7 +37,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/SecurityCheckerRoave.php b/src/Task/SecurityCheckerRoave.php index 548d6cdec..620b180c5 100644 --- a/src/Task/SecurityCheckerRoave.php +++ b/src/Task/SecurityCheckerRoave.php @@ -10,7 +10,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use GrumPHP\Util\Filesystem; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -50,7 +49,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/SecurityCheckerSymfony.php b/src/Task/SecurityCheckerSymfony.php index bbb77ba5a..20d3c03ca 100644 --- a/src/Task/SecurityCheckerSymfony.php +++ b/src/Task/SecurityCheckerSymfony.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -36,7 +35,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Shell.php b/src/Task/Shell.php index ab4b1f6cc..f5fd26b1c 100644 --- a/src/Task/Shell.php +++ b/src/Task/Shell.php @@ -10,7 +10,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -50,7 +49,7 @@ function ($script) { */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/Stylelint.php b/src/Task/Stylelint.php index dd3053d87..e232afd18 100644 --- a/src/Task/Stylelint.php +++ b/src/Task/Stylelint.php @@ -11,7 +11,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Process\Process; @@ -83,7 +82,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return ($context instanceof GitPreCommitContext || $context instanceof RunContext); + return ($this->isGitContextAllowed($context) || $context instanceof RunContext); } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/Tester.php b/src/Task/Tester.php index 0f94b281a..ac29947c8 100644 --- a/src/Task/Tester.php +++ b/src/Task/Tester.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -58,7 +57,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface diff --git a/src/Task/TwigCs.php b/src/Task/TwigCs.php index 125f96694..4e0082690 100644 --- a/src/Task/TwigCs.php +++ b/src/Task/TwigCs.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -42,7 +41,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface @@ -71,7 +70,7 @@ public function run(ContextInterface $context): TaskResultInterface ); $arguments->addArgumentArray('--exclude=%s', $exclude); - if ($context instanceof GitPreCommitContext) { + if ($this->isGitContextAllowed($context)) { $arguments->addFiles($files); } diff --git a/src/Task/TwigCsFixer.php b/src/Task/TwigCsFixer.php index 20bdaa8b5..26b0eac15 100644 --- a/src/Task/TwigCsFixer.php +++ b/src/Task/TwigCsFixer.php @@ -9,7 +9,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; use GrumPHP\Fixer\Provider\FixableProcessResultProvider; @@ -45,7 +44,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface @@ -62,7 +61,7 @@ public function run(ContextInterface $context): TaskResultInterface $arguments = $this->processBuilder->createArgumentsForCommand('twig-cs-fixer'); $arguments->add('lint'); - if ($context instanceof GitPreCommitContext) { + if ($this->isGitContextAllowed($context)) { $arguments->addFiles($files); } diff --git a/src/Task/XmlLint.php b/src/Task/XmlLint.php index d8c2ebd21..154e7be08 100644 --- a/src/Task/XmlLint.php +++ b/src/Task/XmlLint.php @@ -10,7 +10,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -49,7 +48,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver */ public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } /** diff --git a/src/Task/YamlLint.php b/src/Task/YamlLint.php index d0064582b..55a196a6a 100644 --- a/src/Task/YamlLint.php +++ b/src/Task/YamlLint.php @@ -11,7 +11,6 @@ use GrumPHP\Runner\TaskResultInterface; use GrumPHP\Task\Config\ConfigOptionsResolver; use GrumPHP\Task\Context\ContextInterface; -use GrumPHP\Task\Context\GitPreCommitContext; use GrumPHP\Task\Context\RunContext; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -48,7 +47,7 @@ public static function getConfigurableOptions(): ConfigOptionsResolver public function canRunInContext(ContextInterface $context): bool { - return $context instanceof GitPreCommitContext || $context instanceof RunContext; + return $this->isGitContextAllowed($context) || $context instanceof RunContext; } public function run(ContextInterface $context): TaskResultInterface From 1300c44def3ae704352c4a8bf50e6699e95b358e Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 14:06:24 +0200 Subject: [PATCH 09/11] Fix existing PHPSpec test --- spec/Task/Config/MetadataSpec.php | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/Task/Config/MetadataSpec.php b/spec/Task/Config/MetadataSpec.php index dcb76fcb3..d6172f587 100644 --- a/spec/Task/Config/MetadataSpec.php +++ b/spec/Task/Config/MetadataSpec.php @@ -28,6 +28,7 @@ public function it_contains_an_options_resolver_with_default(): void 'enabled' => true, 'task' => '', 'label' => '', + 'git_stage' => ['pre-commit', 'pre-push'], ]); } From e1f02e9b20b4764115fa3c39bbe482cf5acebb9b Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 14:27:47 +0200 Subject: [PATCH 10/11] Init/deinit pre-push --- src/Console/Command/Git/DeInitCommand.php | 1 + src/Console/Command/Git/InitCommand.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Console/Command/Git/DeInitCommand.php b/src/Console/Command/Git/DeInitCommand.php index ea77d1426..04ec1b786 100644 --- a/src/Console/Command/Git/DeInitCommand.php +++ b/src/Console/Command/Git/DeInitCommand.php @@ -22,6 +22,7 @@ class DeInitCommand extends Command */ protected static $hooks = [ 'pre-commit', + 'pre-push', 'commit-msg', ]; diff --git a/src/Console/Command/Git/InitCommand.php b/src/Console/Command/Git/InitCommand.php index 1aadf49bd..57288884c 100644 --- a/src/Console/Command/Git/InitCommand.php +++ b/src/Console/Command/Git/InitCommand.php @@ -27,6 +27,7 @@ class InitCommand extends Command */ public static $hooks = [ 'pre-commit', + 'pre-push', 'commit-msg', ]; From 10ef1e720e499e0e9e29a92d9937c1268b9e7e92 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 26 May 2024 17:14:57 +0200 Subject: [PATCH 11/11] Handle new branch scenario --- resources/config/console.yml | 1 + src/Console/Command/Git/PrePushCommand.php | 46 +++++++++------------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/resources/config/console.yml b/resources/config/console.yml index 160db0cf9..ed2cdd13a 100644 --- a/resources/config/console.yml +++ b/resources/config/console.yml @@ -70,6 +70,7 @@ services: - '@GrumPHP\Collection\TestSuiteCollection' - '@GrumPHP\Runner\TaskRunner' - '@GrumPHP\Git\GitRepository' + - '@GrumPHP\Locator\RegisteredFiles' tags: - { name: 'console.command' } diff --git a/src/Console/Command/Git/PrePushCommand.php b/src/Console/Command/Git/PrePushCommand.php index 62cdce7e5..7b8503bfb 100644 --- a/src/Console/Command/Git/PrePushCommand.php +++ b/src/Console/Command/Git/PrePushCommand.php @@ -7,6 +7,7 @@ use GrumPHP\Collection\FilesCollection; use GrumPHP\Collection\TestSuiteCollection; use GrumPHP\Git\GitRepository; +use GrumPHP\Locator\RegisteredFiles; use GrumPHP\Runner\TaskRunner; use GrumPHP\Runner\TaskRunnerContext; use GrumPHP\Task\Context\GitPrePushContext; @@ -26,9 +27,10 @@ class PrePushCommand extends Command const SHA1_EMPTY = '0000000000000000000000000000000000000000'; public function __construct( - private TestSuiteCollection $testSuites, - private TaskRunner $taskRunner, - private GitRepository $repository, + private readonly TestSuiteCollection $testSuites, + private readonly TaskRunner $taskRunner, + private readonly GitRepository $repository, + private readonly RegisteredFiles $registeredFilesLocator, ) { parent::__construct(); } @@ -52,6 +54,9 @@ protected function configure(): void public function execute(InputInterface $input, OutputInterface $output): int { $files = $this->getChangedFiles(); + if ($files === null) { + return self::EXIT_CODE_OK; + } $context = ( new TaskRunnerContext( @@ -67,48 +72,35 @@ public function execute(InputInterface $input, OutputInterface $output): int return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK; } - protected function getChangedFiles(): FilesCollection + protected function getChangedFiles(): ?FilesCollection { $fileCollection = new FilesCollection([]); - $fullCheck = false; // Loop over the commits. while ($commit = trim((string) fgets(STDIN))) { [$localRef, $localSha, , $remoteSha] = explode(' ', $commit); - // Skip if we are deleting a branch or if there is no local branch. if ($localRef === '(delete)' || $localSha === self::SHA1_EMPTY) { - return $fileCollection; + // A branch has been deleted or there's no local branch. + return null; } - // Do a full check if this is a new branch. if ($remoteSha === self::SHA1_EMPTY) { - $fullCheck = true; - break; + // Do a full check if this is a new branch. + return $this->registeredFilesLocator->locate(); } - $command = (string) $this->repository->run('diff-tree', [ - '--no-commit-id', - '--name-only', - '-r', - $localSha, - $remoteSha, - ]); - - // Filter out empty lines. - $files = array_filter(array_map('trim', explode("\n", $command))); - foreach ($files as $file) { - // Avoid missing files and duplicates. - if (file_exists($file) && !$fileCollection->containsKey($file)) { + $args = ['--no-commit-id', '--name-only', '-r', $localSha, $remoteSha]; + $command = (string) $this->repository->run('diff-tree', $args); + + foreach (explode("\n", $command) as $file) { + // Avoid empty lines, missing files, and duplicates. + if (!empty($file) && file_exists($file) && !$fileCollection->containsKey($file)) { $fileCollection->set($file, new \SplFileInfo($file)); } } } - if ($fileCollection->isEmpty() && !$fullCheck) { - return $fileCollection; - } - return $fileCollection; } }