380 lines
14 KiB
PHP
380 lines
14 KiB
PHP
<?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;
|
|
use Composer\Util\GitHub;
|
|
use Composer\Util\Git as GitUtil;
|
|
use Composer\Util\ProcessExecutor;
|
|
use Composer\IO\IOInterface;
|
|
use Composer\Util\Filesystem;
|
|
use Composer\Config;
|
|
|
|
/**
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
*/
|
|
class GitDownloader extends VcsDownloader
|
|
{
|
|
private $hasStashedChanges = false;
|
|
private $gitUtil;
|
|
|
|
public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null)
|
|
{
|
|
parent::__construct($io, $config, $process, $fs);
|
|
$this->gitUtil = new GitUtil($this->io, $this->config, $this->process, $this->filesystem);
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function doDownload(PackageInterface $package, $path, $url)
|
|
{
|
|
GitUtil::cleanEnv();
|
|
$path = $this->normalizePath($path);
|
|
|
|
$ref = $package->getSourceReference();
|
|
$flag = defined('PHP_WINDOWS_VERSION_MAJOR') ? '/D ' : '';
|
|
$command = 'git clone --no-checkout %s %s && cd '.$flag.'%2$s && git remote add composer %1$s && git fetch composer';
|
|
$this->io->write(" Cloning ".$ref);
|
|
|
|
$commandCallable = function ($url) use ($ref, $path, $command) {
|
|
return sprintf(
|
|
$command,
|
|
ProcessExecutor::escape($url),
|
|
ProcessExecutor::escape($path),
|
|
ProcessExecutor::escape($ref));
|
|
};
|
|
|
|
$this->gitUtil->runCommand($commandCallable, $url, $path, true);
|
|
$this->setPushUrl($path, $url);
|
|
|
|
if ($newRef = $this->updateToCommit($path, $ref, $package->getPrettyVersion(), $package->getReleaseDate())) {
|
|
if ($package->getDistReference() === $package->getSourceReference()) {
|
|
$package->setDistReference($newRef);
|
|
}
|
|
$package->setSourceReference($newRef);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
|
|
{
|
|
GitUtil::cleanEnv();
|
|
$path = $this->normalizePath($path);
|
|
if (!is_dir($path.'/.git')) {
|
|
throw new \RuntimeException('The .git directory is missing from '.$path.', see http://getcomposer.org/commit-deps for more information');
|
|
}
|
|
|
|
$ref = $target->getSourceReference();
|
|
$this->io->write(" Checking out ".$ref);
|
|
$command = 'git remote set-url composer %s && git fetch composer && git fetch --tags composer';
|
|
|
|
$commandCallable = function ($url) use ($command) {
|
|
return sprintf($command, ProcessExecutor::escape ($url));
|
|
};
|
|
|
|
$this->gitUtil->runCommand($commandCallable, $url, $path);
|
|
if ($newRef = $this->updateToCommit($path, $ref, $target->getPrettyVersion(), $target->getReleaseDate())) {
|
|
if ($target->getDistReference() === $target->getSourceReference()) {
|
|
$target->setDistReference($newRef);
|
|
}
|
|
$target->setSourceReference($newRef);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function getLocalChanges(PackageInterface $package, $path)
|
|
{
|
|
GitUtil::cleanEnv();
|
|
$path = $this->normalizePath($path);
|
|
if (!is_dir($path.'/.git')) {
|
|
return;
|
|
}
|
|
|
|
$command = 'git status --porcelain --untracked-files=no';
|
|
if (0 !== $this->process->execute($command, $output, $path)) {
|
|
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
|
}
|
|
|
|
return trim($output) ?: null;
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
protected function cleanChanges(PackageInterface $package, $path, $update)
|
|
{
|
|
GitUtil::cleanEnv();
|
|
$path = $this->normalizePath($path);
|
|
if (!$changes = $this->getLocalChanges($package, $path)) {
|
|
return;
|
|
}
|
|
|
|
if (!$this->io->isInteractive()) {
|
|
$discardChanges = $this->config->get('discard-changes');
|
|
if (true === $discardChanges) {
|
|
return $this->discardChanges($path);
|
|
}
|
|
if ('stash' === $discardChanges) {
|
|
if (!$update) {
|
|
return parent::cleanChanges($package, $path, $update);
|
|
}
|
|
|
|
return $this->stashChanges($path);
|
|
}
|
|
|
|
return parent::cleanChanges($package, $path, $update);
|
|
}
|
|
|
|
$changes = array_map(function ($elem) {
|
|
return ' '.$elem;
|
|
}, preg_split('{\s*\r?\n\s*}', $changes));
|
|
$this->io->write(' <error>The package has modified files:</error>');
|
|
$this->io->write(array_slice($changes, 0, 10));
|
|
if (count($changes) > 10) {
|
|
$this->io->write(' <info>'.count($changes) - 10 . ' more files modified, choose "v" to view the full list</info>');
|
|
}
|
|
|
|
while (true) {
|
|
switch ($this->io->ask(' <info>Discard changes [y,n,v,'.($update ? 's,' : '').'?]?</info> ', '?')) {
|
|
case 'y':
|
|
$this->discardChanges($path);
|
|
break 2;
|
|
|
|
case 's':
|
|
if (!$update) {
|
|
goto help;
|
|
}
|
|
|
|
$this->stashChanges($path);
|
|
break 2;
|
|
|
|
case 'n':
|
|
throw new \RuntimeException('Update aborted');
|
|
|
|
case 'v':
|
|
$this->io->write($changes);
|
|
break;
|
|
|
|
case '?':
|
|
default:
|
|
help:
|
|
$this->io->write(array(
|
|
' y - discard changes and apply the '.($update ? 'update' : 'uninstall'),
|
|
' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up',
|
|
' v - view modified files',
|
|
));
|
|
if ($update) {
|
|
$this->io->write(' s - stash changes and try to reapply them after the update');
|
|
}
|
|
$this->io->write(' ? - print help');
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
protected function reapplyChanges($path)
|
|
{
|
|
$path = $this->normalizePath($path);
|
|
if ($this->hasStashedChanges) {
|
|
$this->hasStashedChanges = false;
|
|
$this->io->write(' <info>Re-applying stashed changes');
|
|
if (0 !== $this->process->execute('git stash pop', $output, $path)) {
|
|
throw new \RuntimeException("Failed to apply stashed changes:\n\n".$this->process->getErrorOutput());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates the given path to the given commit ref
|
|
*
|
|
* @param string $path
|
|
* @param string $reference
|
|
* @param string $branch
|
|
* @param \DateTime $date
|
|
* @return null|string if a string is returned, it is the commit reference that was checked out if the original could not be found
|
|
*
|
|
* @throws \RuntimeException
|
|
*/
|
|
protected function updateToCommit($path, $reference, $branch, $date)
|
|
{
|
|
$template = 'git checkout %s && git reset --hard %1$s';
|
|
$branch = preg_replace('{(?:^dev-|(?:\.x)?-dev$)}i', '', $branch);
|
|
|
|
$branches = null;
|
|
if (0 === $this->process->execute('git branch -r', $output, $path)) {
|
|
$branches = $output;
|
|
}
|
|
|
|
// check whether non-commitish are branches or tags, and fetch branches with the remote name
|
|
$gitRef = $reference;
|
|
if (!preg_match('{^[a-f0-9]{40}$}', $reference)
|
|
&& $branches
|
|
&& preg_match('{^\s+composer/'.preg_quote($reference).'$}m', $branches)
|
|
) {
|
|
$command = sprintf('git checkout -B %s %s && git reset --hard %2$s', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$reference));
|
|
if (0 === $this->process->execute($command, $output, $path)) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// try to checkout branch by name and then reset it so it's on the proper branch name
|
|
if (preg_match('{^[a-f0-9]{40}$}', $reference)) {
|
|
// add 'v' in front of the branch if it was stripped when generating the pretty name
|
|
if (!preg_match('{^\s+composer/'.preg_quote($branch).'$}m', $branches) && preg_match('{^\s+composer/v'.preg_quote($branch).'$}m', $branches)) {
|
|
$branch = 'v' . $branch;
|
|
}
|
|
|
|
$command = sprintf('git checkout %s', ProcessExecutor::escape($branch));
|
|
$fallbackCommand = sprintf('git checkout -B %s %s', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$branch));
|
|
if (0 === $this->process->execute($command, $output, $path)
|
|
|| 0 === $this->process->execute($fallbackCommand, $output, $path)
|
|
) {
|
|
$command = sprintf('git reset --hard %s', ProcessExecutor::escape($reference));
|
|
if (0 === $this->process->execute($command, $output, $path)) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
$command = sprintf($template, ProcessExecutor::escape($gitRef));
|
|
if (0 === $this->process->execute($command, $output, $path)) {
|
|
return;
|
|
}
|
|
|
|
// reference was not found (prints "fatal: reference is not a tree: $ref")
|
|
if ($date && false !== strpos($this->process->getErrorOutput(), $reference)) {
|
|
$date = $date->format('U');
|
|
|
|
// guess which remote branch to look at first
|
|
$command = 'git branch -r';
|
|
if (0 !== $this->process->execute($command, $output, $path)) {
|
|
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
|
}
|
|
|
|
$guessTemplate = 'git log --until=%s --date=raw -n1 --pretty=%%H %s';
|
|
foreach ($this->process->splitLines($output) as $line) {
|
|
if (preg_match('{^composer/'.preg_quote($branch).'(?:\.x)?$}i', trim($line))) {
|
|
// find the previous commit by date in the given branch
|
|
if (0 === $this->process->execute(sprintf($guessTemplate, $date, ProcessExecutor::escape(trim($line))), $output, $path)) {
|
|
$newReference = trim($output);
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (empty($newReference)) {
|
|
// no matching branch found, find the previous commit by date in all commits
|
|
if (0 !== $this->process->execute(sprintf($guessTemplate, $date, '--all'), $output, $path)) {
|
|
throw new \RuntimeException('Failed to execute ' . GitUtil::sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput());
|
|
}
|
|
$newReference = trim($output);
|
|
}
|
|
|
|
// checkout the new recovered ref
|
|
$command = sprintf($template, ProcessExecutor::escape($newReference));
|
|
if (0 === $this->process->execute($command, $output, $path)) {
|
|
$this->io->write(' '.$reference.' is gone (history was rewritten?), recovered by checking out '.$newReference);
|
|
|
|
return $newReference;
|
|
}
|
|
}
|
|
|
|
throw new \RuntimeException('Failed to execute ' . GitUtil::sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput());
|
|
}
|
|
|
|
protected function setPushUrl($path, $url)
|
|
{
|
|
// set push url for github projects
|
|
if (preg_match('{^(?:https?|git)://'.GitUtil::getGitHubDomainsRegex($this->config).'/([^/]+)/([^/]+?)(?:\.git)?$}', $url, $match)) {
|
|
$protocols = $this->config->get('github-protocols');
|
|
$pushUrl = 'git@'.$match[1].':'.$match[2].'/'.$match[3].'.git';
|
|
if ($protocols[0] !== 'git') {
|
|
$pushUrl = 'https://' . $match[1] . '/'.$match[2].'/'.$match[3].'.git';
|
|
}
|
|
$cmd = sprintf('git remote set-url --push origin %s', ProcessExecutor::escape($pushUrl));
|
|
$this->process->execute($cmd, $ignoredOutput, $path);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
protected function getCommitLogs($fromReference, $toReference, $path)
|
|
{
|
|
$path = $this->normalizePath($path);
|
|
$command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"', $fromReference, $toReference);
|
|
|
|
if (0 !== $this->process->execute($command, $output, $path)) {
|
|
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
|
|
/**
|
|
* @param $path
|
|
* @throws \RuntimeException
|
|
*/
|
|
protected function discardChanges($path)
|
|
{
|
|
$path = $this->normalizePath($path);
|
|
if (0 !== $this->process->execute('git reset --hard', $output, $path)) {
|
|
throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $path
|
|
* @throws \RuntimeException
|
|
*/
|
|
protected function stashChanges($path)
|
|
{
|
|
$path = $this->normalizePath($path);
|
|
if (0 !== $this->process->execute('git stash', $output, $path)) {
|
|
throw new \RuntimeException("Could not stash changes\n\n:".$this->process->getErrorOutput());
|
|
}
|
|
|
|
$this->hasStashedChanges = true;
|
|
}
|
|
|
|
protected function normalizePath($path)
|
|
{
|
|
if (defined('PHP_WINDOWS_VERSION_MAJOR') && strlen($path) > 0) {
|
|
$basePath = $path;
|
|
$removed = array();
|
|
|
|
while (!is_dir($basePath) && $basePath !== '\\') {
|
|
array_unshift($removed, basename($basePath));
|
|
$basePath = dirname($basePath);
|
|
}
|
|
|
|
if ($basePath === '\\') {
|
|
return $path;
|
|
}
|
|
|
|
$path = rtrim(realpath($basePath) . '/' . implode('/', $removed), '/');
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
}
|