Created
May 8, 2011 16:43
-
-
Save geeknam/961488 to your computer and use it in GitHub Desktop.
Parse git log with PHP to an array
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 | |
// Author: Ngo Minh Nam | |
$dir = "/path/to/your/repo/"; | |
$output = array(); | |
chdir($dir); | |
exec("git 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
if you do a str_replace to remove any "\n"s in your commit message than the code looks alot cleaner.