1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GitElephant - An abstraction layer for git written in PHP |
4
|
|
|
* Copyright (C) 2013 Matteo Giachino |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see [http://www.gnu.org/licenses/]. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace GitElephant\Objects; |
21
|
|
|
|
22
|
|
|
use \GitElephant\Repository; |
23
|
|
|
use \GitElephant\Command\LogCommand; |
24
|
|
|
use \GitElephant\Utilities; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Git log abstraction object |
28
|
|
|
* |
29
|
|
|
* @author Matteo Giachino <[email protected]> |
30
|
|
|
* @author Dhaval Patel <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class Log implements \ArrayAccess, \Countable, \Iterator |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var \GitElephant\Repository |
36
|
|
|
*/ |
37
|
|
|
private $repository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* the commits related to this log |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $commits = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* the cursor position |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private $position = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* static method to generate standalone log |
55
|
|
|
* |
56
|
|
|
* @param \GitElephant\Repository $repository repo |
57
|
|
|
* @param array $outputLines output lines from command.log |
58
|
|
|
* |
59
|
|
|
* @return \GitElephant\Objects\Log |
60
|
|
|
*/ |
61
|
1 |
|
public static function createFromOutputLines(Repository $repository, array $outputLines) |
62
|
|
|
{ |
63
|
1 |
|
$log = new self($repository); |
64
|
1 |
|
$log->parseOutputLines($outputLines); |
65
|
|
|
|
66
|
1 |
|
return $log; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Class constructor |
71
|
|
|
* |
72
|
|
|
* @param Repository $repository |
73
|
|
|
* @param string $ref |
74
|
|
|
* @param string|null $path |
75
|
|
|
* @param int $limit |
76
|
|
|
* @param int|null $offset |
77
|
|
|
* @param bool $firstParent |
78
|
|
|
*/ |
79
|
19 |
|
public function __construct( |
80
|
|
|
Repository $repository, |
81
|
|
|
$ref = 'HEAD', |
82
|
|
|
$path = null, |
83
|
|
|
int $limit = 15, |
84
|
|
|
int $offset = null, |
85
|
|
|
bool $firstParent = false |
86
|
|
|
) |
87
|
|
|
{ |
|
|
|
|
88
|
19 |
|
$this->repository = $repository; |
89
|
19 |
|
$this->createFromCommand($ref, $path, $limit, $offset, $firstParent); |
90
|
19 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* get the commit properties from command |
94
|
|
|
* |
95
|
|
|
* @param string $ref treeish reference |
96
|
|
|
* @param string $path path |
97
|
|
|
* @param int $limit limit |
98
|
|
|
* @param string $offset offset |
99
|
|
|
* @param boolean $firstParent first parent |
100
|
|
|
* |
101
|
|
|
* @throws \RuntimeException |
102
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
103
|
|
|
* @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
104
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
105
|
|
|
* @see ShowCommand::commitInfo |
106
|
|
|
*/ |
107
|
19 |
|
private function createFromCommand($ref, $path = null, int $limit, int $offset = null, bool $firstParent = false) |
|
|
|
|
108
|
|
|
{ |
109
|
19 |
|
$command = LogCommand::getInstance($this->getRepository()) |
110
|
19 |
|
->showLog($ref, $path, $limit, $offset, $firstParent); |
111
|
|
|
|
112
|
19 |
|
$outputLines = $this->getRepository() |
113
|
19 |
|
->getCaller() |
114
|
19 |
|
->execute($command) |
115
|
19 |
|
->getOutputLines(true); |
116
|
|
|
|
117
|
19 |
|
$this->parseOutputLines($outputLines); |
118
|
19 |
|
} |
119
|
|
|
|
120
|
19 |
|
private function parseOutputLines(array $outputLines) |
121
|
|
|
{ |
122
|
19 |
|
$this->commits = []; |
123
|
19 |
|
$commits = Utilities::pregSplitFlatArray($outputLines, '/^commit (\w+)$/'); |
124
|
|
|
|
125
|
19 |
|
foreach ($commits as $commitOutputLines) { |
126
|
19 |
|
$this->commits[] = Commit::createFromOutputLines($this->getRepository(), $commitOutputLines); |
127
|
|
|
} |
128
|
19 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get array representation |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
1 |
|
public function toArray() |
136
|
|
|
{ |
137
|
1 |
|
return $this->commits; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the first commit |
142
|
|
|
* |
143
|
|
|
* @return Commit|null |
144
|
|
|
*/ |
145
|
|
|
public function first() |
146
|
|
|
{ |
147
|
|
|
return $this->offsetGet(0); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the last commit |
152
|
|
|
* |
153
|
|
|
* @return Commit|null |
154
|
|
|
*/ |
155
|
3 |
|
public function last() |
156
|
|
|
{ |
157
|
3 |
|
return $this->offsetGet($this->count() - 1); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get commit at index |
162
|
|
|
* |
163
|
|
|
* @param int $index the commit index |
164
|
|
|
* |
165
|
|
|
* @return Commit|null |
166
|
|
|
*/ |
167
|
1 |
|
public function index(int $index) |
168
|
|
|
{ |
169
|
1 |
|
return $this->offsetGet($index); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* ArrayAccess interface |
174
|
|
|
* |
175
|
|
|
* @param int $offset offset |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function offsetExists($offset) |
180
|
|
|
{ |
181
|
|
|
return isset($this->commits[$offset]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* ArrayAccess interface |
186
|
|
|
* |
187
|
|
|
* @param int $offset offset |
188
|
|
|
* |
189
|
|
|
* @return Commit|null |
190
|
|
|
*/ |
191
|
14 |
|
public function offsetGet($offset) |
192
|
|
|
{ |
193
|
14 |
|
return isset($this->commits[$offset]) ? $this->commits[$offset] : null; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* ArrayAccess interface |
198
|
|
|
* |
199
|
|
|
* @param int $offset offset |
200
|
|
|
* @param mixed $value value |
201
|
|
|
* |
202
|
|
|
* @throws \RuntimeException |
203
|
|
|
*/ |
204
|
|
|
public function offsetSet($offset, $value) |
205
|
|
|
{ |
206
|
|
|
throw new \RuntimeException('Can\'t set elements on logs'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* ArrayAccess interface |
211
|
|
|
* |
212
|
|
|
* @param int $offset offset |
213
|
|
|
* |
214
|
|
|
* @throws \RuntimeException |
215
|
|
|
*/ |
216
|
|
|
public function offsetUnset($offset) |
217
|
|
|
{ |
218
|
|
|
throw new \RuntimeException('Can\'t unset elements on logs'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Countable interface |
223
|
|
|
* |
224
|
|
|
* @return int |
225
|
|
|
*/ |
226
|
8 |
|
public function count() |
227
|
|
|
{ |
228
|
8 |
|
return count($this->commits); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Iterator interface |
233
|
|
|
* |
234
|
|
|
* @return Commit|null |
235
|
|
|
*/ |
236
|
|
|
public function current() |
237
|
|
|
{ |
238
|
|
|
return $this->offsetGet($this->position); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Iterator interface |
243
|
|
|
*/ |
244
|
|
|
public function next() |
245
|
|
|
{ |
246
|
|
|
++$this->position; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Iterator interface |
251
|
|
|
* |
252
|
|
|
* @return int |
253
|
|
|
*/ |
254
|
|
|
public function key() |
255
|
|
|
{ |
256
|
|
|
return $this->position; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Iterator interface |
261
|
|
|
* |
262
|
|
|
* @return bool |
263
|
|
|
*/ |
264
|
|
|
public function valid() |
265
|
|
|
{ |
266
|
|
|
return $this->offsetExists($this->position); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Iterator interface |
271
|
|
|
*/ |
272
|
|
|
public function rewind() |
273
|
|
|
{ |
274
|
|
|
$this->position = 0; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Repository setter |
279
|
|
|
* |
280
|
|
|
* @param \GitElephant\Repository $repository the repository variable |
281
|
|
|
*/ |
282
|
|
|
public function setRepository(Repository $repository) |
283
|
|
|
{ |
284
|
|
|
$this->repository = $repository; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Repository getter |
289
|
|
|
* |
290
|
|
|
* @return \GitElephant\Repository |
291
|
|
|
*/ |
292
|
19 |
|
public function getRepository() |
293
|
|
|
{ |
294
|
19 |
|
return $this->repository; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|