|
3 | 3 | namespace Igorsgm\GitHooks\Git;
|
4 | 4 |
|
5 | 5 | use Carbon\Carbon;
|
| 6 | +use Illuminate\Support\Str; |
6 | 7 |
|
7 | 8 | class Log
|
8 | 9 | {
|
@@ -52,17 +53,30 @@ public function __construct(string $log)
|
52 | 53 | */
|
53 | 54 | private function parse(array $lines): void
|
54 | 55 | {
|
55 |
| - foreach ($lines as $line) { |
56 |
| - if (strpos($line, 'commit') === 0) { |
| 56 | + $handlers = collect([ |
| 57 | + 'commit' => function ($line) { |
57 | 58 | preg_match('/(?<hash>[a-z0-9]{40})/', $line, $matches);
|
58 | 59 | $this->hash = $matches['hash'] ?? null;
|
59 |
| - } elseif (strpos($line, 'Author') === 0) { |
| 60 | + }, |
| 61 | + 'Author' => function ($line) { |
60 | 62 | $this->author = substr($line, strlen('Author:') + 1);
|
61 |
| - } elseif (strpos($line, 'Date') === 0) { |
| 63 | + }, |
| 64 | + 'Date' => function ($line) { |
62 | 65 | $this->date = Carbon::parse(substr($line, strlen('Date:') + 3));
|
63 |
| - } elseif (strpos($line, 'Merge') === 0) { |
| 66 | + }, |
| 67 | + 'Merge' => function ($line) { |
64 | 68 | $merge = substr($line, strlen('Merge:') + 1);
|
65 | 69 | $this->merge = explode(' ', $merge);
|
| 70 | + }, |
| 71 | + ]); |
| 72 | + |
| 73 | + foreach ($lines as $line) { |
| 74 | + $handler = $handlers->first(function ($handler, $prefix) use ($line) { |
| 75 | + return Str::startsWith($line, $prefix); |
| 76 | + }); |
| 77 | + |
| 78 | + if ($handler !== null) { |
| 79 | + $handler($line); |
66 | 80 | } elseif (! empty($line)) {
|
67 | 81 | $this->message .= substr($line, 4)."\n";
|
68 | 82 | }
|
|
0 commit comments