Skip to content

Instantly share code, notes, and snippets.

@dipenparmar12
Created October 9, 2020 10:28
Show Gist options
  • Save dipenparmar12/5b7ffae6b9355ba756537b9b651b6ec8 to your computer and use it in GitHub Desktop.
Save dipenparmar12/5b7ffae6b9355ba756537b9b651b6ec8 to your computer and use it in GitHub Desktop.
Generate git log ( author, date, msg and commit hash) array
<?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