Skip to content

Commit

Permalink
Improve error reporting when a tag was recreated or a commit is missi…
Browse files Browse the repository at this point in the history
…ng, fixes #10484
  • Loading branch information
Seldaek committed Feb 4, 2022
1 parent 7c2954d commit e7c04e3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Composer/Downloader/GitDownloader.php
Expand Up @@ -135,7 +135,7 @@ protected function doInstall(PackageInterface $package, $path, $url)
$this->setPushUrl($path, $url);
}

if ($newRef = $this->updateToCommit($path, $ref, $package->getPrettyVersion(), $package->getReleaseDate())) {
if ($newRef = $this->updateToCommit($package, $path, $ref, $package->getPrettyVersion(), $package->getReleaseDate())) {
if ($package->getDistReference() === $package->getSourceReference()) {
$package->setDistReference($newRef);
}
Expand Down Expand Up @@ -186,7 +186,7 @@ protected function doUpdate(PackageInterface $initial, PackageInterface $target,
};

$this->gitUtil->runCommand($commandCallable, $url, $path);
if ($newRef = $this->updateToCommit($path, $ref, $target->getPrettyVersion(), $target->getReleaseDate())) {
if ($newRef = $this->updateToCommit($target, $path, $ref, $target->getPrettyVersion(), $target->getReleaseDate())) {
if ($target->getDistReference() === $target->getSourceReference()) {
$target->setDistReference($newRef);
}
Expand Down Expand Up @@ -434,12 +434,12 @@ protected function reapplyChanges($path)
*
* @param string $path
* @param string $reference
* @param string $branch
* @param string $prettyVersion
* @param \DateTime $date
* @throws \RuntimeException
* @return null|string if a string is returned, it is the commit reference that was checked out if the original could not be found
*/
protected function updateToCommit($path, $reference, $branch, $date)
protected function updateToCommit(PackageInterface $package, $path, $reference, $prettyVersion, $date)
{
$force = !empty($this->hasDiscardedChanges[$path]) || !empty($this->hasStashedChanges[$path]) ? '-f ' : '';

Expand All @@ -449,7 +449,7 @@ protected function updateToCommit($path, $reference, $branch, $date)
// If the non-existent branch is actually the name of a file, the file
// is checked out.
$template = 'git checkout '.$force.'%s -- && git reset --hard %1$s --';
$branch = Preg::replace('{(?:^dev-|(?:\.x)?-dev$)}i', '', $branch);
$branch = Preg::replace('{(?:^dev-|(?:\.x)?-dev$)}i', '', $prettyVersion);

$branches = null;
if (0 === $this->process->execute('git branch -r', $output, $path)) {
Expand Down Expand Up @@ -489,12 +489,15 @@ protected function updateToCommit($path, $reference, $branch, $date)
return null;
}

$exceptionExtra = '';

// reference was not found (prints "fatal: reference is not a tree: $ref")
if (false !== strpos($this->process->getErrorOutput(), $reference)) {
$this->io->writeError(' <warning>'.$reference.' is gone (history was rewritten?)</warning>');
$exceptionExtra = "\nIt looks like the commit hash is not available in the repository, maybe ".($package->isDev() ? 'the commit was removed from the branch' : 'the tag was recreated').'? Run "composer update '.$package->getPrettyName().'" to resolve this.';
}

throw new \RuntimeException(Url::sanitize('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()));
throw new \RuntimeException(Url::sanitize('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput() . $exceptionExtra));
}

/**
Expand Down

0 comments on commit e7c04e3

Please sign in to comment.