Skip to content

Commit

Permalink
Allow for SSH URLs when using hg repository type (#11878)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaxweb committed Mar 20, 2024
1 parent 75ccf65 commit a6947f1
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Composer/Util/Hg.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ public function runCommand(callable $commandCallable, string $url, ?string $cwd)
}

// Try with the authentication information available
if (Preg::isMatch('{^(https?)://((.+)(?:\:(.+))?@)?([^/]+)(/.*)?}mi', $url, $match) && $this->io->hasAuthentication((string) $match[5])) {
$auth = $this->io->getAuthentication((string) $match[5]);
$authenticatedUrl = $match[1] . '://' . rawurlencode($auth['username']) . ':' . rawurlencode($auth['password']) . '@' . $match[5] . $match[6];

if (
Preg::isMatch('{^(?P<proto>ssh|https?)://(?:(?P<user>[^:@]+)(?::(?P<pass>[^:@]+))?@)?(?P<host>[^/]+)(?P<path>/.*)?}mi', $url, $matches)
&& $this->io->hasAuthentication((string) $matches['host'])
) {
if ($matches['proto'] === 'ssh') {
$user = '';
if ($matches['user'] !== '' && $matches['user'] !== null) {
$user = rawurlencode($matches['user']) . '@';
}
$authenticatedUrl = $matches['proto'] . '://' . $user . $matches['host'] . $matches['path'];
} else {
$auth = $this->io->getAuthentication((string) $matches['host']);
$authenticatedUrl = $matches['proto'] . '://' . rawurlencode($auth['username']) . ':' . rawurlencode($auth['password']) . '@' . $matches['host'] . $matches['path'];
}
$command = $commandCallable($authenticatedUrl);

if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) {
Expand All @@ -70,10 +80,10 @@ public function runCommand(callable $commandCallable, string $url, ?string $cwd)

$error = $this->process->getErrorOutput();
} else {
$error = 'The given URL (' . $url . ') does not match the required format (http(s)://(username:password@)example.com/path-to-repository)';
$error = 'The given URL (' .$url. ') does not match the required format (ssh|http(s)://(username:password@)example.com/path-to-repository)';
}

$this->throwException('Failed to clone ' . $url . ', ' . "\n\n" . $error, $url);
$this->throwException("Failed to clone $url, \n\n" . $error, $url);
}

/**
Expand All @@ -84,7 +94,9 @@ public function runCommand(callable $commandCallable, string $url, ?string $cwd)
private function throwException($message, string $url): void
{
if (null === self::getVersion($this->process)) {
throw new \RuntimeException(Url::sanitize('Failed to clone ' . $url . ', hg was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()));
throw new \RuntimeException(Url::sanitize(
'Failed to clone ' . $url . ', hg was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()
));
}

throw new \RuntimeException(Url::sanitize($message));
Expand Down

0 comments on commit a6947f1

Please sign in to comment.