2011-04-17 22:14:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Downloader;
|
|
|
|
|
|
|
|
use Composer\Package\PackageInterface;
|
2012-01-18 07:56:35 +00:00
|
|
|
use Composer\Util\ProcessExecutor;
|
2011-04-17 22:14:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*/
|
2012-01-22 20:14:56 +00:00
|
|
|
class GitDownloader extends VcsDownloader
|
2011-04-17 22:14:44 +00:00
|
|
|
{
|
2011-09-25 10:39:08 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-22 20:14:56 +00:00
|
|
|
public function doDownload(PackageInterface $package, $path)
|
2011-09-25 10:39:08 +00:00
|
|
|
{
|
2011-09-28 22:48:17 +00:00
|
|
|
$ref = escapeshellarg($package->getSourceReference());
|
2012-03-06 23:58:37 +00:00
|
|
|
$command = 'git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s';
|
2012-01-22 20:14:56 +00:00
|
|
|
$this->io->write(" Cloning ".$package->getSourceReference());
|
2012-03-06 23:58:37 +00:00
|
|
|
|
2012-03-10 16:49:08 +00:00
|
|
|
$commandCallable = function($url) use ($ref, $path, $command) {
|
|
|
|
return sprintf($command, $url, escapeshellarg($path), $ref);
|
|
|
|
};
|
|
|
|
|
|
|
|
$this->runCommand($commandCallable, $package->getSourceUrl(), $path);
|
2011-09-28 22:48:17 +00:00
|
|
|
}
|
2011-09-25 10:39:08 +00:00
|
|
|
|
2011-09-28 22:48:17 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-22 20:14:56 +00:00
|
|
|
public function doUpdate(PackageInterface $initial, PackageInterface $target, $path)
|
2011-04-17 22:14:44 +00:00
|
|
|
{
|
2012-01-22 20:14:56 +00:00
|
|
|
$ref = escapeshellarg($target->getSourceReference());
|
|
|
|
$path = escapeshellarg($path);
|
|
|
|
$this->io->write(" Checking out ".$target->getSourceReference());
|
2012-03-10 16:49:08 +00:00
|
|
|
$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());
|
2011-04-17 22:14:44 +00:00
|
|
|
}
|
|
|
|
|
2011-09-25 10:39:08 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2012-01-22 20:14:56 +00:00
|
|
|
protected function enforceCleanDirectory($path)
|
2011-04-17 22:14:44 +00:00
|
|
|
{
|
2012-02-20 23:02:34 +00:00
|
|
|
$command = sprintf('cd %s && git status --porcelain', escapeshellarg($path));
|
|
|
|
if (0 !== $this->process->execute($command, $output)) {
|
2012-02-29 11:05:25 +00:00
|
|
|
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
2012-02-20 23:02:34 +00:00
|
|
|
}
|
|
|
|
|
2012-01-22 19:08:20 +00:00
|
|
|
if (trim($output)) {
|
2012-03-06 17:21:45 +00:00
|
|
|
throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes');
|
2011-09-28 22:48:17 +00:00
|
|
|
}
|
2011-04-17 22:14:44 +00:00
|
|
|
}
|
2012-03-10 16:49:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
}
|
|
|
|
}
|
2011-09-17 13:12:45 +00:00
|
|
|
}
|