Skip to content

Commit 6db9b44

Browse files
adrian-lorencidimopoulos
authored andcommitted
Consume merge tag (gitonomy#231)
1 parent 64d8e79 commit 6db9b44

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/Gitonomy/Git/Parser/CommitParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ protected function doParse()
4747
list($this->committerName, $this->committerEmail, $committerDate) = $this->consumeNameEmailDate();
4848
$this->committerDate = $this->parseDate($committerDate);
4949

50+
$this->consumeMergeTag();
51+
5052
// will consume an GPG signed commit if there is one
5153
$this->consumeGPGSignature();
5254

src/Gitonomy/Git/Parser/LogParser.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,21 @@ protected function doParse()
3737
}
3838

3939
$this->consume('author ');
40-
list($commit['authorName'], $commit['authorEmail'], $authorDate) = $this->consumeNameEmailDate();
40+
[$commit['authorName'], $commit['authorEmail'], $authorDate] = $this->consumeNameEmailDate();
4141
$commit['authorDate'] = $this->parseDate($authorDate);
4242
$this->consumeNewLine();
4343

4444
$this->consume('committer ');
45-
list($commit['committerName'], $commit['committerEmail'], $committerDate) = $this->consumeNameEmailDate();
45+
[$commit['committerName'], $commit['committerEmail'], $committerDate] = $this->consumeNameEmailDate();
4646
$commit['committerDate'] = $this->parseDate($committerDate);
4747

48+
$this->consumeMergeTag();
49+
4850
// will consume an GPG signed commit if there is one
4951
$this->consumeGPGSignature();
5052

5153
$this->consumeNewLine();
54+
$this->consumeUnsupportedLinesToNewLine();
5255
if ($this->cursor < strlen($this->content)) {
5356
$this->consumeNewLine();
5457
}
@@ -74,4 +77,15 @@ protected function doParse()
7477
$this->log[] = $commit;
7578
}
7679
}
80+
81+
protected function consumeUnsupportedLinesToNewLine() {
82+
// Consume any unsupported lines that may appear in the log output. For
83+
// example, gitbutler headers or other custom metadata but this should
84+
// work regardless of the content.
85+
while (!$this->isFinished() && substr($this->content, $this->cursor, 1) !== "\n") {
86+
$this->consumeTo("\n");
87+
$this->consumeNewLine();
88+
}
89+
}
90+
7791
}

src/Gitonomy/Git/Parser/ParserBase.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,18 @@ protected function consumeGPGSignature()
136136

137137
return $this->consumeTo("\n\n");
138138
}
139+
140+
protected function consumeMergeTag()
141+
{
142+
$expected = "\nmergetag ";
143+
$length = strlen($expected);
144+
$actual = substr($this->content, $this->cursor, $length);
145+
if ($actual != $expected) {
146+
return '';
147+
}
148+
$this->cursor += $length;
149+
150+
$this->consumeTo('-----END PGP SIGNATURE-----');
151+
$this->consume('-----END PGP SIGNATURE-----');
152+
}
139153
}

tests/Gitonomy/Git/Tests/LogTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Gitonomy\Git\Tests;
1414

15+
use Gitonomy\Git\Parser\LogParser;
16+
1517
class LogTest extends AbstractTest
1618
{
1719
/**
@@ -91,4 +93,45 @@ public function testFirstMessageEmpty()
9193
$commits = $repository->getLog()->getCommits();
9294
$this->assertCount(1, $commits);
9395
}
96+
97+
public function testParsesCommitsWithAndWithoutGitButlerHeaders(): void
98+
{
99+
$logContent = <<<EOT
100+
commit 1111111111111111111111111111111111111111
101+
tree abcdefabcdefabcdefabcdefabcdefabcdefabcd
102+
author John Doe <[email protected]> 1620000000 +0000
103+
committer John Doe <[email protected]> 1620000000 +0000
104+
105+
First commit message
106+
107+
commit 2222222222222222222222222222222222222222
108+
tree abcdefabcdefabcdefabcdefabcdefabcdefabcd
109+
parent 1111111111111111111111111111111111111111
110+
author Jane Smith <[email protected]> 1620003600 +0000
111+
committer Jane Smith <[email protected]> 1620003600 +0000
112+
gitbutler-headers-version: 2
113+
gitbutler-change-id: a7bd485c-bae6-45b2-910f-163c78aace81
114+
115+
Commit with GitButler headers
116+
117+
commit 3333333333333333333333333333333333333333
118+
tree abcdefabcdefabcdefabcdefabcdefabcdefabcd
119+
author John Doe <[email protected]> 1620007200 +0000
120+
committer Jane Smith <[email protected]> 1620007200 +0000
121+
122+
Another commit without GitButler headers
123+
124+
EOT;
125+
126+
$parser = new LogParser();
127+
$parser->parse($logContent);
128+
129+
$log = $parser->log;
130+
$this->assertCount(3, $log);
131+
132+
$this->assertEquals("First commit message\n", $log[0]['message']);
133+
$this->assertEquals("Commit with GitButler headers\n", $log[1]['message']);
134+
$this->assertEquals("Another commit without GitButler headers\n", $log[2]['message']);
135+
}
136+
94137
}

0 commit comments

Comments
 (0)