Skip to content

Commit adfc190

Browse files
author
alexandresalome
committed
git log loads data in commit
1 parent 39a05fa commit adfc190

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

src/Gitonomy/Git/Commit.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ public function __construct(Repository $repository, $hash, array $data = array()
4747

4848
parent::__construct($repository, $hash);
4949

50-
$this->data = $data;
50+
$this->setData($data);
51+
}
52+
53+
public function setData(array $data)
54+
{
55+
foreach ($data as $name => $value) {
56+
$this->data[$name] = $value;
57+
}
5158
}
5259

5360
/**

src/Gitonomy/Git/Log.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function getSingleCommit()
155155
*/
156156
public function getCommits()
157157
{
158-
$args = array('--encoding='.StringHelper::getEncoding(), '--format=format:%H');
158+
$args = array('--encoding='.StringHelper::getEncoding(), '--format=raw');
159159

160160
if (null !== $this->offset) {
161161
$args[] = '--skip='.((int) $this->offset);
@@ -177,17 +177,23 @@ public function getCommits()
177177
$args = array_merge($args, $this->paths);
178178

179179
try {
180-
$exp = explode("\n", $this->repository->run('log', $args));
180+
$output = $this->repository->run('log', $args);
181181
} catch (ProcessException $e) {
182182
throw new ReferenceNotFoundException(sprintf('Can not find revision "%s"', implode(' ', $this->revisions->getAsTextArray())), null, $e);
183183
}
184184

185+
$parser = new Parser\LogParser();
186+
$parser->parse($output);
187+
185188
$result = array();
186-
foreach ($exp as $hash) {
187-
if ($hash == '') {
188-
continue;
189-
}
190-
$result[] = $this->repository->getCommit($hash);
189+
foreach ($parser->log as $commitData) {
190+
$hash = $commitData['id'];
191+
unset($commitData['id']);
192+
193+
$commit = $this->repository->getCommit($hash);
194+
$commit->setData($commitData);
195+
196+
$result[] = $commit;
191197
}
192198

193199
return $result;

src/Gitonomy/Git/Parser/LogParser.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/**
4+
* This file is part of Gitonomy.
5+
*
6+
* (c) Alexandre Salomé <alexandre.salome@gmail.com>
7+
* (c) Julien DIDIER <genzo.wm@gmail.com>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace Gitonomy\Git\Parser;
14+
15+
use Gitonomy\Git\Exception\RuntimeException;
16+
17+
class LogParser extends CommitParser
18+
{
19+
public $log = array();
20+
21+
protected function doParse()
22+
{
23+
$this->log = array();
24+
25+
while (!$this->isFinished()) {
26+
$commit = array();
27+
$this->consume('commit ');
28+
$commit['id'] = $this->consumeHash();
29+
$this->consumeNewLine();
30+
31+
$this->consume('tree ');
32+
$commit['tree'] = $this->consumeHash();
33+
$this->consumeNewLine();
34+
35+
$commit['parentHashes'] = array();
36+
while ($this->expects('parent ')) {
37+
$commit['parentHashes'][] = $this->consumeHash();
38+
$this->consumeNewLine();
39+
}
40+
41+
$this->consume('author ');
42+
list($commit['authorName'], $commit['authorEmail'], $commit['authorDate']) = $this->consumeNameEmailDate();
43+
$commit['authorDate'] = $this->parseDate($commit['authorDate']);
44+
$this->consumeNewLine();
45+
46+
$this->consume('committer ');
47+
list($commit['committerName'], $commit['committerEmail'], $commit['committerDate']) = $this->consumeNameEmailDate();
48+
$commit['committerDate'] = $this->parseDate($commit['committerDate']);
49+
$this->consumeNewLine();
50+
$this->consumeNewLine();
51+
52+
$message = '';
53+
while ($this->expects(' ')) {
54+
$message .= $this->consumeTo("\n")."\n";
55+
$this->consumeNewLine();
56+
}
57+
58+
if (!$this->isFinished()) {
59+
$this->consumeNewLine();
60+
}
61+
62+
$commit['message'] = $message;
63+
64+
$this->log[] = $commit;
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)