Skip to content

Instantly share code, notes, and snippets.

@jankal
Created March 20, 2016 12:20
Show Gist options
  • Save jankal/70abaf5eb3df903e2ec6 to your computer and use it in GitHub Desktop.
Save jankal/70abaf5eb3df903e2ec6 to your computer and use it in GitHub Desktop.
Parse outputs of `git log`
<?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;
}
@PF94
Copy link

PF94 commented Feb 22, 2021

Does not work.

@jankal
Copy link
Author

jankal commented Feb 22, 2021

@chaziz94 I'm happy to assist even if it's kinda old.

I just tried this function in 3v4l, see here: https://3v4l.org/7eGFF

It still seems to work for me.

Can you show me how you are using it?

@dev-ocsi
Copy link

Works a treat for me too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment