Created
October 9, 2020 10:28
-
-
Save dipenparmar12/5b7ffae6b9355ba756537b9b651b6ec8 to your computer and use it in GitHub Desktop.
Generate git log ( author, date, msg and commit hash) array
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 | |
// Last 20 commits | |
$number_of_log = 20 | |
// Change To Repo Directory | |
chdir("/full/path/to/repo"); | |
// Load Last 10 Git Logs | |
$git_history = []; | |
$git_logs = []; | |
exec("git log -$number_of_log", $git_logs); | |
// Parse Logs | |
$last_hash = null; | |
foreach ($git_logs as $line) | |
{ | |
// Clean Line | |
$line = trim($line); | |
// Proceed If There Are Any Lines | |
if (!empty($line)) | |
{ | |
// Commit | |
if (strpos($line, 'commit') !== false) | |
{ | |
$hash = explode(' ', $line); | |
$hash = trim(end($hash)); | |
$git_history[$hash] = [ | |
'message' => '' | |
]; | |
$last_hash = $hash; | |
} | |
// Author | |
else if (strpos($line, 'Author') !== false) { | |
$author = explode(':', $line); | |
$author = trim(end($author)); | |
$git_history[$last_hash]['author'] = $author; | |
} | |
// Date | |
else if (strpos($line, 'Date') !== false) { | |
$date = explode(':', $line, 2); | |
$date = trim(end($date)); | |
$git_history[$last_hash]['date'] = date('d/m/Y H:i:s A', strtotime($date)); | |
} | |
// Message | |
else { | |
$git_history[$last_hash]['message'] .= $line ." "; | |
} | |
} | |
} | |
echo "<pre>"; | |
print_r($git_history); | |
echo "</pre>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment