Skip to content

Commit 145e2b8

Browse files
committed
Improve Log parsing, fix ProcessHelper bug, and update .scrutinizer.yml
1 parent c3d4656 commit 145e2b8

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

.scrutinizer.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ build:
22
environment:
33
php:
44
version: 8.1
5+
node: "v16.20.0"
56
nodes:
67
analysis:
78
project_setup:
@@ -10,6 +11,8 @@ build:
1011
override: [ php-scrutinizer-run ]
1112
coverage:
1213
tests:
14+
before:
15+
- command: git config --global user.email "[email protected]" && git config --global user.name "GitHub Actions"
1316
override:
1417
- command: XDEBUG_MODE=coverage ./vendor/bin/pest --coverage --coverage-clover=coverage.clover
1518
coverage:

src/Git/Log.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Igorsgm\GitHooks\Git;
44

55
use Carbon\Carbon;
6+
use Illuminate\Support\Str;
67

78
class Log
89
{
@@ -52,17 +53,30 @@ public function __construct(string $log)
5253
*/
5354
private function parse(array $lines): void
5455
{
55-
foreach ($lines as $line) {
56-
if (strpos($line, 'commit') === 0) {
56+
$handlers = collect([
57+
'commit' => function ($line) {
5758
preg_match('/(?<hash>[a-z0-9]{40})/', $line, $matches);
5859
$this->hash = $matches['hash'] ?? null;
59-
} elseif (strpos($line, 'Author') === 0) {
60+
},
61+
'Author' => function ($line) {
6062
$this->author = substr($line, strlen('Author:') + 1);
61-
} elseif (strpos($line, 'Date') === 0) {
63+
},
64+
'Date' => function ($line) {
6265
$this->date = Carbon::parse(substr($line, strlen('Date:') + 3));
63-
} elseif (strpos($line, 'Merge') === 0) {
66+
},
67+
'Merge' => function ($line) {
6468
$merge = substr($line, strlen('Merge:') + 1);
6569
$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);
6680
} elseif (! empty($line)) {
6781
$this->message .= substr($line, 4)."\n";
6882
}

src/Traits/ProcessHelper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public function runCommands($commands, $params = [])
3131
});
3232
}
3333

34-
if (! empty($input->definition) && $input->definition->hasOption('quiet') && $input->getOption('quiet')) {
34+
if (! empty($input->definition) && $input->definition->hasOption('quiet') &&
35+
! empty($input) && $input->getOption('quiet')
36+
) {
3537
$commands = $this->transformCommands($commands, function ($value) {
3638
return $value.' --quiet';
3739
});
@@ -68,7 +70,7 @@ public function runCommands($commands, $params = [])
6870
}
6971

7072
/**
71-
* @param array $commands
73+
* @param array|string $commands
7274
* @param callable $callback
7375
* @return array
7476
*/

0 commit comments

Comments
 (0)