Skip to content

Commit

Permalink
Update existing package instead of reverting changes in the updated p…
Browse files Browse the repository at this point in the history
…ackage to ensure we keep all metadata intact, fixes composer#11787
  • Loading branch information
Seldaek committed Feb 23, 2024
1 parent c4f356d commit efa03f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
46 changes: 22 additions & 24 deletions src/Composer/DependencyResolver/LockTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Composer\Package\AliasPackage;
use Composer\Package\BasePackage;
use Composer\Package\Package;
use Composer\Package\PackageInterface;
use Composer\Pcre\Preg;

/**
Expand Down Expand Up @@ -111,21 +110,21 @@ public function getNewLockPackages(bool $devMode, bool $updateMirrors = false):

$packages[] = $package;

// if we're just updating mirrors we need to reset references to the same as currently "present" packages' references to keep the lock file as-is
// if we're just updating mirrors we need to reset everything to the same as currently "present" packages' references to keep the lock file as-is
if ($updateMirrors === true && !array_key_exists(spl_object_hash($package), $this->presentMap)) {
$this->resetDistSourceRefs($package);
$packages[count($packages) - 1] = $this->updateMirrorAndUrls($package);
}
}

return $packages;
}

/**
* Resets dist and source references to what they were in presentMap
* Try to return the original package from presentMap with updated URLs/mirrors
*
* This does not reset references if the currently present package didn't have any, or if the type of VCS has changed
* If the type of source/dist changed, then we do not update those and keep them as they were
*/
private function resetDistSourceRefs(PackageInterface $package): void
private function updateMirrorAndUrls(BasePackage $package): BasePackage
{
foreach ($this->presentMap as $presentPackage) {
if ($package->getName() !== $presentPackage->getName()) {
Expand All @@ -144,31 +143,30 @@ private function resetDistSourceRefs(PackageInterface $package): void
continue;
}

// if the dist type changed, we only override the source reference and leave the rest as is even though it is not an ideal situation
if ($presentPackage->getDistType() !== $package->getDistType()) {
$package->setSourceReference($presentPackage->getSourceReference());
break;
}

// otherwise we can update source and dist references to match what they were previously
$package->setSourceDistReferences($presentPackage->getSourceReference());

// if the dist url is not one of those handled gracefully by setSourceDistReferences then we should overwrite it with the old one
if ($package->getDistUrl() !== null && !Preg::isMatch('{^https?://(?:(?:www\.)?bitbucket\.org|(api\.)?github\.com|(?:www\.)?gitlab\.com)/}i', $package->getDistUrl())) {
$package->setDistUrl($presentPackage->getDistUrl());
if ($presentPackage instanceof Package) {
$presentPackage->setSourceUrl($package->getSourceUrl());
$presentPackage->setSourceMirrors($package->getSourceMirrors());
}
$package->setDistType($presentPackage->getDistType());

if ($package instanceof Package) {
$package->setDistSha1Checksum($presentPackage->getDistSha1Checksum());
// if the dist type changed, we only update the source url/mirrors
if ($presentPackage->getDistType() !== $package->getDistType()) {
return $presentPackage;
}

if ($presentPackage->getReleaseDate() !== null && $package instanceof Package) {
$package->setReleaseDate($presentPackage->getReleaseDate());
// update dist url if it is in a known format
if (
$package->getDistUrl() !== null
&& $presentPackage->getDistReference() !== null
&& Preg::isMatch('{^https?://(?:(?:www\.)?bitbucket\.org|(api\.)?github\.com|(?:www\.)?gitlab\.com)/}i', $package->getDistUrl())
) {
$presentPackage->setDistUrl(Preg::replace('{(?<=/|sha=)[a-f0-9]{40}(?=/|$)}i', $presentPackage->getDistReference(), $package->getDistUrl()));
}
$presentPackage->setDistMirrors($package->getDistMirrors());

break;
return $presentPackage;
}

return $package;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ g/g is dev and installed in a different ref than the #ref, so it gets updated an
"package": [
{
"name": "a/a", "version": "dev-master",
"source": { "reference": "2222222222222222222222222222222222222222", "url": "https://github.com/a/newa", "type": "git" },
"dist": { "reference": "2222222222222222222222222222222222222222", "url": "https://api.github.com/repos/a/newa/zipball/2222222222222222222222222222222222222222", "type": "zip" },
"require": { "b/b": "^2.0.1" },
"source": { "reference": "2222222222222222222222222222222222222222", "url": "https://github.com/a/newa", "type": "git", "mirrors": [{"url": "https://example.org/src/%package%/%version%/r%reference%.%type%", "preferred": true}] },
"dist": { "reference": "2222222222222222222222222222222222222222", "url": "https://api.github.com/repos/a/newa/zipball/2222222222222222222222222222222222222222", "type": "zip", "mirrors": [{"url": "https://example.org/dists/%package%/%version%/r%reference%.%type%", "preferred": true}] },
"time": "2021-03-27T14:32:16+00:00"
},
{
Expand Down Expand Up @@ -67,6 +68,7 @@ g/g is dev and installed in a different ref than the #ref, so it gets updated an
[
{
"name": "a/a", "version": "dev-master",
"require": { "b/b": "^2" },
"source": { "reference": "1111111111111111111111111111111111111111", "url": "https://github.com/a/a", "type": "git" },
"dist": { "reference": "1111111111111111111111111111111111111111", "url": "https://api.github.com/repos/a/a/zipball/1111111111111111111111111111111111111111", "type": "zip" },
"time": "2021-03-14T16:24:37+00:00"
Expand Down Expand Up @@ -102,6 +104,7 @@ g/g is dev and installed in a different ref than the #ref, so it gets updated an
"packages": [
{
"name": "a/a", "version": "dev-master",
"require": { "b/b": "^2" },
"source": { "reference": "1111111111111111111111111111111111111111", "url": "https://github.com/a/a", "type": "git" },
"dist": { "reference": "1111111111111111111111111111111111111111", "url": "https://api.github.com/repos/a/a/zipball/1111111111111111111111111111111111111111", "type": "zip" },
"time": "2021-03-14T16:24:37+00:00",
Expand Down Expand Up @@ -152,8 +155,9 @@ g/g is dev and installed in a different ref than the #ref, so it gets updated an
"packages": [
{
"name": "a/a", "version": "dev-master",
"source": { "reference": "1111111111111111111111111111111111111111", "url": "https://github.com/a/newa", "type": "git" },
"dist": { "reference": "1111111111111111111111111111111111111111", "url": "https://api.github.com/repos/a/newa/zipball/1111111111111111111111111111111111111111", "type": "zip" },
"require": { "b/b": "^2" },
"source": { "reference": "1111111111111111111111111111111111111111", "url": "https://github.com/a/newa", "type": "git", "mirrors": [{"url": "https://example.org/src/%package%/%version%/r%reference%.%type%", "preferred": true}] },
"dist": { "reference": "1111111111111111111111111111111111111111", "url": "https://api.github.com/repos/a/newa/zipball/1111111111111111111111111111111111111111", "type": "zip", "mirrors": [{"url": "https://example.org/dists/%package%/%version%/r%reference%.%type%", "preferred": true}] },
"time": "2021-03-14T16:24:37+00:00",
"type": "library"
},
Expand Down

0 comments on commit efa03f9

Please sign in to comment.