mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
a2d3e6badd
* 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>
153 lines
3.7 KiB
PHP
153 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pest\Plugins\Tia;
|
|
|
|
use Pest\Support\Git;
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
final class Storage
|
|
{
|
|
private static ?string $directory = null;
|
|
|
|
public static function tempDir(string $projectRoot): string
|
|
{
|
|
if (self::$directory !== null) {
|
|
$isAbsolute = str_starts_with(self::$directory, DIRECTORY_SEPARATOR)
|
|
|| preg_match('/^[a-z]:[\\\\\/]/i', self::$directory) === 1;
|
|
|
|
return $isAbsolute ? self::$directory : $projectRoot.DIRECTORY_SEPARATOR.self::$directory;
|
|
}
|
|
|
|
$home = self::homeDir();
|
|
|
|
if ($home === null) {
|
|
return $projectRoot
|
|
.DIRECTORY_SEPARATOR.'.pest'
|
|
.DIRECTORY_SEPARATOR.'tia';
|
|
}
|
|
|
|
return $home
|
|
.DIRECTORY_SEPARATOR.'.pest'
|
|
.DIRECTORY_SEPARATOR.'tia'
|
|
.DIRECTORY_SEPARATOR.self::projectKey($projectRoot);
|
|
}
|
|
|
|
public static function useDirectory(?string $directory): void
|
|
{
|
|
self::$directory = $directory;
|
|
}
|
|
|
|
public static function purge(string $projectRoot): void
|
|
{
|
|
$dir = self::tempDir($projectRoot);
|
|
|
|
if (! is_dir($dir)) {
|
|
return;
|
|
}
|
|
|
|
self::removeRecursive($dir);
|
|
}
|
|
|
|
private static function removeRecursive(string $dir): void
|
|
{
|
|
$entries = @scandir($dir);
|
|
|
|
if ($entries === false) {
|
|
return;
|
|
}
|
|
|
|
foreach ($entries as $entry) {
|
|
if ($entry === '.') {
|
|
continue;
|
|
}
|
|
if ($entry === '..') {
|
|
continue;
|
|
}
|
|
$path = $dir.DIRECTORY_SEPARATOR.$entry;
|
|
|
|
if (is_dir($path) && ! is_link($path)) {
|
|
self::removeRecursive($path);
|
|
|
|
continue;
|
|
}
|
|
|
|
@unlink($path);
|
|
}
|
|
|
|
@rmdir($dir);
|
|
}
|
|
|
|
private static function homeDir(): ?string
|
|
{
|
|
foreach (['HOME', 'USERPROFILE'] as $key) {
|
|
$value = getenv($key);
|
|
|
|
if (is_string($value) && $value !== '' && is_dir($value)) {
|
|
return rtrim($value, '/\\');
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static function projectKey(string $projectRoot): string
|
|
{
|
|
$origin = self::originIdentity($projectRoot);
|
|
|
|
$realpath = @realpath($projectRoot);
|
|
$input = $origin ?? ($realpath === false ? $projectRoot : $realpath);
|
|
|
|
$prefix = $origin === null ? '' : self::gitPrefix($projectRoot);
|
|
|
|
if ($prefix !== '') {
|
|
$input .= '/'.$prefix;
|
|
}
|
|
|
|
$hash = substr(hash('sha256', $input), 0, 16);
|
|
$slug = self::slug(basename($projectRoot));
|
|
|
|
return $slug === '' ? $hash : $slug.'-'.$hash;
|
|
}
|
|
|
|
private static function originIdentity(string $projectRoot): ?string
|
|
{
|
|
$url = self::rawOriginUrl($projectRoot);
|
|
|
|
if ($url === null) {
|
|
return null;
|
|
}
|
|
|
|
if (preg_match('#^[\w.-]+@([\w.-]+):([\w./-]+?)(?:\.git)?/?$#', $url, $m) === 1) {
|
|
return strtolower($m[1].'/'.$m[2]);
|
|
}
|
|
|
|
if (preg_match('#^[a-z]+://(?:[^@/]+@)?([^/:]+)(?::\d+)?/([\w./-]+?)(?:\.git)?/?$#i', $url, $m) === 1) {
|
|
return strtolower($m[1].'/'.$m[2]);
|
|
}
|
|
|
|
return strtolower($url);
|
|
}
|
|
|
|
private static function rawOriginUrl(string $projectRoot): ?string
|
|
{
|
|
return new Git($projectRoot)->originUrl();
|
|
}
|
|
|
|
private static function gitPrefix(string $projectRoot): string
|
|
{
|
|
return new Git($projectRoot)->pathPrefix();
|
|
}
|
|
|
|
private static function slug(string $name): string
|
|
{
|
|
$slug = strtolower($name);
|
|
$slug = preg_replace('/[^a-z0-9]+/', '-', $slug) ?? '';
|
|
|
|
return trim($slug, '-');
|
|
}
|
|
}
|