Skip to content

Instantly share code, notes, and snippets.

@nexeck
Forked from geeknam/git_history.php
Created November 24, 2011 15:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nexeck/1391525 to your computer and use it in GitHub Desktop.
Save nexeck/1391525 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);
// Linux
exec("git log",$output);
// Windows
exec('"C:\Program Files (x86)\Git\bin\git.exe" ' ."log", $output);
$history = array();
foreach($output as $line){
if(strpos($line, 'commit')===0){
if(!empty($commit)){
array_push($history, $commit);
unset($commit);
}
$commit['hash'] = substr($line, strlen('commit'));
}
else if(strpos($line, 'Author')===0){
$commit['author'] = substr($line, strlen('Author:'));
}
else if(strpos($line, 'Date')===0){
$commit['date'] = substr($line, strlen('Date:'));
}
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