diff --git a/src/Gitonomy/Git/Parser/DiffParser.php b/src/Gitonomy/Git/Parser/DiffParser.php index 3e95d1a..7b48293 100644 --- a/src/Gitonomy/Git/Parser/DiffParser.php +++ b/src/Gitonomy/Git/Parser/DiffParser.php @@ -97,20 +97,22 @@ protected function doParse(): void $newMode = $oldMode = $vars[0]; } $this->consumeNewLine(); + } - // verifying if the file was deleted or created - if ($this->expects('--- ')) { - $oldName = '/dev/null' === $this->consumeTo("\n") ? '/dev/null' : $oldName; - $this->consumeNewLine(); - $this->consume('+++ '); - $newName = '/dev/null' === $this->consumeTo("\n") ? '/dev/null' : $newName; - $this->consumeNewLine(); - } elseif ($this->expects('Binary files ')) { - $vars = $this->consumeRegexp('/"?(.*?)"? and "?(.*?)"? differ\n/'); - $isBinary = true; - $oldName = $vars[1]; - $newName = $vars[2]; - } + // verifying if the file was deleted or created + // Note: a dirty submodule diff has no "index " line, so this must + // not be nested inside the block above. + if ($this->expects('--- ')) { + $oldName = '/dev/null' === $this->consumeTo("\n") ? '/dev/null' : $oldName; + $this->consumeNewLine(); + $this->consume('+++ '); + $newName = '/dev/null' === $this->consumeTo("\n") ? '/dev/null' : $newName; + $this->consumeNewLine(); + } elseif ($this->expects('Binary files ')) { + $vars = $this->consumeRegexp('/"?(.*?)"? and "?(.*?)"? differ\n/'); + $isBinary = true; + $oldName = $vars[1]; + $newName = $vars[2]; } $oldName = '/dev/null' === $oldName ? null : substr($oldName, 2); diff --git a/tests/Gitonomy/Git/Tests/DiffTest.php b/tests/Gitonomy/Git/Tests/DiffTest.php index a79fd4b..5e06378 100644 --- a/tests/Gitonomy/Git/Tests/DiffTest.php +++ b/tests/Gitonomy/Git/Tests/DiffTest.php @@ -250,6 +250,27 @@ public function testEmptyOldFile(): void $this->assertSame('test', $firstFile->getOldName()); } + public function testDirtySubmoduleWithoutRaw(): void + { + $this->expectUserDeprecationMessage('Using Diff::parse without raw information is deprecated. See https://github.com/gitonomy/gitlib/issues/227.'); + + $diff = Diff::parse(<<<'DIFF' + diff --git a/sub b/sub + --- a/sub + +++ b/sub + @@ -1 +1 @@ + -Subproject commit e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 + +Subproject commit e69de29bb2d1d6434b8b29ae775ad8c2e48c5391-dirty + + DIFF); + $firstFile = $diff->getFiles()[0]; + + $this->assertFalse($firstFile->isCreation()); + $this->assertFalse($firstFile->isDeletion()); + $this->assertSame('sub', $firstFile->getOldName()); + $this->assertSame('sub', $firstFile->getNewName()); + } + protected function verifyCreateCommitDiff(Diff $diff): void { $files = $diff->getFiles();