|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Gitonomy. |
| 5 | + * |
| 6 | + * (c) Alexandre Salomé <alexandre.salome@gmail.com> |
| 7 | + * (c) Julien DIDIER <genzo.wm@gmail.com> |
| 8 | + * |
| 9 | + * This source file is subject to the MIT license that is bundled |
| 10 | + * with this source code in the file LICENSE. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Gitonomy\Git\Driver; |
| 14 | + |
| 15 | +use Gitonomy\Git\Exception\DriverException; |
| 16 | +use Gitonomy\Git\Exception\InvalidArgumentException; |
| 17 | +use Gitonomy\Git\Exception\UnsupportedOperationException; |
| 18 | + |
| 19 | +/** |
| 20 | + * The git directory and the working tree of a repository, found the same way by every driver. |
| 21 | + * |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +final readonly class RepositoryPaths |
| 25 | +{ |
| 26 | + private function __construct( |
| 27 | + public string $gitDir, |
| 28 | + public ?string $workingDir, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param string $dir A bare repository, a working tree, or a git directory when $workingDir is given |
| 34 | + * @param string|null $workingDir The working tree of the git directory $dir |
| 35 | + * |
| 36 | + * @throws InvalidArgumentException $dir doesn't exist, or isn't a repository |
| 37 | + * @throws DriverException The ".git" file of $dir doesn't point to a git directory |
| 38 | + * @throws UnsupportedOperationException The repository uses SHA-256 hashes |
| 39 | + */ |
| 40 | + public static function resolve(string $dir, ?string $workingDir): self |
| 41 | + { |
| 42 | + $realDir = realpath($dir); |
| 43 | + if (false === $realDir || !is_dir($realDir)) { |
| 44 | + throw new InvalidArgumentException(\sprintf('Directory "%s" does not exist or is not a directory.', $dir)); |
| 45 | + } |
| 46 | + |
| 47 | + $paths = match (true) { |
| 48 | + null !== $workingDir => new self($realDir, $workingDir), |
| 49 | + is_dir($realDir.'/.git') => new self($realDir.'/.git', $realDir), |
| 50 | + // In a linked worktree or a submodule, ".git" is a file pointing to the git directory. |
| 51 | + is_file($realDir.'/.git') => new self(self::readGitFile($realDir), $realDir), |
| 52 | + default => new self($realDir, null), |
| 53 | + }; |
| 54 | + |
| 55 | + // Like git, which requires it in any git directory. |
| 56 | + if (!is_file($paths->gitDir.'/HEAD')) { |
| 57 | + throw new InvalidArgumentException(\sprintf('Directory "%s" is not a git repository.', $dir)); |
| 58 | + } |
| 59 | + |
| 60 | + $config = is_file($paths->gitDir.'/config') ? file_get_contents($paths->gitDir.'/config') : false; |
| 61 | + if (false !== $config && preg_match('/^\s*objectformat\s*=\s*sha256\s*$/mi', $config)) { |
| 62 | + throw new UnsupportedOperationException(\sprintf('The repository "%s" uses SHA-256 hashes, which gitlib doesn\'t support yet.', $dir)); |
| 63 | + } |
| 64 | + |
| 65 | + return $paths; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return string The git directory the ".git" file of $dir points to |
| 70 | + */ |
| 71 | + private static function readGitFile(string $dir): string |
| 72 | + { |
| 73 | + $content = file_get_contents($dir.'/.git'); |
| 74 | + if (false === $content || !preg_match('/^gitdir: (.+)$/m', $content, $match)) { |
| 75 | + throw new DriverException(\sprintf('The "%s/.git" file doesn\'t point to a git directory.', $dir)); |
| 76 | + } |
| 77 | + |
| 78 | + $gitDir = str_starts_with($match[1], '/') || preg_match('/^[a-zA-Z]:[\\\\\/]/', $match[1]) ? $match[1] : $dir.'/'.$match[1]; |
| 79 | + |
| 80 | + return realpath($gitDir) ?: throw new DriverException(\sprintf('The git directory "%s" of "%s" does not exist.', $match[1], $dir)); |
| 81 | + } |
| 82 | +} |
0 commit comments