From 271e9086924bbf1a0aec4be3ec166f2d039bf373 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Sat, 10 Mar 2012 17:49:08 +0100 Subject: [PATCH] Fixed the GitDownloader when changing the repo url --- src/Composer/Downloader/GitDownloader.php | 66 +++++++++++++++-------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 0aa043c31..cceb4cf8b 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -29,24 +29,11 @@ class GitDownloader extends VcsDownloader $command = 'git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s'; $this->io->write(" Cloning ".$package->getSourceReference()); - // github, autoswitch protocols - if (preg_match('{^(?:https?|git)(://github.com/.*)}', $package->getSourceUrl(), $match)) { - $protocols = array('git', 'https', 'http'); - foreach ($protocols as $protocol) { - $url = escapeshellarg($protocol . $match[1]); - if (0 === $this->process->execute(sprintf($command, $url, escapeshellarg($path), $ref), $ignoredOutput)) { - return; - } - $this->filesystem->removeDirectory($path); - } - throw new \RuntimeException('Failed to checkout ' . $url .' via git, https and http protocols, aborting.' . "\n\n" . $this->process->getErrorOutput()); - } else { - $url = escapeshellarg($package->getSourceUrl()); - $command = sprintf($command, $url, escapeshellarg($path), $ref); - if (0 !== $this->process->execute($command, $ignoredOutput)) { - throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); - } - } + $commandCallable = function($url) use ($ref, $path, $command) { + return sprintf($command, $url, escapeshellarg($path), $ref); + }; + + $this->runCommand($commandCallable, $package->getSourceUrl(), $path); } /** @@ -57,10 +44,13 @@ class GitDownloader extends VcsDownloader $ref = escapeshellarg($target->getSourceReference()); $path = escapeshellarg($path); $this->io->write(" Checking out ".$target->getSourceReference()); - $command = sprintf('cd %s && git fetch && git checkout %2$s && git reset --hard %2$s', $path, $ref); - if (0 !== $this->process->execute($command, $ignoredOutput)) { - throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); - } + $command = 'cd %s && git remote set-url origin %s && git fetch && git checkout %3$s && git reset --hard %3$s'; + + $commandCallable = function($url) use ($ref, $path, $command) { + return sprintf($command, $path, $url, $ref); + }; + + $this->runCommand($commandCallable, $target->getSourceUrl()); } /** @@ -77,4 +67,36 @@ class GitDownloader extends VcsDownloader throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes'); } } + + /** + * Runs a command doing attempts for each protocol supported by github. + * + * @param callable $commandCallable A callable building the command for the given url + * @param string $url + * @param string $path The directory to remove for each attempt (null if not needed) + * @throws \RuntimeException + */ + protected function runCommand($commandCallable, $url, $path = null) + { + // github, autoswitch protocols + if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) { + $protocols = array('git', 'https', 'http'); + foreach ($protocols as $protocol) { + $url = escapeshellarg($protocol . $match[1]); + if (0 === $this->process->execute(call_user_func($commandCallable, $url), $ignoredOutput)) { + return; + } + if (null !== $path) { + $this->filesystem->removeDirectory($path); + } + } + throw new \RuntimeException('Failed to checkout ' . $url .' via git, https and http protocols, aborting.' . "\n\n" . $this->process->getErrorOutput()); + } + + $url = escapeshellarg($url); + $command = call_user_func($commandCallable, $url); + if (0 !== $this->process->execute($command, $ignoredOutput)) { + throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); + } + } }