-
-
Save jankal/a92f01aead3d5becab41 to your computer and use it in GitHub Desktop.
Parse git log with PHP to an array
This file contains hidden or 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 | |
// Author: Ngo Minh Nam | |
$dir = "/path/to/your/repo/"; | |
$output = array(); | |
chdir($dir); | |
exec("git log", $output); | |
$history = array(); | |
foreach($lines as $key => $line) { | |
if(strpos($line, 'commit') === 0 || $key + 1 == count($lines)){ | |
if(!empty($commit)){ | |
$commit['message'] = substr($commit['message'], 4); | |
array_push($history, $commit); | |
unset($commit); | |
} | |
$commit['hash'] = substr($line, strlen('commit') + 1); | |
} else if(strpos($line, 'Author') === 0){ | |
$commit['author'] = substr($line, strlen('Author:') + 1); | |
} else if(strpos($line, 'Date') === 0){ | |
$commit['date'] = substr($line, strlen('Date:') + 3); | |
} elseif (strpos($line, 'Merge') === 0) { | |
$commit['merge'] = substr($line, strlen('Merge:') + 1); | |
$commit['merge'] = explode(' ', $commit['merge']); | |
} else { | |
if(isset($commit['message'])) { | |
$commit['message'] .= $line; | |
} else { | |
$commit['message'] = $line; | |
} | |
} | |
} | |
print_r($history); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment