#!/usr/bin/env php
<?php
$wd = getcwd();
$ref = $argv[1] ?? '' ?: 'master';
$git_dir = shell_exec('git rev-parse --show-toplevel');
if(!$git_dir) {
fprintf(STDERR, 'Not inside a git path');
exit(1);
}
$list_raw = explode(PHP_EOL, trim(shell_exec("git branch --list --format '%(objectname) %(refname:short) %(committerdate)'")));
$branches = [];
$shas = [];
foreach($list_raw as $row)
{
$columns = explode(' ', $row, 3);
$columns = array_values(array_filter($columns));
$branches[$columns[1]] = [
'sha' => $columns[0],
'name' => $columns[1],
'date' => $columns[2],
'time' => strtotime($columns[2]),
];
$shas[$columns[0]] = [];
}
foreach(array_keys($shas) as $sha)
{
$log = trim(shell_exec('git log --oneline ' . $ref . '..' . $sha));
if($log)
{
$shas[$sha] = count(explode(PHP_EOL, $log));
continue;
}
$log = trim(shell_exec('git log --oneline ' . $sha . '..' . $ref));
if($log)
{
$shas[$sha] = -count(explode(PHP_EOL, $log));
continue;
}
$shas[$sha] = 0;
}
foreach(array_keys($branches) as $branch)
{
$branches[$branch]['commits'] = $shas[$branches[$branch]['sha']];
}
if(empty($branches[$ref]))
{
$branches[$ref] = [
'sha' => $ref,
'name' => $ref,
'date' => null,
'time' => 0,
'commits' => 0,
];
}
usort($branches, function($a, $b) {
if($a['commits'] < 1 && $b['commits'] > 0)
{
return 1;
}
if($a['commits'] > 0 && $b['commits'] < 1)
{
return -1;
}
if($a['time'] !== $b['time'])
{
return $b['time'] - $a['time'];
}
if($a['commits'] !== $b['commits'])
{
return $b['commits'] - $a['commits'];
}
return strcmp($a['name'], $b['name']);
});
$timelimit = time() - 12*3600;
foreach($branches as $branch)
{
if($branch['commits']) {
printf(
"%5d %10s %s\n",
$branch['commits'],
date($branch['time'] < $timelimit ? 'Y-m-d' : ' H:i:s ', $branch['time']),
$branch['name']
);
} else {
echo ' -- ', $branch['name'], ' --', PHP_EOL;
}
}
Comments