forked from gitonomy/gitlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.php
146 lines (120 loc) · 3.66 KB
/
Tree.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* This file is part of Gitonomy.
*
* (c) Alexandre Salomé <[email protected]>
* (c) Julien DIDIER <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Gitonomy\Git;
use Gitonomy\Git\Exception\InvalidArgumentException;
use Gitonomy\Git\Exception\UnexpectedValueException;
/**
* @author Alexandre Salomé <[email protected]>
*/
class Tree
{
protected $repository;
protected $hash;
protected $isInitialized = false;
protected $entries;
protected $entriesByType;
public function __construct(Repository $repository, $hash)
{
$this->repository = $repository;
$this->hash = $hash;
}
public function getHash()
{
return $this->hash;
}
protected function initialize()
{
if (true === $this->isInitialized) {
return;
}
$output = $this->repository->run('cat-file', ['-p', $this->hash]);
$parser = new Parser\TreeParser();
$parser->parse($output);
$this->entries = [];
$this->entriesByType = [
'blob' => [],
'tree' => [],
'commit' => [],
];
foreach ($parser->entries as $entry) {
list($mode, $type, $hash, $name) = $entry;
if ($type == 'blob') {
$treeEntry = [$mode, $this->repository->getBlob($hash)];
} elseif ($type == 'tree') {
$treeEntry = [$mode, $this->repository->getTree($hash)];
} else {
$treeEntry = [$mode, new CommitReference($hash)];
}
$this->entries[$name] = $treeEntry;
$this->entriesByType[$type][$name] = $treeEntry;
}
$this->isInitialized = true;
}
/**
* @return array<string, array{string, CommitReference|Tree|Blob}> An associative array name => $object
*/
public function getEntries(): array
{
$this->initialize();
return $this->entries;
}
/**
* @return array<string, array{string, CommitReference}> An associative array of name => [mode, commit reference]
*/
public function getCommitReferenceEntries(): array
{
$this->initialize();
return $this->entriesByType['commit'];
}
/**
* @return array<string, array{string, Tree}> An associative array of name => [mode, tree]
*/
public function getTreeEntries(): array
{
$this->initialize();
return $this->entriesByType['tree'];
}
/**
* @return array<string, array{string, Blob}> An associative array of name => [mode, blob]
*/
public function getBlobEntries(): array
{
$this->initialize();
return $this->entriesByType['blob'];
}
public function getEntry($name)
{
$this->initialize();
if (!isset($this->entries[$name])) {
throw new InvalidArgumentException('No entry '.$name);
}
return $this->entries[$name][1];
}
public function resolvePath($path)
{
if ($path == '') {
return $this;
}
$path = preg_replace('#^/#', '', $path);
$segments = explode('/', $path);
$element = $this;
foreach ($segments as $segment) {
if ($element instanceof self) {
$element = $element->getEntry($segment);
} elseif ($element instanceof Blob) {
throw new InvalidArgumentException('Unresolvable path');
} else {
throw new UnexpectedValueException('Unknow type of element');
}
}
return $element;
}
}