diff --git a/src/Gitonomy/Git/Parser/LogParser.php b/src/Gitonomy/Git/Parser/LogParser.php index 53e5b16..67b8668 100644 --- a/src/Gitonomy/Git/Parser/LogParser.php +++ b/src/Gitonomy/Git/Parser/LogParser.php @@ -51,6 +51,7 @@ protected function doParse() $this->consumeGPGSignature(); $this->consumeNewLine(); + $this->consumeUnsupportedLinesToNewLine(); if ($this->cursor < strlen($this->content)) { $this->consumeNewLine(); } @@ -76,4 +77,15 @@ protected function doParse() $this->log[] = $commit; } } + + protected function consumeUnsupportedLinesToNewLine() + { + // Consume any unsupported lines that may appear in the log output. For + // example, gitbutler headers or other custom metadata but this should + // work regardless of the content. + while (!$this->isFinished() && substr($this->content, $this->cursor, 1) !== "\n") { + $this->consumeTo("\n"); + $this->consumeNewLine(); + } + } } diff --git a/tests/Gitonomy/Git/Tests/LogTest.php b/tests/Gitonomy/Git/Tests/LogTest.php index 970e55c..a02184d 100644 --- a/tests/Gitonomy/Git/Tests/LogTest.php +++ b/tests/Gitonomy/Git/Tests/LogTest.php @@ -12,6 +12,8 @@ namespace Gitonomy\Git\Tests; +use Gitonomy\Git\Parser\LogParser; + class LogTest extends AbstractTest { /** @@ -91,4 +93,44 @@ public function testFirstMessageEmpty() $commits = $repository->getLog()->getCommits(); $this->assertCount(1, $commits); } + + public function testParsesCommitsWithAndWithoutGitButlerHeaders(): void + { + $logContent = <<<'EOT' + commit 1111111111111111111111111111111111111111 + tree abcdefabcdefabcdefabcdefabcdefabcdefabcd + author John Doe 1620000000 +0000 + committer John Doe 1620000000 +0000 + + First commit message + + commit 2222222222222222222222222222222222222222 + tree abcdefabcdefabcdefabcdefabcdefabcdefabcd + parent 1111111111111111111111111111111111111111 + author Jane Smith 1620003600 +0000 + committer Jane Smith 1620003600 +0000 + gitbutler-headers-version: 2 + gitbutler-change-id: a7bd485c-bae6-45b2-910f-163c78aace81 + + Commit with GitButler headers + + commit 3333333333333333333333333333333333333333 + tree abcdefabcdefabcdefabcdefabcdefabcdefabcd + author John Doe 1620007200 +0000 + committer Jane Smith 1620007200 +0000 + + Another commit without GitButler headers + + EOT; + + $parser = new LogParser(); + $parser->parse($logContent); + + $log = $parser->log; + $this->assertCount(3, $log); + + $this->assertEquals("First commit message\n", $log[0]['message']); + $this->assertEquals("Commit with GitButler headers\n", $log[1]['message']); + $this->assertEquals("Another commit without GitButler headers\n", $log[2]['message']); + } }