forked from gitonomy/gitlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommitParser.php
More file actions
82 lines (64 loc) · 2.22 KB
/
CommitParser.php
File metadata and controls
82 lines (64 loc) · 2.22 KB
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
<?php
/**
* This file is part of Gitonomy.
*
* (c) Alexandre Salomé <alexandre.salome@gmail.com>
* (c) Julien DIDIER <genzo.wm@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Gitonomy\Git\Parser;
use Gitonomy\Git\Exception\RuntimeException;
class CommitParser extends ParserBase
{
public $tree;
public $parents;
public $authorName;
public $authorEmail;
public $authorDate;
public $committerName;
public $committerEmail;
public $committerDate;
public $message;
protected function doParse()
{
$this->consume('tree ');
$this->tree = $this->consumeHash();
$this->consumeNewLine();
$this->parents = [];
while ($this->expects('parent ')) {
$this->parents[] = $this->consumeHash();
$this->consumeNewLine();
}
$this->consume('author ');
list($this->authorName, $this->authorEmail, $this->authorDate) = $this->consumeNameEmailDate();
$this->authorDate = $this->parseDate($this->authorDate);
$this->consumeNewLine();
$this->consume('committer ');
list($this->committerName, $this->committerEmail, $committerDate) = $this->consumeNameEmailDate();
$this->committerDate = $this->parseDate($committerDate);
$this->consumeMergeTag();
// will consume an GPG signed commit if there is one
$this->consumeGPGSignature();
$this->consumeNewLine();
$this->consumeNewLine();
$this->message = $this->consumeAll();
}
protected function consumeNameEmailDate()
{
if (!preg_match('/(([^\n]*) <([^\n]*)> (\d+ [+-]\d{4}))/A', $this->content, $vars, 0, $this->cursor)) {
throw new RuntimeException('Unable to parse name, email and date');
}
$this->cursor += strlen($vars[1]);
return [$vars[2], $vars[3], $vars[4]];
}
protected function parseDate($text)
{
$date = \DateTime::createFromFormat('U e O', $text.' UTC');
if (!$date instanceof \DateTime) {
throw new RuntimeException(sprintf('Unable to convert "%s" to datetime', $text));
}
return $date;
}
}