This commit is contained in:
nuno maduro
2026-08-05 18:21:34 +01:00
parent bfd5b75677
commit 71f39366c2
5 changed files with 173 additions and 17 deletions
+43
View File
@@ -219,6 +219,49 @@ final readonly class ChangedFiles
return $branch === '' || $branch === 'HEAD' ? null : $branch;
}
/**
* The repository's default branch — the one every other branch's baseline
* falls back to reading.
*
* Advisory, unlike {@see self::currentBranch()}: a repository that cannot
* answer the question is not a broken repository. A remote-less checkout
* has no `origin/HEAD`, and plenty of CI checkouts never run
* `git remote set-head`, so every step here fails soft and the caller is
* left to pick its own default.
*/
public function defaultBranch(): ?string
{
$head = $this->gitOutput(['git', 'symbolic-ref', '--short', 'refs/remotes/origin/HEAD']);
if ($head !== null) {
$branch = preg_replace('#^origin/#', '', $head);
if (is_string($branch) && $branch !== '') {
return $branch;
}
}
return $this->gitOutput(['git', 'config', '--get', 'init.defaultBranch']);
}
/**
* @param array<int, string> $command
*/
private function gitOutput(array $command): ?string
{
$process = new Process($command, $this->projectRoot);
$process->setTimeout(5.0);
$process->run();
if (! $process->isSuccessful()) {
return null;
}
$output = trim($process->getOutput());
return $output === '' ? null : $output;
}
private function shaIsReachable(string $sha): bool
{
$process = new Process(
+18
View File
@@ -60,6 +60,24 @@ final class Configuration
return $this;
}
/**
* The branch whose baseline every other branch falls back to reading.
*
* Autodetected from the repository when left unset; declare it here when
* the repository cannot answer for itself — no `origin/HEAD`, or an
* `init.defaultBranch` that disagrees with reality.
*
* @return $this
*/
public function defaultBranch(string $branch): self
{
/** @var WatchPatterns $watchPatterns */
$watchPatterns = Container::getInstance()->get(WatchPatterns::class);
$watchPatterns->setDefaultBranch($branch);
return $this;
}
/**
* @param array<string, string> $patterns glob → project-relative test dir
* @return $this
+26 -8
View File
@@ -48,6 +48,17 @@ final class Graph
*/
private array $baselines = [];
/**
* The baseline a branch with none of its own reads from.
*
* Only ever read from: a branch writes to its own key, so a fallback that
* leaked into the write path would corrupt the baseline every other branch
* depends on. Resolved once per run by the plugin — see
* {@see self::setFallbackBranch()} — because the git calls it takes are not
* free and the read path runs per test.
*/
private string $fallbackBranch = 'main';
private readonly string $projectRoot;
/** @var array<string, true>|null */
@@ -576,7 +587,12 @@ final class Graph
return $this->fingerprint;
}
public function recordedAtSha(string $branch, string $fallbackBranch = 'main'): ?string
public function setFallbackBranch(string $branch): void
{
$this->fallbackBranch = $branch;
}
public function recordedAtSha(string $branch, ?string $fallbackBranch = null): ?string
{
$baseline = $this->baselineFor($branch, $fallbackBranch);
@@ -611,7 +627,7 @@ final class Graph
$this->baselines[$branch]['results'][$testId] = $entry;
}
public function getAssertions(string $branch, string $testId, string $fallbackBranch = 'main'): ?int
public function getAssertions(string $branch, string $testId, ?string $fallbackBranch = null): ?int
{
$baseline = $this->baselineFor($branch, $fallbackBranch);
@@ -622,7 +638,7 @@ final class Graph
return $baseline['results'][$testId]['assertions'];
}
public function getTime(string $branch, string $testId, string $fallbackBranch = 'main'): ?float
public function getTime(string $branch, string $testId, ?string $fallbackBranch = null): ?float
{
$baseline = $this->baselineFor($branch, $fallbackBranch);
@@ -633,7 +649,7 @@ final class Graph
return $baseline['results'][$testId]['time'];
}
public function getResult(string $branch, string $testId, string $fallbackBranch = 'main'): ?TestStatus
public function getResult(string $branch, string $testId, ?string $fallbackBranch = null): ?TestStatus
{
$baseline = $this->baselineFor($branch, $fallbackBranch);
@@ -660,7 +676,7 @@ final class Graph
/**
* @return array<int, string>
*/
public function testFilesToRerun(string $branch, string $fallbackBranch = 'main'): array
public function testFilesToRerun(string $branch, ?string $fallbackBranch = null): array
{
$baseline = $this->baselineFor($branch, $fallbackBranch);
$files = [];
@@ -697,7 +713,7 @@ final class Graph
* collects no tests, so the run reports green without ever re-running the
* failure — and does so again on every subsequent invocation.
*/
public function hasUnlocatedTestsToRerun(string $branch, string $fallbackBranch = 'main'): bool
public function hasUnlocatedTestsToRerun(string $branch, ?string $fallbackBranch = null): bool
{
$baseline = $this->baselineFor($branch, $fallbackBranch);
@@ -808,7 +824,7 @@ final class Graph
/**
* @return array<string, string>
*/
public function lastRunTree(string $branch, string $fallbackBranch = 'main'): array
public function lastRunTree(string $branch, ?string $fallbackBranch = null): array
{
return $this->baselineFor($branch, $fallbackBranch)['tree'];
}
@@ -816,8 +832,10 @@ final class Graph
/**
* @return array{sha: ?string, tree: array<string, string>, results: array<string, array{status: int, message: string, time: float, assertions?: int, file?: string}>}
*/
private function baselineFor(string $branch, string $fallbackBranch): array
private function baselineFor(string $branch, ?string $fallbackBranch): array
{
$fallbackBranch ??= $this->fallbackBranch;
if (isset($this->baselines[$branch])) {
return $this->baselines[$branch];
}
+13
View File
@@ -44,6 +44,8 @@ final class WatchPatterns
private bool $baselined = false;
private ?string $defaultBranch = null;
public function useDefaults(string $projectRoot): void
{
$testPath = TestSuite::getInstance()->testPath;
@@ -177,6 +179,16 @@ final class WatchPatterns
return $this->baselined;
}
public function setDefaultBranch(string $branch): void
{
$this->defaultBranch = $branch;
}
public function defaultBranch(): ?string
{
return $this->defaultBranch;
}
public function reset(): void
{
$this->patterns = [];
@@ -185,6 +197,7 @@ final class WatchPatterns
$this->locally = false;
$this->filtered = false;
$this->baselined = false;
$this->defaultBranch = null;
}
private function keyMatches(string $key, string $file): bool