From 98e96a8f5c7ac3061c76d92f11f4a77fa250ee64 Mon Sep 17 00:00:00 2001 From: Mohannad Elemary Date: Fri, 13 Nov 2026 17:13:41 +0200 Subject: [PATCH] fix(tia): report git errors instead of a missing dependency (#1987) * fix(tia): show git's error instead of asking to install git when it fails * fix(tia): detect a missing git binary with ExecutableFinder Only report MissingDependency when git can't be found on PATH or there is no repository. Any other failure shows git's error output, or the exit code when git printed nothing. --------- Co-authored-by: Mohannad Elemary <20709195+MohannadElemary@users.noreply.github.com> Co-authored-by: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com> --- src/Exceptions/TiaGitFailed.php | 50 ++++++++++++ src/Plugins/Tia.php | 5 +- src/Plugins/Tia/ChangedFiles.php | 50 ++++++++---- src/Support/Git.php | 3 +- .../Features/Tia/DefaultBranchResolution.php | 76 +++++++++++++++++++ 5 files changed, 165 insertions(+), 19 deletions(-) create mode 100644 src/Exceptions/TiaGitFailed.php diff --git a/src/Exceptions/TiaGitFailed.php b/src/Exceptions/TiaGitFailed.php new file mode 100644 index 00000000..885a42e6 --- /dev/null +++ b/src/Exceptions/TiaGitFailed.php @@ -0,0 +1,50 @@ +error)); + } + + public function render(OutputInterface $output): void + { + $output->writeln([ + '', + ' ERROR Tia mode could not read the git repository.', + '', + ' Git is installed, but it failed with:', + '', + ]); + + foreach (explode("\n", $this->error) as $line) { + $output->writeln(' '.OutputFormatter::escape(rtrim($line)).''); + } + + $output->writeln([ + '', + ' Runs without --tia are unaffected.', + '', + ]); + } + + public function exitCode(): int + { + return 1; + } +} diff --git a/src/Plugins/Tia.php b/src/Plugins/Tia.php index 52b7f06f..df3ad6e2 100644 --- a/src/Plugins/Tia.php +++ b/src/Plugins/Tia.php @@ -13,6 +13,7 @@ use Pest\Exceptions\InvalidOption; use Pest\Exceptions\MissingDependency; use Pest\Exceptions\NoAffectedTestsFound; use Pest\Exceptions\TiaBaselineCommitMissing; +use Pest\Exceptions\TiaGitFailed; use Pest\Exceptions\TiaRequiresCommit; use Pest\Exceptions\TiaRequiresDefaultBranch; use Pest\Exceptions\TiaRequiresRemote; @@ -867,7 +868,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument try { $this->resolveBranch($projectRoot); - } catch (MissingDependency $missingGit) { + } catch (MissingDependency|TiaGitFailed $missingGit) { $repository = new ChangedFiles($projectRoot); if ($repository->isRepository() && ! $repository->hasCommits()) { @@ -1859,7 +1860,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument try { $this->resolveBranch($projectRoot); - } catch (MissingDependency) { + } catch (MissingDependency|TiaGitFailed) { } $graph->setFallbackBranch($this->fallbackBranch); diff --git a/src/Plugins/Tia/ChangedFiles.php b/src/Plugins/Tia/ChangedFiles.php index 25efe8b0..16d833c5 100644 --- a/src/Plugins/Tia/ChangedFiles.php +++ b/src/Plugins/Tia/ChangedFiles.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace Pest\Plugins\Tia; use Pest\Exceptions\MissingDependency; +use Pest\Exceptions\TiaGitFailed; use Pest\Support\Git; +use Symfony\Component\Process\ExecutableFinder; /** * @internal @@ -179,7 +181,7 @@ final readonly class ChangedFiles ); if ($result['exitCode'] !== 0 && $result['exitCode'] !== 1) { - throw new MissingDependency('Tia mode', 'git'); + throw $this->gitFailure($result); } $output = $result['output']; @@ -199,13 +201,13 @@ final readonly class ChangedFiles public function currentBranch(): ?string { - $output = $this->git->raw(['rev-parse', '--abbrev-ref', 'HEAD']); + $result = $this->git->result(['rev-parse', '--abbrev-ref', 'HEAD']); - if ($output === null) { - throw new MissingDependency('Tia mode', 'git'); + if ($result['exitCode'] !== 0) { + throw $this->gitFailure($result); } - $branch = trim($output); + $branch = trim($result['output']); return $branch === '' || $branch === 'HEAD' ? null : $branch; } @@ -321,13 +323,13 @@ final readonly class ChangedFiles */ private function diffSinceSha(string $sha): array { - $output = $this->scan()->raw(['diff', '--name-only', '--no-renames', $sha.'..HEAD']); + $result = $this->scan()->result(['diff', '--name-only', '--no-renames', $sha.'..HEAD']); - if ($output === null) { - throw new MissingDependency('Tia mode', 'git'); + if ($result['exitCode'] !== 0) { + throw $this->gitFailure($result); } - return $this->splitLines($output); + return $this->splitLines($result['output']); } /** @@ -335,12 +337,14 @@ final readonly class ChangedFiles */ private function workingTreeChanges(): array { - $output = $this->scan()->raw(['status', '--porcelain', '-z', '--untracked-files=all']); + $result = $this->scan()->result(['status', '--porcelain', '-z', '--untracked-files=all']); - if ($output === null) { - throw new MissingDependency('Tia mode', 'git'); + if ($result['exitCode'] !== 0) { + throw $this->gitFailure($result); } + $output = $result['output']; + if ($output === '') { return []; } @@ -378,17 +382,31 @@ final readonly class ChangedFiles public function currentSha(): ?string { - $output = $this->git->raw(['rev-parse', 'HEAD']); + $result = $this->git->result(['rev-parse', 'HEAD']); - if ($output === null) { - throw new MissingDependency('Tia mode', 'git'); + if ($result['exitCode'] !== 0) { + throw $this->gitFailure($result); } - $sha = trim($output); + $sha = trim($result['output']); return $sha === '' ? null : $sha; } + /** + * @param array{exitCode: int, output: string, errorOutput: string} $result + */ + private function gitFailure(array $result): MissingDependency|TiaGitFailed + { + $error = trim($result['errorOutput']); + + if ((new ExecutableFinder)->find('git') === null || str_contains($error, 'not a git repository')) { + return new MissingDependency('Tia mode', 'git'); + } + + return new TiaGitFailed($error !== '' ? $error : sprintf('Git exited with code %d.', $result['exitCode'])); + } + /** * @return array */ diff --git a/src/Support/Git.php b/src/Support/Git.php index 03f52a00..36af5986 100644 --- a/src/Support/Git.php +++ b/src/Support/Git.php @@ -59,7 +59,7 @@ final readonly class Git /** * @param array $arguments - * @return array{exitCode: int, output: string} + * @return array{exitCode: int, output: string, errorOutput: string} */ public function result(array $arguments, ?string $input = null): array { @@ -75,6 +75,7 @@ final readonly class Git return [ 'exitCode' => $process->getExitCode() ?? 1, 'output' => $process->getOutput(), + 'errorOutput' => $process->getErrorOutput(), ]; } diff --git a/tests/Features/Tia/DefaultBranchResolution.php b/tests/Features/Tia/DefaultBranchResolution.php index ab4c020f..b9b2fb94 100644 --- a/tests/Features/Tia/DefaultBranchResolution.php +++ b/tests/Features/Tia/DefaultBranchResolution.php @@ -179,6 +179,82 @@ test('tia still requires git', function (): void { ->and($result->exitCode)->not->toBe(0); })->skipOnWindows(); +test('a failing git shows its own error instead of asking for git', function (): void { + $project = Project::make('master'); + + $project->write('shim/git', implode("\n", [ + '#!/bin/sh', + 'echo "fatal: detected dubious ownership in repository at \'$PWD\'" >&2', + 'echo "To add an exception for this directory, call:" >&2', + 'echo "" >&2', + 'echo " git config --global --add safe.directory $PWD" >&2', + 'exit 128', + '', + ])); + chmod($project->path('shim/git'), 0755); + + $result = $project->pestWithEnvironment($project->path(), [ + 'PATH' => $project->path('shim').':'.getenv('PATH'), + ], '--tia'); + + expect($result->output)->toContain('detected dubious ownership in repository') + ->and($result->output)->toContain('git config --global --add safe.directory') + ->and($result->output)->not->toContain('requires [git]') + ->and($result->exitCode)->toBe(1, $result->describe()); +})->skipOnWindows(); + +test('a git binary missing from PATH still asks for git', function (): void { + $project = Project::make('master'); + + mkdir($project->path('empty-path')); + + $result = $project->pestWithEnvironment($project->path(), [ + 'PATH' => $project->path('empty-path'), + ], '--tia'); + + expect($result->output)->toContain('The [Tia mode] feature requires [git].') + ->and($result->exitCode)->toBe(1, $result->describe()); +})->skipOnWindows(); + +test('a git failure without error output reports its exit code', function (): void { + $project = Project::make('master'); + + $project->write('shim/git', implode("\n", [ + '#!/bin/sh', + 'exit 3', + '', + ])); + chmod($project->path('shim/git'), 0755); + + $result = $project->pestWithEnvironment($project->path(), [ + 'PATH' => $project->path('shim').':'.getenv('PATH'), + ], '--tia'); + + expect($result->output)->toContain('Git exited with code 3.') + ->and($result->output)->not->toContain('requires [git]') + ->and($result->exitCode)->toBe(1, $result->describe()); +})->skipOnWindows(); + +test('a git failure with exit code 127 shows its own error', function (): void { + $project = Project::make('master'); + + $project->write('shim/git', implode("\n", [ + '#!/bin/sh', + 'echo "git: error while loading shared libraries: libpcre2-8.so.0" >&2', + 'exit 127', + '', + ])); + chmod($project->path('shim/git'), 0755); + + $result = $project->pestWithEnvironment($project->path(), [ + 'PATH' => $project->path('shim').':'.getenv('PATH'), + ], '--tia'); + + expect($result->output)->toContain('error while loading shared libraries') + ->and($result->output)->not->toContain('requires [git]') + ->and($result->exitCode)->toBe(1, $result->describe()); +})->skipOnWindows(); + test('a plain run outside a repository creates no baseline', function (): void { $project = Project::withoutGit();