Skip to content

Fix incorrect file name on new or deleted empty files #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Gitonomy/Git/Parser/DiffParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function doParse()
$newMode = $this->consumeTo("\n");
$this->consumeNewLine();
$oldMode = null;
$oldName = '/dev/null';
}
if ($this->expects('old mode ')) {
$oldMode = $this->consumeTo("\n");
Expand All @@ -49,6 +50,7 @@ protected function doParse()
if ($this->expects('deleted file mode ')) {
$oldMode = $this->consumeTo("\n");
$newMode = null;
$newName = '/dev/null';
$this->consumeNewLine();
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Gitonomy/Git/Tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,26 @@ public function testWorksWithUmlauts($repository)
$files = $repository->getCommit(self::FILE_WITH_UMLAUTS_COMMIT)->getDiff()->getFiles();
$this->assertSame('file_with_umlauts_\303\244\303\266\303\274', $files[0]->getNewName());
}

public function testEmptyNewFile()
{
$diff = Diff::parse("diff --git a/test b/test\nnew file mode 100644\nindex 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391\n");
$firstFile = $diff->getFiles()[0];

$this->assertTrue($firstFile->isCreation());
$this->assertFalse($firstFile->isDeletion());
$this->assertSame('test', $firstFile->getNewName());
$this->assertNull($firstFile->getOldName());
}

public function testEmptyOldFile()
{
$diff = Diff::parse("diff --git a/test b/test\ndeleted file mode 100644\nindex e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000\n");
$firstFile = $diff->getFiles()[0];

$this->assertFalse($firstFile->isCreation());
$this->assertTrue($firstFile->isDeletion());
$this->assertNull($firstFile->getNewName());
$this->assertSame('test', $firstFile->getOldName());
}
}