From db1e4b7574cb82f60d91219bf8c20e0ec4e902ae Mon Sep 17 00:00:00 2001 From: Tim Roberson Date: Mon, 24 Mar 2014 09:01:35 -0400 Subject: [PATCH 1/2] Add auth for private, non-GitHub repos (#2826) --- src/Composer/Repository/Vcs/GitDriver.php | 29 +++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php index ecfad77db..fd1859259 100644 --- a/src/Composer/Repository/Vcs/GitDriver.php +++ b/src/Composer/Repository/Vcs/GitDriver.php @@ -71,9 +71,34 @@ class GitDriver extends VcsDriver if (0 !== $this->process->execute('git --version', $ignoredOutput)) { throw new \RuntimeException('Failed to clone '.$this->url.', git was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()); + } elseif ( + $this->io->isInteractive() && + preg_match('{(https?://)([^/]+)(.*)$}i', $this->url, $match) && + strpos($output, 'fatal: Authentication failed') !== false + ) { + if ($this->io->hasAuthentication($match[2])) { + $auth = $this->io->getAuthentication($match[2]); + } else { + $this->io->write($this->url.' requires Authentication'); + $auth = array( + 'username' => $this->io->ask('Username: '), + 'password' => $this->io->askAndHideAnswer('Password: '), + ); + } + + $url = $match[1].urlencode($auth['username']).':'.urlencode($auth['password']).'@'.$match[2].$match[3]; + + $command = sprintf('git clone --mirror %s %s', escapeshellarg($url), escapeshellarg($this->repoDir)); + + if (0 === $this->process->execute($command, $output)) { + $this->io->setAuthentication($match[2], $auth['username'], $auth['password']); + } else { + $output = $this->process->getErrorOutput(); + throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output); + } + } else { + throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output); } - - throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output); } } } From 1f7014888458eb34fd5de3b26809967882dac13a Mon Sep 17 00:00:00 2001 From: Tim Roberson Date: Mon, 24 Mar 2014 11:03:44 -0400 Subject: [PATCH 2/2] Change elseif to if. --- src/Composer/Repository/Vcs/GitDriver.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php index fd1859259..06b860227 100644 --- a/src/Composer/Repository/Vcs/GitDriver.php +++ b/src/Composer/Repository/Vcs/GitDriver.php @@ -71,7 +71,9 @@ class GitDriver extends VcsDriver if (0 !== $this->process->execute('git --version', $ignoredOutput)) { throw new \RuntimeException('Failed to clone '.$this->url.', git was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()); - } elseif ( + } + + if ( $this->io->isInteractive() && preg_match('{(https?://)([^/]+)(.*)$}i', $this->url, $match) && strpos($output, 'fatal: Authentication failed') !== false