Skip to content
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
5 changes: 4 additions & 1 deletion src/Gitonomy/Git/Parser/DiffParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ protected function doParse(): void
$fileIndex = 0;
while (!$this->isFinished()) {
// 1. title
$vars = $this->consumeRegexp("/diff --git \"?(a\\/.*?)\"? \"?(b\\/.*?)\"?\n/");
// The prefix is normally "a" and "b", but git's diff.mnemonicPrefix option
// (used by tools such as GrumPHP) can produce other single-letter prefixes
// ("c"ommit, "i"ndex, "o"bject, "w"ork tree), or "1"/"2" with --no-index.
$vars = $this->consumeRegexp("/diff --git \"?([^\\/\\s]\\/.*?)\"? \"?([^\\/\\s]\\/.*?)\"?\n/");
$oldName = $vars[1];
$newName = $vars[2];
// Get indexes from raw if it exists
Expand Down
20 changes: 20 additions & 0 deletions tests/Gitonomy/Git/Tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,26 @@ public function testThrowErrorOnBlobGetWithoutIndex(): void
$this->assertSame('', $file->getNewIndex());
}

public function testMnemonicPrefix(): void
{
$this->expectUserDeprecationMessage('Using Diff::parse without raw information is deprecated. See https://github.com/gitonomy/gitlib/issues/227.');

// With `diff.mnemonicPrefix` enabled, git replaces the default "a/" and "b/"
// prefixes with context-specific ones, e.g. "c/" (commit) and "i/" (index)
// for `git diff --cached`. See https://github.com/gitonomy/gitlib/issues/114.
$diff = Diff::parse(<<<'DIFF'
diff --git c/composer.json i/composer.json
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644
--- c/composer.json
+++ i/composer.json

DIFF);
$firstFile = $diff->getFiles()[0];

$this->assertSame('composer.json', $firstFile->getOldName());
$this->assertSame('composer.json', $firstFile->getNewName());
}

public function testEmptyNewFile(): void
{
$this->expectUserDeprecationMessage('Using Diff::parse without raw information is deprecated. See https://github.com/gitonomy/gitlib/issues/227.');
Expand Down