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