Created
December 1, 2014 19:40
-
-
Save latheesan-k/3490cb6ab522ca87f535 to your computer and use it in GitHub Desktop.
Parse git logs into array in PHP
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 | |
// Change To Repo Directory | |
chdir("/full/path/to/repo"); | |
// Load Last 10 Git Logs | |
$git_history = []; | |
$git_logs = []; | |
exec("git log -10", $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>"; | |
?> |
what if the git commit message it
This is a commit message
* but how about this line
* another newline
the commit message above displays like this
This is a commit message * but how about this line * another newline
include some \n where you want a new line.
this breaks if a commit message contains the word "commit" instead of using !== false
replace with === 0
for more strictness
include some \n where you want a new line.
but how?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Much better.