Last active
November 14, 2022 09:38
-
-
Save kasparsd/ffefa07b05bc5668b4784f4826c6ce82 to your computer and use it in GitHub Desktop.
This file contains 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 | |
function list_to_file( $list, $file ) { | |
array_unshift( $list, array_keys( reset( $list ) ) ); // Append the keys to the top of the file. | |
$csv_file = fopen( $file, 'w' ); | |
foreach ( $list as $fields ) { | |
fputcsv( $csv_file, $fields ); | |
} | |
fclose( $csv_file ); | |
} | |
$log = []; | |
$format = [ | |
'commit' => '%h', | |
'author' => '%an', | |
'date' => '%ah', | |
'message' => '%s', | |
]; | |
$commit_key = 'commit'; | |
$log_keys = array_keys( $format ); | |
$command = sprintf( | |
'git log --no-merges --shortstat --pretty=%s', | |
escapeshellarg( sprintf( 'format:%s', implode( '%x09', $format ) ) ), // Seperate by tabs. | |
); | |
$log = shell_exec( $command ); | |
$commits = []; | |
$log = explode( "\n\n", $log ); | |
// Create an array of all data for each commit. | |
$log = array_map( | |
function( $log_lines ) use ( $log_keys ) { | |
list( $commit, $changes ) = explode( "\n", $log_lines ); | |
$commit = array_combine( $log_keys, explode( "\t", $commit ) ); | |
$changes = array_values( array_filter( explode( ' ', $changes ), 'is_numeric', ) ); | |
$changes = array_combine( [ 'changed', 'inserted', 'deleted' ], array_replace( [ 0, 0, 0 ], $changes ) ); | |
return array_merge( $commit, $changes ); | |
}, | |
$log | |
); | |
list_to_file( $log, 'commits-until.csv' ); | |
$authors = array_unique( array_map( | |
function ( $meta ) { | |
return $meta['author']; | |
}, | |
$log | |
) ); | |
$range_start = floor( strtotime( end( $log )['date'] ) / 86400 ) * 86400; | |
$range_end = ceil( strtotime( reset( $log )['date'] ) / 86400 ) * 86400; | |
$dates = array_fill( 0, ( $range_end - $range_start ) / 86400, $range_start ); | |
array_walk( | |
$dates, | |
function( &$value, $key ) { | |
$value = $value + ( $key * 86400 ); | |
} | |
); | |
$by_author_by_dates = []; | |
$author_commits = array_combine( $authors, array_fill( 0, count( $authors ), 0 ) ); | |
foreach ( $dates as $date ) { | |
$date_data = array_merge( | |
[ | |
'date' => date( 'r', $date ), | |
'commits' => '', | |
], | |
$author_commits | |
); | |
$commits_for_day = array_filter( | |
$log, | |
function( $meta ) use ( $date ) { | |
$commit_timestamp = strtotime( $meta['date'] ); | |
return ( $commit_timestamp >= $date && $commit_timestamp < $date + 86400 ); | |
} | |
); | |
foreach ( $commits_for_day as $commit ) { | |
$date_data[ $commit['author'] ] += intval( $commit['changed'] ); | |
$date_data['commits'] .= sprintf( ',%s', $commit['commit'] ); | |
} | |
$by_author_by_dates[] = $date_data; | |
} | |
list_to_file( $by_author_by_dates, 'commits-by-author.csv' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment