mirror of
https://github.com/pestphp/pest.git
synced 2026-12-05 07:15:33 +01:00
fix(tia): support a Pest project nested in a git repository (#1914)
* fix(tia): support a Pest project nested in a git repository Tia refused to run whenever the Pest project was not the git repository root, because git prints and addresses paths relative to the repository root while the dependency graph is keyed on project-relative paths. Translate at every git boundary instead of guarding against the mismatch. `Git::pathPrefix()` resolves the directory of the project inside the repository, and `ChangedFiles` strips that prefix off the paths git prints, drops the paths of sibling packages, and prepends it to `git show`. Both diffs read `-z` records, because `core.quotePath` C-quotes a path outside ASCII, and `--no-relative` holds the frame when a user sets `diff.relative`. Three places read `<projectRoot>/.git` directly, which is absent in a nested project and in a worktree, and now ask git: `BaseRemote`, `Storage` and `Fingerprint::isTrackedByGit()`. `Storage` folds the prefix into the state key so two projects in one repository do not share a graph, and the fingerprint carries the prefix so a baseline published by a sibling project is rejected. A project at the repository root keeps the key and the fingerprint it had. `git check-ignore` runs without `--no-index`, so a tracked file that an ignore rule matches counts as part of the project on both sides of the boundary. Changes outside the project are dropped and reported by count in the run summary, so a nested project does not look idle when the repository around it moved. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(tia): keep whitespace at the edge of the paths git returns * fix(tia): run the full suite when a PHP file outside a nested project changes --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
@@ -12,15 +12,83 @@ use Symfony\Component\Process\ExecutableFinder;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final readonly class ChangedFiles
|
||||
final class ChangedFiles
|
||||
{
|
||||
private Git $git;
|
||||
private readonly Git $git;
|
||||
|
||||
public function __construct(private string $projectRoot)
|
||||
private ?string $gitPrefix = null;
|
||||
|
||||
/**
|
||||
* @var array<string, true>
|
||||
*/
|
||||
private array $outsideProject = [];
|
||||
|
||||
/**
|
||||
* @var array<string, true>
|
||||
*/
|
||||
private array $outsideProjectDirty = [];
|
||||
|
||||
public function __construct(private readonly string $projectRoot)
|
||||
{
|
||||
$this->git = new Git($projectRoot);
|
||||
}
|
||||
|
||||
public function gitPrefix(): string
|
||||
{
|
||||
return $this->gitPrefix ??= $this->git->pathPrefix();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $files repository-relative paths, as printed by git.
|
||||
* @return array<int, string> project-relative paths; paths outside the project are dropped.
|
||||
*/
|
||||
private function toProjectRelative(array $files, bool $fromWorkingTree = false): array
|
||||
{
|
||||
$prefix = $this->gitPrefix();
|
||||
|
||||
if ($prefix === '') {
|
||||
return $files;
|
||||
}
|
||||
|
||||
$projectFiles = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (str_starts_with($file, $prefix)) {
|
||||
$projectFiles[] = substr($file, strlen($prefix));
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($file === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->outsideProject[$file] = true;
|
||||
|
||||
if ($fromWorkingTree) {
|
||||
$this->outsideProjectDirty[$file] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $projectFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function outsideProject(): array
|
||||
{
|
||||
return array_map(strval(...), array_keys($this->outsideProject));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function outsideProjectDirty(): array
|
||||
{
|
||||
return array_map(strval(...), array_keys($this->outsideProjectDirty));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $files project-relative paths.
|
||||
* @param array<string, string> $lastRunTree path → content hash from last run.
|
||||
@@ -97,6 +165,9 @@ final readonly class ChangedFiles
|
||||
*/
|
||||
public function since(?string $sha): ?array
|
||||
{
|
||||
$this->outsideProject = [];
|
||||
$this->outsideProjectDirty = [];
|
||||
|
||||
$files = [];
|
||||
|
||||
if ($sha !== null && $sha !== '') {
|
||||
@@ -120,6 +191,8 @@ final readonly class ChangedFiles
|
||||
|
||||
$candidates = array_keys($this->filterIgnored($unique));
|
||||
|
||||
$this->filterIgnoredOutsideProject();
|
||||
|
||||
if ($sha !== null && $sha !== '') {
|
||||
return $this->filterBehaviourallyUnchanged($candidates, $sha);
|
||||
}
|
||||
@@ -162,7 +235,25 @@ final readonly class ChangedFiles
|
||||
|
||||
private function contentAtSha(string $sha, string $path): ?string
|
||||
{
|
||||
return $this->git->show($sha, $path);
|
||||
return $this->git->show($sha, $this->gitPrefix().$path);
|
||||
}
|
||||
|
||||
private function filterIgnoredOutsideProject(): void
|
||||
{
|
||||
if ($this->outsideProject === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$repositoryRoot = $this->git->repositoryRoot();
|
||||
|
||||
if ($repositoryRoot === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$git = new Git($repositoryRoot);
|
||||
|
||||
$this->outsideProject = $this->filterIgnoredWith($git, $this->outsideProject);
|
||||
$this->outsideProjectDirty = array_intersect_key($this->outsideProjectDirty, $this->outsideProject);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,13 +261,22 @@ final readonly class ChangedFiles
|
||||
* @return array<string, true>
|
||||
*/
|
||||
private function filterIgnored(array $candidates): array
|
||||
{
|
||||
return $this->filterIgnoredWith($this->git, $candidates);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, true> $candidates
|
||||
* @return array<string, true>
|
||||
*/
|
||||
private function filterIgnoredWith(Git $git, array $candidates): array
|
||||
{
|
||||
if ($candidates === []) {
|
||||
return $candidates;
|
||||
}
|
||||
|
||||
$result = $this->git->result(
|
||||
['check-ignore', '--no-index', '-z', '--stdin'],
|
||||
$result = $git->result(
|
||||
['check-ignore', '-z', '--stdin'],
|
||||
implode("\x00", array_keys($candidates)),
|
||||
);
|
||||
|
||||
@@ -323,13 +423,13 @@ final readonly class ChangedFiles
|
||||
*/
|
||||
private function diffSinceSha(string $sha): array
|
||||
{
|
||||
$result = $this->scan()->result(['diff', '--name-only', '--no-renames', $sha.'..HEAD']);
|
||||
$result = $this->scan()->result(['diff', '--name-only', '--no-renames', '--no-relative', '-z', $sha.'..HEAD']);
|
||||
|
||||
if ($result['exitCode'] !== 0) {
|
||||
throw $this->gitFailure($result);
|
||||
}
|
||||
|
||||
return $this->splitLines($result['output']);
|
||||
return $this->toProjectRelative($this->splitRecords($result['output']));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -377,7 +477,7 @@ final readonly class ChangedFiles
|
||||
$files[] = $path;
|
||||
}
|
||||
|
||||
return $files;
|
||||
return $this->toProjectRelative($files, fromWorkingTree: true);
|
||||
}
|
||||
|
||||
public function currentSha(): ?string
|
||||
@@ -407,6 +507,21 @@ final readonly class ChangedFiles
|
||||
return new TiaGitFailed($error !== '' ? $error : sprintf('Git exited with code %d.', $result['exitCode']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function splitRecords(string $output): array
|
||||
{
|
||||
if ($output === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values(array_filter(
|
||||
explode("\x00", rtrim($output, "\x00")),
|
||||
static fn (string $record): bool => $record !== '',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user