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 31e33315..160db0cf 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 00000000..333d115c --- /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 00000000..43e66402 --- /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 c6b66870..d2ee7c32 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 2f96fdaa..ac88c609 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 941dd8b1..687c26aa 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 ea77d142..04ec1b78 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 1aadf49b..58cb41e3 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 769f24d9..493c4381 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 43e66402..62cdce7e 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 00000000..8bd63ef5 --- /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 d2ee7c32..c6b66870 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 ac88c609..2f96fdaa 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 687c26aa..941dd8b1 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 04ec1b78..ea77d142 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 58cb41e3..1aadf49b 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 134499a7..09e414ac 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 00000000..4dc1fe36 --- /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 5aa8c8fb..c5a49a06 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 4a3b3194..92208a45 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 c40c4b90..04203898 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 f6d50653..55cc0a7a 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 4ae47dd4..97551722 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 50988de8..a7efeef5 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 d60f6614..fbd27cc8 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 9315a360..85053cef 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 2a228959..b78df822 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 9290e0ce..e36e4634 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 e2af7161..468c6784 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 46ed1ce6..febbdca5 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 1def9a99..a1cc6595 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 3c407833..39482611 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 356e85a3..1325d2f2 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 51816c3d..283c89dc 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 93d70353..0fc8d059 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 fd85a313..305aff6d 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 00fc4cd1..824ca6ed 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 f30793f8..a43b2433 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 0084f1c1..816eb553 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 c284887b..c89ccd17 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 215384e1..a2442ae3 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 65d83152..0a0b311c 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 d928df9e..4792d686 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 c4262adf..6120e779 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 d8750f07..b46fd177 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 88e7a536..c0846dda 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 602809cb..768174ce 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 e1c33a29..23744f54 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 a42a8e4e..138ef4ea 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 7ccb2358..0cda73be 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 21186a4a..c156c006 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 5a35785c..bd9addfc 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 87476dc5..a0a1bcdb 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 cfec6ae9..93555c9c 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 108c1953..21e98d24 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 1aeccdaa..88edd9ba 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 00caa55a..28e8606c 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 7fef195c..b1ae4cf5 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 0fa048bd..c7c3de43 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 26393535..58c6a935 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 c16320b3..14bd395c 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 c5ae9988..f85f1e11 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 857950bd..f86a893f 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 5c1472f8..51d07fb6 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 d5162c9c..b6179ec5 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 b4415a21..9941ddfa 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 ac23c9e7..7f2a346e 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 d33e4314..2a1686c4 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 4937f74b..cbd4fa0e 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 d57dc1a6..798b38ce 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 614e41ec..4cb562fe 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 5858a400..672f9e19 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 548d6cde..620b180c 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 bbb77ba5..20d3c03c 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 ab4b1f6c..f5fd26b1 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 dd3053d8..e232afd1 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 0f94b281..ac29947c 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 125f9669..4e008269 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 20bdaa8b..26b0eac1 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 d8c2ebd2..154e7be0 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 d0064582..55a196a6 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 dcb76fcb..d6172f58 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 ea77d142..04ec1b78 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 1aadf49b..57288884 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 160db0cf..ed2cdd13 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 62cdce7e..7b8503bf 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; } }