Skip to content

Instantly share code, notes, and snippets.

@fbouchery
Last active March 14, 2024 09:49
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbouchery/969a7824acf0fc7b5cd6395b1c96a9e5 to your computer and use it in GitHub Desktop.
Save fbouchery/969a7824acf0fc7b5cd6395b1c96a9e5 to your computer and use it in GitHub Desktop.
Extract phpstan baseline history count from GIT
<?php
$baselineFile = 'phpstan-baseline.neon';
$branch = 'origin/main';
echo "date;count\n";
foreach (explode("\n", `git log {$branch} --pretty="format:%H;%cI" --date-order --reverse {$baselineFile}`) as $line) {
[$hash, $date] = explode(';', $line);
preg_match_all('`^\s+count:\s+(\d+)`m', `git show $hash:{$baselineFile}`, $matches);
echo $date, ';', array_sum(array_map('intval', $matches[1])), "\n";
}
@alexander-schranz
Copy link

alexander-schranz commented Nov 13, 2023

Nice 👍

For all which just want to copy & paste it into the terminal:

php -r '(function(string $baselineFile, string $branch) {
    echo "date;count\\n";
    foreach (explode("\\n", `git log {$branch} --pretty="format:%H;%cI" --date-order --reverse {$baselineFile}`) as $line) {
        [$hash, $date] = explode(";", $line);
        preg_match_all("`^\\s+count:\\s+(\\d+)`m", `git show $hash:{$baselineFile}`, $matches);
        echo $date, ";", array_sum(array_map("intval", $matches[1])), "\\n";
    }
})("phpstan-baseline.neon", "origin/main");'

For line numbers instead of error count:

php -r '(function(string $baselineFile, string $branch) {
    echo "date;count\\n";
    foreach (explode("\\n", `git log {$branch} --pretty="format:%H;%cI" --date-order --reverse {$baselineFile}`) as $line) {
        [$hash, $date] = explode(";", $line);
        $content = `git show $hash:{$baselineFile}`;
        echo $date, ";", count(explode(PHP_EOL, $content)) . PHP_EOL;
    }
})("phpstan-baseline.neon", "origin/main");'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment