Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse commit log with loophp/collection
<?php
declare(strict_types=1);
include 'vendor/autoload.php';
use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;
$commandStream = static function (string $command): Generator {
$fh = popen($command, 'r');
while (false !== $line = fgets($fh)) {
yield $line;
}
fclose($fh);
};
$buildIfThenElseCallbacks = static function (string $lineStart): array {
return [
static function ($line) use ($lineStart): bool {
return \is_string($line) && 0 === mb_strpos($line, $lineStart);
},
static function ($line) use ($lineStart): array {
[, $line] = explode($lineStart, $line);
return [
sprintf(
'%s:%s',
mb_strtolower(str_replace(':', '', $lineStart)),
trim($line)
),
];
},
];
};
$c = Collection::fromIterable($commandStream('git log'))
->map(
static function (string $value): string {
return trim($value);
}
)
->compact('', ' ', "\n")
->ifThenElse(...$buildIfThenElseCallbacks('commit'))
->ifThenElse(...$buildIfThenElseCallbacks('Date:'))
->ifThenElse(...$buildIfThenElseCallbacks('Author:'))
->ifThenElse(...$buildIfThenElseCallbacks('Merge:'))
->ifThenElse(...$buildIfThenElseCallbacks('Signed-off-by:'))
->split(
static function ($value): bool {
return \is_array($value) ?
(1 === preg_match('/^commit:\b[0-9a-f]{5,40}\b/', $value[0])) :
false;
}
)
->map(
static function (array $value): CollectionInterface {
return Collection::fromIterable($value);
}
)
->map(
static function (CollectionInterface $collection): CollectionInterface {
return $collection
->group(
static function ($value): ?string {
return \is_array($value) ? 'headers' : null;
}
)
->group(
static function ($value): ?string {
return \is_string($value) ? 'log' : null;
}
)
->ifThenElse(
static function ($value, $key): bool {
return 'headers' === $key;
},
static function ($value, $key): array {
return Collection::fromIterable($value)
->unwrap()
->associate(
static function ($key, string $value): string {
[$key, $line] = explode(':', $value, 2);
return $key;
},
static function ($key, string $value): string {
[$key, $line] = explode(':', $value, 2);
return trim($line);
}
)
->all();
}
);
}
)
->map(
static function (CollectionInterface $collection): CollectionInterface {
return $collection
->flatten()
->group(
static function ($value, $key): ?string {
if (is_numeric($key)) {
return 'log';
}
return null;
}
);
}
)
->map(
static function (CollectionInterface $collection): array {
return $collection->all();
}
)
->limit(52);
print_r($c->all());
/**
...
[43] => Array
(
[commit] => 84e63813ba8a0fdecb2b07275b892aee695cce0e
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Thu Aug 20 22:09:09 2020 +0200
[log] => Array
(
[0] => Update Split operation.
)
)
[44] => Array
(
[commit] => d1be59ef01651a7c7794175fe5b2d88e3e58e353
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Thu Aug 20 22:00:07 2020 +0200
[log] => Array
(
[0] => Add new example.
)
)
[45] => Array
(
[commit] => e716b854527e576d3e9f76455d0f5f8dfe20bf17
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Sun Aug 16 16:12:53 2020 +0200
[log] => Array
(
[0] => Update typing information.
)
)
[46] => Array
(
[commit] => 81b7a6f546848812b74a12309d7e701cbe7536e7
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Sun Aug 16 09:04:19 2020 +0200
[log] => Array
(
[0] => Update typing information.
)
)
[47] => Array
(
[commit] => 119cdfeb092b31970eac5a98f56e52d4f4ee8e69
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Sat Aug 15 23:02:15 2020 +0200
[log] => Array
(
[0] => Add missing return statements.
)
)
[48] => Array
(
[commit] => 1c9e929919e9482bec517aa746559d63071c79f2
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Sat Aug 15 21:57:01 2020 +0200
[log] => Array
(
[0] => Update Implode transformation.
)
)
[49] => Array
(
[commit] => cece391c57ab7800246fc5ebd788f0ec4c738322
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Sat Aug 15 21:56:31 2020 +0200
[log] => Array
(
[0] => Update FoldLeft/FoldRight transformations.
)
)
[50] => Array
(
[commit] => ddde5cc6f1dd14ac079b68c58bb2dd3f68aac8ee
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Sat Aug 15 07:58:56 2020 +0200
[log] => Array
(
[0] => Update Last operation.
)
)
[51] => Array
(
[commit] => e6e7116b7b75a8fbc4c50dfddd196c26c01cd496
[merge] => e6b25ac c5ae61a
[author] => Pol Dellaiera <pol.dellaiera@protonmail.com>
[date] => Fri Aug 14 20:50:05 2020 +0200
[log] => Array
(
[0] => Merge pull request #16 from loophp/dependabot/github_actions/actions/create-release-v1.1.3
[1] => Bump actions/create-release from v1.1.2 to v1.1.3
)
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.