1
0
Fork 0

Automatically recover from some merge conflicts

pull/11517/head
Dan Wallis 2023-06-19 20:01:07 +01:00
parent bbea3e5284
commit af4ae5a7a7
No known key found for this signature in database
GPG Key ID: A02198884F2740BC
5 changed files with 2246 additions and 0 deletions

View File

@ -53,6 +53,10 @@ Some improvement _could_ be made to git's conflict resolving by using a custom g
An example of this can be found at [balbuf's composer git merge driver](https://github.com/balbuf/composer-git-merge-driver).
### Handling the trivial case
In a small number of cases, only the `content-hash` will show as being conflicted as the version control system may be able to cleanly merge the remaining text in the file. This typically happens when two different packages have been added or updated in each side of the merge, with no overlapping nor conflicting dependencies. When this occurs, running `composer update --lock` may be enough to update the lock file.
## Important considerations
Keep in mind that whenever merge conflicts occur on the lock file, the information, about the exact version

View File

@ -318,6 +318,14 @@ class JsonFile
}
$data = json_decode($json, true);
if (null === $data && JSON_ERROR_NONE !== json_last_error()) {
$lines = explode(PHP_EOL, $json);
if (isset($lines[9]) && strpos($lines[9], '"content-hash": "') !== false && strpos($lines[7], '"content-hash": "') !== false) {
// We may have found a git merge conflict in composer.lock. Remove the offending lines and try again.
unset($lines[6], $lines[7], $lines[9], $lines[10]);
$lines[8] = ' "content-hash": "VCS merge conflict detected. Please run `composer update --lock`.",';
return self::parseJson(implode(PHP_EOL, $lines), $file);
}
self::validateSyntax($json, $file);
}

View File

@ -0,0 +1,2 @@
/vendor/
/composer.lock

File diff suppressed because it is too large Load Diff

View File

@ -364,6 +364,63 @@ class JsonFileTest extends TestCase
$this->assertEquals($data, $doubleData);
}
public function testComposerLockFileMergeConflictSimple(): void
{
$data = [
'_readme' => [
'This file locks the dependencies of your project to a known state',
'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies',
'This file is @generated automatically',
],
'content-hash' => 'VCS merge conflict detected. Please run `composer update --lock`.',
'packages' => [],
'packages-dev' => [],
'aliases' => [],
'minimum-stability' => "stable",
'stability-flags' => [],
'prefer-stable' => false,
'prefer-lowest' => false,
'platform' => [],
'platform-dev' => [],
'plugin-api-version' => '2.3.0',
];
$json = '{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
<<<<<<< HEAD
"content-hash": "e0281a92ffdb4118e47df762a8e80d8d",
=======
"content-hash": "19ff2417ff3290c12442b3d170de974e",
>>>>>>> branch-name
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.3.0"
}';
$this->assertEquals($data, JsonFile::parseJson($json));
}
public function testComposerLockFileMergeConflictComplex(): void
{
$data = (string) file_get_contents(__DIR__ . '/Fixtures/composer-lock-merge-conflict-complex.txt');
$this->expectException(ParsingException::class);
// We don't care here what the error message says, just that there is an exception thrown.
// Other tests verify that the message text is sensible.
JsonFile::parseJson($data);
}
private function expectParseException(string $text, string $json): void
{
try {