biurad /
php-git-scm
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of Biurad opensource projects. |
||
| 5 | * |
||
| 6 | * @copyright 2022 Biurad Group (https://biurad.com/) |
||
| 7 | * @license https://opensource.org/licenses/BSD-3-Clause License |
||
| 8 | * |
||
| 9 | * For the full copyright and license information, please view the LICENSE |
||
| 10 | * file that was distributed with this source code. |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Biurad\Git; |
||
| 14 | |||
| 15 | use Symfony\Component\Process\Exception\ExceptionInterface; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * An object representing a git refname. |
||
| 19 | * |
||
| 20 | * @author Divine Niiquaye Ibok <[email protected]> |
||
| 21 | */ |
||
| 22 | class Revision extends GitObject |
||
| 23 | { |
||
| 24 | protected string $name, $revision; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $revision The revision name eg. (refs/heads/master) |
||
| 28 | */ |
||
| 29 | public function __construct(Repository $repository, string $revision, string $commitHash = null) |
||
| 30 | { |
||
| 31 | parent::__construct($repository, $commitHash ?? ''); |
||
| 32 | $this->revision = $revision; |
||
| 33 | $this->name = $this->doName(); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function __toString(): string |
||
| 37 | { |
||
| 38 | if (null === $this->hash) { |
||
| 39 | try { |
||
| 40 | $this->hash = $this->repository->run('rev-parse', ['--verify', $this->revision]); |
||
| 41 | } catch (ExceptionInterface) { |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | return parent::__toString(); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The name of the revision (e.g. "refs/heads/master"). |
||
| 50 | */ |
||
| 51 | public function getRevision(): string |
||
| 52 | { |
||
| 53 | if (!\str_starts_with($this->revision, 'refs/')) { |
||
| 54 | throw new \RuntimeException(\sprintf('Invalid revision provided "%s", expected revision to start with a "refs/"', $this->revision)); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $this->revision; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The filtered name of the revision (e.g. "master"). |
||
| 62 | */ |
||
| 63 | public function getName(): string |
||
| 64 | { |
||
| 65 | return $this->name ?: throw new \RuntimeException('Not implemented yet.'); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function getCommit(): ?Commit |
||
| 69 | { |
||
| 70 | return $this->repository->getCommit($this->__toString()); |
||
| 71 | } |
||
| 72 | |||
| 73 | protected function doName(): string |
||
| 74 | { |
||
| 75 | return ''; // Should be extendedly implemented |
||
| 76 | } |
||
| 77 | } |
||
| 78 |