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
|
|
|
/** |
16
|
|
|
* A class for fetching git commit logs. |
17
|
|
|
* |
18
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Log implements \Countable, \IteratorAggregate |
21
|
|
|
{ |
22
|
|
|
private array $revisions = [], $paths = [], $commits = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Repository $repository the repository where log occurs |
26
|
|
|
* @param null|array<int,string>|string $revisions a list of revisions or null if you want all history |
27
|
|
|
* @param null|array<int,string>|string $paths paths to filter on |
28
|
|
|
* @param null|int $offset start list from a given position |
29
|
|
|
* @param null|int $limit limit number of fetched elements |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
private Repository $repository, |
33
|
|
|
array|string|null $revisions = null, |
34
|
|
|
array|string $paths = null, |
35
|
|
|
private ?int $offset = null, |
36
|
|
|
private ?int $limit = null |
37
|
|
|
) { |
38
|
|
|
$this->paths = \is_string($paths) ? [$paths] : $paths ?? []; |
|
|
|
|
39
|
|
|
$this->revisions = \is_string($revisions) ? [$revisions] : $revisions ?? []; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Position to start listing commits from. |
44
|
|
|
*/ |
45
|
|
|
public function setOffset(int $offset): self |
46
|
|
|
{ |
47
|
|
|
$this->offset = $offset; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Limit number of fetched commits. |
54
|
|
|
*/ |
55
|
|
|
public function setLimit(int $limit): self |
56
|
|
|
{ |
57
|
|
|
$this->limit = $limit; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getRevisions(): array |
63
|
|
|
{ |
64
|
|
|
return $this->revisions; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getPaths(): array |
68
|
|
|
{ |
69
|
|
|
return $this->paths; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array<int,Commit> |
74
|
|
|
*/ |
75
|
|
|
public function getCommits(): array |
76
|
|
|
{ |
77
|
|
|
$args = ['--encoding='.'utf8', '--pretty='.'format:%H']; |
78
|
|
|
|
79
|
|
|
if (null !== $this->offset) { |
80
|
|
|
$args[] = '--skip='.$this->offset; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (null !== $this->limit) { |
84
|
|
|
$args[] = '-n'; |
85
|
|
|
$args[] = $this->limit; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$args = \array_merge($args, \count($this->revisions) > 0 ? $this->revisions : ['--all', '--'], $this->paths); |
89
|
|
|
$o = $this->repository->run('log', $args); |
90
|
|
|
|
91
|
|
|
if (empty($o) || 0 !== $this->repository->getExitCode()) { |
92
|
|
|
return []; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!isset($this->commits[$i = \md5($o)])) { |
96
|
|
|
foreach (\explode("\n", $o) as $commit) { |
97
|
|
|
$this->commits[$i][] = $this->repository->getCommit($commit); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->commits[$i]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function count(): int |
105
|
|
|
{ |
106
|
|
|
if (\count($this->revisions) > 0) { |
107
|
|
|
$count = (int) $this->repository->run('rev-list', ['--count', ...$this->revisions, '--', ...$this->paths]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $count ?? (int) $this->repository->run('rev-list', ['--count', '--all', '--', ...$this->paths]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return \Traversable<Commit> |
115
|
|
|
*/ |
116
|
|
|
public function getIterator(): \Traversable |
117
|
|
|
{ |
118
|
|
|
return new \ArrayIterator($this->getCommits()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|