Skip to content

Commit

Permalink
Fix incorrect file name on new or deleted empty files (#226)
Browse files Browse the repository at this point in the history
* Fix incorrect file name on new or deleted empty files

* Add test for empty file diff
  • Loading branch information
Patrick-Beuks authored Jan 27, 2025
1 parent ac17834 commit 16214e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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());
}
}

0 comments on commit 16214e3

Please sign in to comment.