Skip to content

Commit e3ec605

Browse files
Matth--sebastianbergmann
authored andcommitted
Allow spaces in parser
The from and to in diffs did not include the full path name when spaces are being used. This change allows spaces in the file paths as well (no tabs).
1 parent ffc949a commit e3ec605

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/Parser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function parse(string $string): array
3737
$collected = [];
3838

3939
for ($i = 0; $i < $lineCount; ++$i) {
40-
if (preg_match('(^---\\s+(?P<file>\\S+))', $lines[$i], $fromMatch) &&
41-
preg_match('(^\\+\\+\\+\\s+(?P<file>\\S+))', $lines[$i + 1], $toMatch)) {
40+
if (preg_match('#^---\h+"?(?P<file>[^\\v\\t"]+)#', $lines[$i], $fromMatch) &&
41+
preg_match('#^\\+\\+\\+\\h+"?(?P<file>[^\\v\\t"]+)#', $lines[$i + 1], $toMatch)) {
4242
if ($diff !== null) {
4343
$this->parseFileDiff($diff, $collected);
4444

tests/ParserTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,49 @@ public function testParseWithMultipleChunks(): void
7171
$this->assertCount(4, $chunks[2]->getLines());
7272
}
7373

74+
public function testParseWithSpacesInFileNames(): void
75+
{
76+
$content =
77+
<<<PATCH
78+
diff --git a/Foo Bar.txt b/Foo Bar.txt
79+
index abcdefg..abcdefh 100644
80+
--- a/Foo Bar.txt
81+
+++ b/Foo Bar.txt
82+
@@ -20,4 +20,5 @@ class Foo
83+
const ONE = 1;
84+
const TWO = 2;
85+
+ const THREE = 3;
86+
const FOUR = 4;
87+
88+
PATCH;
89+
90+
$diffs = $this->parser->parse($content);
91+
92+
$this->assertEquals('a/Foo Bar.txt', $diffs[0]->getFrom());
93+
$this->assertEquals('b/Foo Bar.txt', $diffs[0]->getTo());
94+
}
95+
96+
public function testParseWithSpacesInFileNamesAndTimesamp(): void
97+
{
98+
$content =
99+
<<<PATCH
100+
diff --git a/Foo Bar.txt b/Foo Bar.txt
101+
index abcdefg..abcdefh 100644
102+
--- "a/Foo Bar.txt" 2020-10-02 13:31:52.938811371 +0200
103+
+++ "b/Foo Bar.txt" 2020-10-02 13:31:50.022792064 +0200
104+
@@ -20,4 +20,5 @@ class Foo
105+
const ONE = 1;
106+
const TWO = 2;
107+
+ const THREE = 3;
108+
const FOUR = 4;
109+
PATCH;
110+
111+
$diffs = $this->parser->parse($content);
112+
113+
$this->assertEquals('a/Foo Bar.txt', $diffs[0]->getFrom());
114+
$this->assertEquals('b/Foo Bar.txt', $diffs[0]->getTo());
115+
}
116+
74117
public function testParseWithRemovedLines(): void
75118
{
76119
$content = <<<END

0 commit comments

Comments
 (0)