1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GitElephant - An abstraction layer for git written in PHP |
5
|
|
|
* Copyright (C) 2013 Matteo Giachino |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program. If not, see [http://www.gnu.org/licenses/]. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace GitElephant\Objects; |
22
|
|
|
|
23
|
|
|
use GitElephant\Command\RevParseCommand; |
24
|
|
|
use GitElephant\Repository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A Object instance represents a node in the git tree repository |
28
|
|
|
* It could be a file or a folder, as well as a submodule (a "link" in the git language) |
29
|
|
|
* |
30
|
|
|
* @author Matteo Giachino <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class NodeObject implements TreeishInterface |
33
|
|
|
{ |
34
|
|
|
public const TYPE_BLOB = 'blob'; |
35
|
|
|
public const TYPE_TREE = 'tree'; |
36
|
|
|
public const TYPE_LINK = 'commit'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \GitElephant\Repository |
40
|
|
|
*/ |
41
|
|
|
protected $repository; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* permissions |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $permissions; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* type |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $type; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* sha |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $sha; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* size |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $size; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* name |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
private $name; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* path |
80
|
|
|
* |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
private $path; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* create a Object from a single outputLine of the git ls-tree command |
87
|
|
|
* |
88
|
|
|
* @param \GitElephant\Repository $repository repository instance |
89
|
|
|
* @param string $outputLine output from ls-tree command |
90
|
|
|
* |
91
|
|
|
* @see LsTreeCommand::tree |
92
|
|
|
* @return NodeObject |
93
|
|
|
*/ |
94
|
8 |
|
public static function createFromOutputLine(Repository $repository, string $outputLine) |
95
|
|
|
{ |
96
|
8 |
|
$slices = static::getLineSlices($outputLine); |
97
|
8 |
|
$fullPath = $slices['fullPath']; |
98
|
8 |
|
if (false === $pos = mb_strrpos($fullPath, '/')) { |
99
|
|
|
// repository root |
100
|
6 |
|
$path = ''; |
101
|
6 |
|
$name = $fullPath; |
102
|
|
|
} else { |
103
|
4 |
|
$path = substr($fullPath, 0, $pos); |
104
|
4 |
|
$name = substr($fullPath, $pos + 1); |
105
|
|
|
} |
106
|
|
|
|
107
|
8 |
|
return new static( |
108
|
8 |
|
$repository, |
109
|
8 |
|
$slices['permissions'], |
110
|
8 |
|
$slices['type'], |
111
|
8 |
|
$slices['sha'], |
112
|
8 |
|
$slices['size'], |
113
|
8 |
|
$name, |
114
|
8 |
|
$path |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Take a line and turn it in slices |
120
|
|
|
* |
121
|
|
|
* @param string $line a single line output from the git binary |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
14 |
|
public static function getLineSlices(string $line): array |
126
|
|
|
{ |
127
|
14 |
|
preg_match('/^(\d+) (\w+) ([a-z0-9]+) +(\d+|-)\t(.*)$/', $line, $matches); |
128
|
14 |
|
$permissions = $matches[1]; |
129
|
14 |
|
$type = null; |
130
|
14 |
|
switch ($matches[2]) { |
131
|
|
|
case NodeObject::TYPE_TREE: |
132
|
5 |
|
$type = NodeObject::TYPE_TREE; |
133
|
5 |
|
break; |
134
|
|
|
case NodeObject::TYPE_BLOB: |
135
|
14 |
|
$type = NodeObject::TYPE_BLOB; |
136
|
14 |
|
break; |
137
|
|
|
case NodeObject::TYPE_LINK: |
138
|
1 |
|
$type = NodeObject::TYPE_LINK; |
139
|
1 |
|
break; |
140
|
|
|
} |
141
|
14 |
|
$sha = $matches[3]; |
142
|
14 |
|
$size = $matches[4]; |
143
|
14 |
|
$fullPath = $matches[5]; |
144
|
|
|
|
145
|
|
|
return [ |
146
|
14 |
|
'permissions' => $permissions, |
147
|
14 |
|
'type' => $type, |
148
|
14 |
|
'sha' => $sha, |
149
|
14 |
|
'size' => $size, |
150
|
14 |
|
'fullPath' => $fullPath, |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Class constructor |
156
|
|
|
* |
157
|
|
|
* @param \GitElephant\Repository $repository repository instance |
158
|
|
|
* @param string $permissions node permissions |
159
|
|
|
* @param string $type node type |
160
|
|
|
* @param string $sha node sha |
161
|
|
|
* @param string $size node size in bytes |
162
|
|
|
* @param string $name node name |
163
|
|
|
* @param string $path node path |
164
|
|
|
*/ |
165
|
14 |
|
public function __construct( |
166
|
|
|
Repository $repository, |
167
|
|
|
string $permissions, |
168
|
|
|
string $type, |
169
|
|
|
string $sha, |
170
|
|
|
string $size, |
171
|
|
|
string $name, |
172
|
|
|
string $path |
173
|
|
|
) { |
174
|
14 |
|
$this->repository = $repository; |
175
|
14 |
|
$this->permissions = $permissions; |
176
|
14 |
|
$this->type = $type; |
177
|
14 |
|
$this->sha = $sha; |
178
|
14 |
|
$this->size = $size; |
179
|
14 |
|
$this->name = $name; |
180
|
14 |
|
$this->path = $path; |
181
|
14 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* toString magic method |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
1 |
|
public function __toString(): string |
189
|
|
|
{ |
190
|
1 |
|
return (string) $this->name; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Mime Type getter |
195
|
|
|
* |
196
|
|
|
* @param string $basePath the base path of the repository |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getMimeType(string $basePath) |
201
|
|
|
{ |
202
|
|
|
return mime_content_type($basePath . DIRECTORY_SEPARATOR . $this->path); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* get extension if it's a blob |
207
|
|
|
* |
208
|
|
|
* @return string|null |
209
|
|
|
*/ |
210
|
|
|
public function getExtension(): ?string |
211
|
|
|
{ |
212
|
|
|
$pos = strrpos($this->name, '.'); |
213
|
|
|
if ($pos === false) { |
214
|
|
|
return null; |
215
|
|
|
} else { |
216
|
|
|
return substr($this->name, $pos + 1); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* whether the node is a tree |
222
|
|
|
* |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
9 |
|
public function isTree(): bool |
226
|
|
|
{ |
227
|
9 |
|
return self::TYPE_TREE == $this->getType(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* whether the node is a link |
232
|
|
|
* |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
|
|
public function isLink(): bool |
236
|
|
|
{ |
237
|
|
|
return self::TYPE_LINK == $this->getType(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* whether the node is a blob |
242
|
|
|
* |
243
|
|
|
* @return bool |
244
|
|
|
*/ |
245
|
1 |
|
public function isBlob(): bool |
246
|
|
|
{ |
247
|
1 |
|
return self::TYPE_BLOB == $this->getType(); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Full path getter |
252
|
|
|
* |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
12 |
|
public function getFullPath(): string |
256
|
|
|
{ |
257
|
12 |
|
return rtrim( |
258
|
12 |
|
'' == $this->path ? $this->name : $this->path . DIRECTORY_SEPARATOR . $this->name, |
259
|
12 |
|
DIRECTORY_SEPARATOR |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* permissions getter |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function getPermissions(): string |
269
|
|
|
{ |
270
|
|
|
return $this->permissions; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* sha getter |
275
|
|
|
* |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
6 |
|
public function getSha(): string |
279
|
|
|
{ |
280
|
6 |
|
return $this->sha; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* type getter |
285
|
|
|
* |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
13 |
|
public function getType(): string |
289
|
|
|
{ |
290
|
13 |
|
return $this->type; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* name getter |
295
|
|
|
* |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
5 |
|
public function getName(): string |
299
|
|
|
{ |
300
|
5 |
|
return $this->name; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* path getter |
305
|
|
|
* |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
public function getPath(): string |
309
|
|
|
{ |
310
|
|
|
return $this->path; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* size getter |
315
|
|
|
* |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
|
public function getSize(): string |
319
|
|
|
{ |
320
|
|
|
return $this->size; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* gets the last commit in this object |
325
|
|
|
* |
326
|
|
|
* @return Commit |
327
|
|
|
*/ |
328
|
3 |
|
public function getLastCommit(): ?\GitElephant\Objects\Commit |
329
|
|
|
{ |
330
|
3 |
|
$log = $this->repository->getLog('HEAD', $this->getFullPath(), 1); |
331
|
|
|
|
332
|
3 |
|
return $log[0]; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* rev-parse command - often used to return a commit tag. |
337
|
|
|
* |
338
|
|
|
* @param array $options the options to apply to rev-parse |
339
|
|
|
* |
340
|
|
|
* @throws \RuntimeException |
341
|
|
|
* @throws \InvalidArgumentException |
342
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
343
|
|
|
* @return array |
344
|
|
|
*/ |
345
|
1 |
|
public function revParse(array $options = []): array |
346
|
|
|
{ |
347
|
1 |
|
$c = RevParseCommand::getInstance()->revParse($this, $options); |
348
|
1 |
|
$caller = $this->repository->getCaller(); |
349
|
1 |
|
$caller->execute($c); |
350
|
|
|
|
351
|
1 |
|
return array_map('trim', $caller->getOutputLines(true)); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/* |
355
|
|
|
* Repository setter |
356
|
|
|
* |
357
|
|
|
* @param \GitElephant\Repository $repository the repository variable |
358
|
|
|
*/ |
359
|
1 |
|
public function setRepository(Repository $repository): void |
360
|
|
|
{ |
361
|
1 |
|
$this->repository = $repository; |
362
|
1 |
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Repository getter |
366
|
|
|
* |
367
|
|
|
* @return \GitElephant\Repository |
368
|
|
|
*/ |
369
|
59 |
|
public function getRepository(): \GitElephant\Repository |
370
|
|
|
{ |
371
|
59 |
|
return $this->repository; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|