GIT Commits mit PHP auslesen und ausgeben
Möchte man bei einem Projekt die GIT Commits als History ausgeben, kann man folgenden Code dafür nutzen (vorher vielleicht noch schön machen):
$gitDir = '.';
$output = [];
chdir($gitDir);
exec('git log', $output);
$history = [];
foreach($output as $line){
if(strpos($line, 'commit')===0){
if(!empty($commit)){
array_push($history, $commit);
unset($commit);
}
$commit['hash'] = substr($line, strlen('commit'));
}elseif(strpos($line, 'Author')===0){
$commit['author'] = substr($line, strlen('Author:'));
}elseif(strpos($line, 'Date')===0){
$commit['date'] = substr($line, strlen('Date:'));
}else{
if(!isset($commit['message'])){
$commit['message'] = '';
}
$commit['message'] .= $line;
}
}
$content = '';
foreach($history as $item){
echo date('d.m.Y H:i:s', strtotime($item['date'])) . ': ' . $item['message'] . "\n";
}
Verbesserungsvorschläge sind gern gesehen!
