|
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\Commit; |
|
14
|
|
|
|
|
15
|
|
|
use Biurad\Git\{GitObject, Repository}; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The Tree object represents a Git tree object. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Tree extends GitObject |
|
23
|
|
|
{ |
|
24
|
|
|
protected bool $initialized = false; |
|
25
|
|
|
protected array $entries = []; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(Repository $repository, string $hash = null, protected ?int $mode = null) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($repository, $hash); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getMode(): ?int |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->mode; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return array<string,Blob|CommitRef|Tree> An associative array name => $object |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getEntries(): array |
|
41
|
|
|
{ |
|
42
|
|
|
$this->doInitialize(); |
|
43
|
|
|
|
|
44
|
|
|
return $this->entries; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function has(string $entry): bool |
|
48
|
|
|
{ |
|
49
|
|
|
$this->doInitialize(); |
|
50
|
|
|
|
|
51
|
|
|
return isset($this->entries[$entry]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function get(string $entry): Blob|Tree|CommitRef |
|
55
|
|
|
{ |
|
56
|
|
|
$this->doInitialize(); |
|
57
|
|
|
|
|
58
|
|
|
return $this->entries[$entry] ?? throw new \InvalidArgumentException(\sprintf('Tree entry "%s" does not exist.', $entry)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get a sub-tree of a given path. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getSubTree(string $path): Blob|Tree|CommitRef |
|
65
|
|
|
{ |
|
66
|
|
|
$tree = $this; |
|
67
|
|
|
|
|
68
|
|
|
foreach (\explode('/', \ltrim($path, '/')) as $segment) { |
|
69
|
|
|
if (!empty($segment)) { |
|
70
|
|
|
$tree = $tree->get($segment); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $tree; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function doInitialize(): void |
|
78
|
|
|
{ |
|
79
|
|
|
if ($this->initialized) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$o = $this->repository->run('ls-tree', [$this->__toString(),'--format=%(objectmode) %(objecttype) %(objectname) %(path)']); |
|
84
|
|
|
|
|
85
|
|
|
if (empty($o) || 0 !== $this->repository->getExitCode()) { |
|
86
|
|
|
throw new \RuntimeException(\sprintf('Failed to get tree data for "%s"', $this->__toString())); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
foreach (\explode("\n", $o) as $line) { |
|
90
|
|
|
if (empty($line)) { |
|
91
|
|
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
[$a, $b, $c, $d] = \explode(' ', $line, 4); |
|
94
|
|
|
$object = null; |
|
95
|
|
|
|
|
96
|
|
|
if ('tree' === $b) { |
|
97
|
|
|
$object = new self($this->repository, $c, (int) $a); |
|
98
|
|
|
} elseif ('blob' === $b) { |
|
99
|
|
|
$object = new Blob($this->repository, $c, (int) $a); |
|
100
|
|
|
} |
|
101
|
|
|
$this->entries[$d] = $object ?? new CommitRef($c, (int) $a); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->initialized = true; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|