Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions src/Exceptions/TiaRequiresRepositoryRoot.php

This file was deleted.

15 changes: 1 addition & 14 deletions src/Plugins/Tia.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Pest\Exceptions\TiaRequiresCommit;
use Pest\Exceptions\TiaRequiresDefaultBranch;
use Pest\Exceptions\TiaRequiresRemote;
use Pest\Exceptions\TiaRequiresRepositoryRoot;
use Pest\Panic;
use Pest\Plugins\Concerns\HandleArguments;
use Pest\Plugins\Tia\BaselineSync;
Expand All @@ -33,7 +32,6 @@
use Pest\Plugins\Tia\TableExtractor;
use Pest\Plugins\Tia\WatchPatterns;
use Pest\Support\Container;
use Pest\Support\Git;
use Pest\Support\View;
use Pest\TestCaseFilters\TiaTestCaseFilter;
use Pest\TestSuite;
Expand Down Expand Up @@ -816,12 +814,6 @@ private function handleParent(array $arguments, string $projectRoot, bool $force
{
$this->watchPatterns->useDefaults($projectRoot);

$subdirectoryPrefix = $this->gitSubdirectoryPrefix($projectRoot);

if ($subdirectoryPrefix !== null) {
Panic::with(new TiaRequiresRepositoryRoot($subdirectoryPrefix));
}

try {
$this->resolveBranch($projectRoot);
} catch (MissingDependency $missingGit) {
Expand Down Expand Up @@ -2192,19 +2184,14 @@ private function formatStructuralDrift(array $drift): string
return implode(', ', array_keys($seen));
}

private function gitSubdirectoryPrefix(string $projectRoot): ?string
{
return new Git($projectRoot)->subdirectoryPrefix();
}

private function composerLockDelta(string $projectRoot, string $sha): string
{
$current = @file_get_contents($projectRoot.'/composer.lock');
if ($current === false) {
return '';
}

$baseline = new Git($projectRoot)->show($sha, 'composer.lock');
$baseline = new ChangedFiles($projectRoot)->contentAtSha($sha, 'composer.lock');

if ($baseline === null) {
return '';
Expand Down
5 changes: 3 additions & 2 deletions src/Plugins/Tia/Baselines/BaseRemote.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Pest\Plugins\Tia\Baselines;

use Pest\Plugins\Tia\GitRepository;
use Pest\Plugins\Tia\WatchPatterns;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -75,9 +76,9 @@ public function classifyError(string $output): array

protected function readOriginUrl(string $projectRoot): ?string // @pest-arch-ignore-line
{
$gitConfig = $projectRoot.DIRECTORY_SEPARATOR.'.git'.DIRECTORY_SEPARATOR.'config';
$gitConfig = GitRepository::configPath($projectRoot);

if (! is_file($gitConfig)) {
if ($gitConfig === null) {
return null;
}

Expand Down
58 changes: 53 additions & 5 deletions src/Plugins/Tia/ChangedFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
{
private Git $git;

private string $repoPrefix;

public function __construct(private string $projectRoot)
{
$this->git = new Git($projectRoot);
$this->repoPrefix = $this->detectRepoPrefix();
}

public function repoPrefix(): string
{
return $this->repoPrefix;
}

/**
Expand Down Expand Up @@ -158,9 +166,9 @@ private function filterBehaviourallyUnchanged(array $files, string $sha): array
return $remaining;
}

private function contentAtSha(string $sha, string $path): ?string
public function contentAtSha(string $sha, string $path): ?string
{
return $this->git->show($sha, $path);
return $this->git->show($sha, $this->repoPrefix.$path);
}

/**
Expand Down Expand Up @@ -305,13 +313,15 @@ private function shaIsReachable(string $sha): bool
*/
private function diffSinceSha(string $sha): array
{
$output = $this->scan()->raw(['diff', '--name-only', '--no-renames', $sha.'..HEAD']);
$output = $this->scan()->raw(['diff', '--name-only', '-z', '--no-renames', $sha.'..HEAD']);

if ($output === null) {
throw new MissingDependency('Tia mode', 'git');
}

return $this->splitLines($output);
$paths = explode("\x00", rtrim($output, "\x00"));

return $this->toProjectRelative(array_values(array_filter($paths, static fn (string $path): bool => $path !== '')));
}

/**
Expand Down Expand Up @@ -357,7 +367,7 @@ private function workingTreeChanges(): array
$files[] = $path;
}

return $files;
return $this->toProjectRelative($files);
}

public function currentSha(): ?string
Expand All @@ -373,6 +383,44 @@ public function currentSha(): ?string
return $sha === '' ? null : $sha;
}

/**
* @param array<int, string> $repoRelativePaths
* @return array<int, string>
*/
private function toProjectRelative(array $repoRelativePaths): array
{
if ($this->repoPrefix === '') {
return $repoRelativePaths;
}

$projectRelative = [];

foreach ($repoRelativePaths as $path) {
if (str_starts_with($path, $this->repoPrefix)) {
$projectRelative[] = substr($path, strlen($this->repoPrefix));
}
}

return $projectRelative;
}

private function detectRepoPrefix(): string
{
static $cache = [];

if (isset($cache[$this->projectRoot])) {
return $cache[$this->projectRoot];
}

$prefix = $this->git->subdirectoryPrefix();

if ($prefix === null || $prefix === '') {
return $cache[$this->projectRoot] = '';
}

return $cache[$this->projectRoot] = $prefix.'/';
}

/**
* @return array<int, string>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Tia/Fingerprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private static function isTrackedByGit(string $projectRoot, string $relativePath
return $cache[$key];
}

if (! is_dir($projectRoot.'/.git') && ! is_file($projectRoot.'/.git')) {
if (GitRepository::locate($projectRoot) === null) {
return $cache[$key] = true;
}

Expand Down
106 changes: 106 additions & 0 deletions src/Plugins/Tia/GitRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace Pest\Plugins\Tia;

final class GitRepository
{
public static function locate(string $path): ?string
{
$dir = $path;

while (true) {
$dotGit = $dir.DIRECTORY_SEPARATOR.'.git';

if (is_dir($dotGit) || is_file($dotGit)) {
return $dotGit;
}

$parent = dirname($dir);

if ($parent === $dir) {
return null;
}

$dir = $parent;
}
}

public static function configPath(string $path): ?string
{
$dotGit = self::locate($path);

if ($dotGit === null) {
return null;
}

$gitDir = is_dir($dotGit) ? $dotGit : self::resolveGitDirFile($dotGit);

if ($gitDir === null) {
return null;
}

$config = $gitDir.DIRECTORY_SEPARATOR.'config';

return is_file($config) ? $config : null;
}

public static function subdirectoryPrefix(string $projectRoot): string
{
$dotGit = self::locate($projectRoot);

if ($dotGit === null) {
return '';
}

$repoRoot = dirname($dotGit);
$realRepoRoot = @realpath($repoRoot);
$realProjectRoot = @realpath($projectRoot);

if ($realRepoRoot === false || $realProjectRoot === false || $realRepoRoot === $realProjectRoot) {
return '';
}

$prefix = substr($realProjectRoot, strlen(rtrim($realRepoRoot, DIRECTORY_SEPARATOR)) + 1);

return str_replace(DIRECTORY_SEPARATOR, '/', rtrim($prefix, DIRECTORY_SEPARATOR)).'/';
}

private static function resolveGitDirFile(string $dotGitFile): ?string
{
$content = @file_get_contents($dotGitFile);

if ($content === false || preg_match('/^gitdir:\s*(.+)$/m', $content, $match) !== 1) {
return null;
}

$gitDir = trim($match[1]);

if (! self::isAbsolutePath($gitDir)) {
$gitDir = dirname($dotGitFile).DIRECTORY_SEPARATOR.$gitDir;
}

$commonDir = $gitDir.DIRECTORY_SEPARATOR.'commondir';

if (is_file($commonDir)) {
$common = trim((string) @file_get_contents($commonDir));

if ($common !== '') {
$gitDir = self::isAbsolutePath($common)
? $common
: $gitDir.DIRECTORY_SEPARATOR.$common;
}
}

$resolved = @realpath($gitDir);

return $resolved === false ? null : $resolved;
}

private static function isAbsolutePath(string $path): bool
{
return str_starts_with($path, DIRECTORY_SEPARATOR)
|| preg_match('#^[A-Za-z]:[\\\\/]#', $path) === 1;
}
}
12 changes: 10 additions & 2 deletions src/Plugins/Tia/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ private static function projectKey(string $projectRoot): string
{
$origin = self::originIdentity($projectRoot);

if ($origin !== null) {
$prefix = GitRepository::subdirectoryPrefix($projectRoot);

if ($prefix !== '') {
$origin .= '#'.$prefix;
}
}

$realpath = @realpath($projectRoot);
$input = $origin ?? ($realpath === false ? $projectRoot : $realpath);

Expand Down Expand Up @@ -131,9 +139,9 @@ private static function originIdentity(string $projectRoot): ?string

private static function rawOriginUrl(string $projectRoot): ?string
{
$config = $projectRoot.DIRECTORY_SEPARATOR.'.git'.DIRECTORY_SEPARATOR.'config';
$config = GitRepository::configPath($projectRoot);

if (! is_file($config)) {
if ($config === null) {
return null;
}

Expand Down
Loading