diff --git a/src/Plugins/Tia/ChangedFiles.php b/src/Plugins/Tia/ChangedFiles.php index 4e2d9d45..4c2e446b 100644 --- a/src/Plugins/Tia/ChangedFiles.php +++ b/src/Plugins/Tia/ChangedFiles.php @@ -297,7 +297,23 @@ final readonly class ChangedFiles private function shaIsReachable(string $sha): bool { - return $this->git->succeeds(['merge-base', '--is-ancestor', $sha, 'HEAD']); + if ($this->git->succeeds(['merge-base', '--is-ancestor', $sha, 'HEAD'])) { + return true; + } + + $parents = $this->git->raw(['show', '--no-patch', '--format=%P', $sha]); + + if ($parents === null) { + return false; + } + + $parents = explode(' ', trim($parents)); + + if (count($parents) < 2) { + return false; + } + + return array_all($parents, fn (string $parent): bool => $this->git->succeeds(['merge-base', '--is-ancestor', $parent, 'HEAD'])); } /** diff --git a/tests/Features/Tia/MergeBaselines.php b/tests/Features/Tia/MergeBaselines.php new file mode 100644 index 00000000..329a2b2e --- /dev/null +++ b/tests/Features/Tia/MergeBaselines.php @@ -0,0 +1,52 @@ +git(); + $git->switchTo('feature', new: true); + $git->commit('feature'); + $git->run(['checkout', '--detach', 'master']); + $git->run(['merge', '--no-ff', '--no-edit', 'feature']); + $project->seed('123/merge'); + + $git->switchTo($changedBranch); + $project->write('tests/Unit/CalculatorTest.php', str_replace( + '->add(1, 2)', + '->add(2, 1)', + file_get_contents($project->path('tests/Unit/CalculatorTest.php')) ?: throw new RuntimeException('Missing calculator test fixture.'), + )); + $git->commit('change calculator'); + $git->run(['checkout', '--detach', 'master']); + $git->run(['merge', '--no-ff', '--no-edit', 'feature']); + + $result = $project->pestWithEnvironment($project->path(), [ + 'GITHUB_REF_NAME' => '123/merge', + ], '--tia', ...$arguments); + + expect($result->exitCode)->toBe(0, $result->describe()) + ->and($result->output)->not->toContain('no longer reachable') + ->and($result->replayed())->toBe(4, $result->describe()) + ->and($result->tally())->toContain('6 passed'); +})->with(Project::SEQUENTIAL_AND_PARALLEL)->with(['feature', 'master'])->skipOnWindows(); + +test('a merge baseline is rejected when one of its parents is absent from the current history', function (): void { + $project = Project::make('master'); + $git = $project->git(); + $git->switchTo('feature', new: true); + $git->commit('feature'); + $git->run(['checkout', '--detach', 'master']); + $git->run(['merge', '--no-ff', '--no-edit', 'feature']); + $sha = $git->sha(); + $git->switchTo('master'); + + expect(new ChangedFiles($project->path())->since($sha))->toBeNull(); +})->skipOnWindows();