Skip to content

Instantly share code, notes, and snippets.

@jankal
Forked from geeknam/git_history.php
Last active March 20, 2016 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jankal/a92f01aead3d5becab41 to your computer and use it in GitHub Desktop.
Save jankal/a92f01aead3d5becab41 to your computer and use it in GitHub Desktop.
Parse git log with PHP to an array
<?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