1
0
Fork 0

Warn/throw when we detect git safe.directory errors (#12178)

Fixes #12158
Fixes #12160
pull/12180/head^2
Jordi Boggiano 2024-10-28 21:37:38 +01:00 committed by GitHub
parent 1f0d012845
commit e0ed22bbd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 32 additions and 7 deletions

View File

@ -92,7 +92,7 @@ EOT
$vcsVersionChanges = [];
$parser = new VersionParser;
$guesser = new VersionGuesser($composer->getConfig(), $composer->getLoop()->getProcessExecutor() ?? new ProcessExecutor($io), $parser);
$guesser = new VersionGuesser($composer->getConfig(), $composer->getLoop()->getProcessExecutor() ?? new ProcessExecutor($io), $parser, $io);
$dumper = new ArrayDumper;
// list packages

View File

@ -224,7 +224,7 @@ class PathDownloader extends FileDownloader implements VcsCapableDownloaderInter
{
$path = Filesystem::trimTrailingSlash($path);
$parser = new VersionParser;
$guesser = new VersionGuesser($this->config, $this->process, $parser);
$guesser = new VersionGuesser($this->config, $this->process, $parser, $this->io);
$dumper = new ArrayDumper;
$packageConfig = $dumper->dump($package);

View File

@ -241,7 +241,7 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa
public function getVcsReference(PackageInterface $package, string $path): ?string
{
$parser = new VersionParser;
$guesser = new VersionGuesser($this->config, $this->process, $parser);
$guesser = new VersionGuesser($this->config, $this->process, $parser, $this->io);
$dumper = new ArrayDumper;
$packageConfig = $dumper->dump($package);

View File

@ -384,7 +384,7 @@ class Factory
// load package
$parser = new VersionParser;
$guesser = new VersionGuesser($config, $process, $parser);
$guesser = new VersionGuesser($config, $process, $parser, $io);
$loader = $this->loadRootPackage($rm, $config, $parser, $guesser, $io);
$package = $loader->load($localConfig, 'Composer\Package\RootPackage', $cwd);
$composer->setPackage($package);

View File

@ -60,7 +60,7 @@ class RootPackageLoader extends ArrayLoader
$this->manager = $manager;
$this->config = $config;
$this->versionGuesser = $versionGuesser ?: new VersionGuesser($config, new ProcessExecutor($io), $this->versionParser);
$this->versionGuesser = $versionGuesser ?: new VersionGuesser($config, new ProcessExecutor($io), $this->versionParser, $io);
$this->io = $io;
}

View File

@ -13,6 +13,7 @@
namespace Composer\Package\Version;
use Composer\Config;
use Composer\IO\IOInterface;
use Composer\Pcre\Preg;
use Composer\Repository\Vcs\HgDriver;
use Composer\IO\NullIO;
@ -50,11 +51,17 @@ class VersionGuesser
*/
private $versionParser;
public function __construct(Config $config, ProcessExecutor $process, SemverVersionParser $versionParser)
/**
* @var IOInterface|null
*/
private $io;
public function __construct(Config $config, ProcessExecutor $process, SemverVersionParser $versionParser, ?IOInterface $io = null)
{
$this->config = $config;
$this->process = $process;
$this->versionParser = $versionParser;
$this->io = $io;
}
/**
@ -178,6 +185,7 @@ class VersionGuesser
$prettyVersion = $result['pretty_version'];
}
}
GitUtil::checkForRepoOwnershipError($this->process->getErrorOutput(), $path, $this->io);
if (!$version || $isDetached) {
$result = $this->versionFromGitTags($path);

View File

@ -116,7 +116,7 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn
$this->loader = new ArrayLoader(null, true);
$this->url = Platform::expandPath($repoConfig['url']);
$this->process = $process ?? new ProcessExecutor($io);
$this->versionGuesser = new VersionGuesser($config, $this->process, new VersionParser());
$this->versionGuesser = new VersionGuesser($config, $this->process, new VersionParser(), $io);
$this->repoConfig = $repoConfig;
$this->options = $repoConfig['options'] ?? [];
if (!isset($this->options['relative'])) {

View File

@ -236,6 +236,7 @@ class GitDriver extends VcsDriver
if ($process->execute('git tag', $output, $url) === 0) {
return true;
}
GitUtil::checkForRepoOwnershipError($process->getErrorOutput(), $url);
}
if (!$deep) {

View File

@ -43,6 +43,20 @@ class Git
$this->filesystem = $fs;
}
/**
* @param IOInterface|null $io If present, a warning is output there instead of throwing, so pass this in only for cases where this is a soft failure
*/
public static function checkForRepoOwnershipError(string $output, string $path, ?IOInterface $io = null): void
{
if (str_contains($output, 'fatal: detected dubious ownership')) {
$msg = 'The repository at "' . $path . '" does not have the correct ownership and git refuses to use it:' . PHP_EOL . PHP_EOL . $output;
if ($io === null) {
throw new \RuntimeException($msg);
}
$io->writeError('<warning>'.$msg.'</warning>');
}
}
public function setHttpDownloader(HttpDownloader $httpDownloader): void
{
$this->httpDownloader = $httpDownloader;
@ -317,6 +331,7 @@ class Git
return true;
}
self::checkForRepoOwnershipError($this->process->getErrorOutput(), $dir);
// clean up directory and do a fresh clone into it
$this->filesystem->removeDirectory($dir);
@ -384,6 +399,7 @@ class Git
return true;
}
}
self::checkForRepoOwnershipError($this->process->getErrorOutput(), $dir);
return false;
}