1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Changelog keeper |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/chkipper |
6
|
|
|
* @package chkipper |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\chkipper\lib\parsers; |
12
|
|
|
|
13
|
|
|
use UnexpectedValueException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Git log parser. |
17
|
|
|
* |
18
|
|
|
* @author Andrii Vasyliev <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class GitLogParser extends AbstractParser |
21
|
|
|
{ |
22
|
|
|
public function parseGitLog() |
23
|
|
|
{ |
24
|
|
|
exec("git log --date=short --pretty='format:%h %ad %s [%ae] %d'", $logs); |
25
|
|
|
|
26
|
|
|
return $this->parseLines($logs); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
public function parseLines(array $lines) |
30
|
|
|
{ |
31
|
1 |
|
$this->getHistory()->initTags(); |
32
|
|
|
|
33
|
1 |
|
foreach ($lines as $line) { |
34
|
1 |
|
if (!preg_match('/^(\w+) (([0-9-]+) (.*?) \[.*?\]) *\(?(.*?)\)?$/', $line, $m)) { |
35
|
|
|
throw new UnexpectedValueException('failed parse git line'); |
36
|
|
|
} |
37
|
1 |
|
$tag = $this->matchTag($m[5]); |
38
|
1 |
|
$note = $this->matchNote($m[4]); |
39
|
1 |
|
if ($tag) { |
40
|
1 |
|
$this->addTag($tag, $m[3]); |
41
|
1 |
|
} |
42
|
1 |
|
if (false && $note) { /// disabled adding notes |
43
|
|
|
$this->addNote($note); |
44
|
|
|
} |
45
|
1 |
|
$this->addCommit($m[1], $m[2]); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
1 |
|
return $this->getHistory(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Finds first tag in given refs string. |
53
|
|
|
* @param string $str |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
1 |
|
public function matchTag($str) |
57
|
|
|
{ |
58
|
1 |
|
$refs = explode(', ', $str); |
59
|
1 |
|
foreach ($refs as $ref) { |
60
|
1 |
|
if (preg_match('/^tag: (.*)$/', $ref, $m)) { |
61
|
1 |
|
return $m[1]; |
62
|
|
|
} |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function matchNote($str) |
69
|
|
|
{ |
70
|
1 |
|
return strpos($str, ' ') ? $str : null; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|