Created
March 20, 2016 12:20
-
-
Save jankal/70abaf5eb3df903e2ec6 to your computer and use it in GitHub Desktop.
Parse outputs of `git log`
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 | |
function parseLog($log) { | |
$lines = explode("\n", $log); | |
$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; | |
} | |
} | |
} | |
return $history; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works a treat for me too