-
-
Save sarfraznawaz2005/4b1e3cd3a7c08be9233445fc6fa62d90 to your computer and use it in GitHub Desktop.
Parse commit log with loophp/collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
include 'vendor/autoload.php'; | |
use loophp\collection\Collection; | |
use loophp\collection\Contract\Operation\Sortable; | |
$commandStream = static function (string $command): Generator { | |
$fh = \popen($command, 'r'); | |
while (false !== $line = fgets($fh)) { | |
yield $line; | |
} | |
\fclose($fh); | |
}; | |
$c = Collection::fromIterable($commandStream('git log')) | |
->compact('', "\n") | |
->map( | |
static function (string $value): string { | |
return trim($value); | |
} | |
) | |
->associate( | |
static function (int $key, string $value) use (&$lastCommitId): string { | |
if (strpos($value, 'commit ') === 0) { | |
[, $commitId] = explode('commit ', $value, 2); | |
$lastCommitId = $commitId; | |
} | |
return $lastCommitId; | |
} | |
) | |
->group() | |
->map( | |
static function (array $values, string $commitId): array { | |
$callbackFilter = static function ($value): bool | |
{ | |
return 1 !== preg_match('/^commit \b[0-9a-f]{5,40}\b/', $value); | |
}; | |
$callbackMap1 = static function($value, $key) { | |
$modifiers = [ | |
'Signed-off-by', | |
'Date', | |
'Author' | |
]; | |
$delimiter = ':'; | |
foreach ($modifiers as $modifier) { | |
$searchItemWithColumn = sprintf('%s%s', $modifier, $delimiter); | |
if (strpos($value, $searchItemWithColumn) === 0) { | |
[,$data] = explode($searchItemWithColumn, $value, 2); | |
return [ | |
strtolower($modifier) => trim($data), | |
]; | |
} | |
} | |
return $value; | |
}; | |
$callbackMap2 = static function ($value) { | |
return is_string($value) ? | |
['log' => $value] : | |
$value; | |
}; | |
$callbackMap3 = static function ($value): string { | |
return is_array($value) ? | |
implode(PHP_EOL, $value): | |
$value; | |
}; | |
return Collection::fromIterable($values) | |
->filter($callbackFilter) | |
->map($callbackMap1) | |
->map($callbackMap2) | |
->unwrap() | |
->group() | |
->map($callbackMap3) | |
->merge(['commit' => $commitId]) | |
->sort(Sortable::BY_KEYS) | |
->all(); | |
} | |
) | |
->normalize() | |
->limit(20); | |
print_r($c->all()); | |
/** | |
Array | |
( | |
[0] => Array | |
( | |
[author] => Pol Dellaiera <[email protected]> | |
[commit] => 81c7681622040f3cb4bce207e4489010f40e13c7 | |
[date] => Sun Aug 16 19:11:18 2020 +0200 | |
[log] => WIP | |
) | |
[1] => Array | |
( | |
[author] => Pol Dellaiera <[email protected]> | |
[commit] => e716b854527e576d3e9f76455d0f5f8dfe20bf17 | |
[date] => Sun Aug 16 16:12:53 2020 +0200 | |
[log] => Update typing information. | |
) | |
... | |
[18] => Array | |
( | |
[author] => Pol Dellaiera <[email protected]> | |
[commit] => 940fcec1d86d7296d7c87017755e0f6b09a67802 | |
[date] => Thu Aug 6 23:37:41 2020 +0200 | |
[log] => Update documentation. | |
) | |
[19] => Array | |
( | |
[author] => Pol Dellaiera <[email protected]> | |
[commit] => 54d2559e2af97a33c7600ab2e8e0bae76ea7a64a | |
[date] => Thu Aug 6 22:36:07 2020 +0200 | |
[log] => Add docker stack for building documentation locally. | |
) | |
) | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment