2011-11-17 15:47:58 +00:00
|
|
|
<?php
|
|
|
|
|
2012-01-18 07:56:35 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-11-17 15:47:58 +00:00
|
|
|
namespace Composer\Downloader;
|
|
|
|
|
|
|
|
use Composer\Package\PackageInterface;
|
2012-03-22 16:19:10 +00:00
|
|
|
use Composer\Util\Svn as SvnUtil;
|
2016-01-09 18:01:48 +00:00
|
|
|
use Composer\Repository\VcsRepository;
|
2018-01-12 09:42:46 +00:00
|
|
|
use Composer\Util\ProcessExecutor;
|
2011-11-17 15:47:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Ben Bieker <mail@ben-bieker.de>
|
2012-03-23 15:03:52 +00:00
|
|
|
* @author Till Klampaeckel <till@php.net>
|
2011-11-17 15:47:58 +00:00
|
|
|
*/
|
2012-01-22 20:14:56 +00:00
|
|
|
class SvnDownloader extends VcsDownloader
|
2011-11-17 15:47:58 +00:00
|
|
|
{
|
2015-12-14 12:23:15 +00:00
|
|
|
protected $cacheCredentials = true;
|
|
|
|
|
2011-11-17 15:47:58 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2019-08-29 09:37:23 +00:00
|
|
|
protected function doDownload(PackageInterface $package, $path, $url, PackageInterface $prevPackage = null)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function doInstall(PackageInterface $package, $path, $url)
|
2011-11-17 15:47:58 +00:00
|
|
|
{
|
2014-02-19 09:55:00 +00:00
|
|
|
SvnUtil::cleanEnv();
|
|
|
|
$ref = $package->getSourceReference();
|
2012-03-22 16:19:10 +00:00
|
|
|
|
2016-01-09 18:01:48 +00:00
|
|
|
$repo = $package->getRepository();
|
|
|
|
if ($repo instanceof VcsRepository) {
|
|
|
|
$repoConfig = $repo->getRepoConfig();
|
|
|
|
if (array_key_exists('svn-cache-credentials', $repoConfig)) {
|
|
|
|
$this->cacheCredentials = (bool) $repoConfig['svn-cache-credentials'];
|
|
|
|
}
|
2015-12-14 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 22:41:46 +00:00
|
|
|
$this->io->writeError(" Checking out ".$package->getSourceReference());
|
2020-02-04 12:22:46 +00:00
|
|
|
$this->execute($package, $url, "svn co", sprintf("%s/%s", $url, $ref), null, $path);
|
2011-11-17 15:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2019-08-29 09:37:23 +00:00
|
|
|
protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path, $url)
|
2011-11-17 15:47:58 +00:00
|
|
|
{
|
2014-02-19 09:55:00 +00:00
|
|
|
SvnUtil::cleanEnv();
|
2012-03-22 16:19:10 +00:00
|
|
|
$ref = $target->getSourceReference();
|
|
|
|
|
2016-02-04 01:11:18 +00:00
|
|
|
if (!$this->hasMetadataRepository($path)) {
|
2016-02-04 21:01:21 +00:00
|
|
|
throw new \RuntimeException('The .svn directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information');
|
2013-07-26 10:25:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 14:56:10 +00:00
|
|
|
$util = new SvnUtil($url, $this->io, $this->config);
|
2014-03-29 13:16:13 +00:00
|
|
|
$flags = "";
|
2018-03-02 14:56:10 +00:00
|
|
|
if (version_compare($util->binaryVersion(), '1.7.0', '>=')) {
|
|
|
|
$flags .= ' --ignore-ancestry';
|
2014-03-28 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 22:41:46 +00:00
|
|
|
$this->io->writeError(" Checking out " . $ref);
|
2020-02-04 12:22:46 +00:00
|
|
|
$this->execute($target, $url, "svn switch" . $flags, sprintf("%s/%s", $url, $ref), $path);
|
2011-11-17 15:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2013-07-31 21:27:46 +00:00
|
|
|
public function getLocalChanges(PackageInterface $package, $path)
|
2012-01-22 19:08:45 +00:00
|
|
|
{
|
2016-02-04 01:11:18 +00:00
|
|
|
if (!$this->hasMetadataRepository($path)) {
|
2016-04-06 22:52:50 +00:00
|
|
|
return null;
|
2012-11-20 13:32:57 +00:00
|
|
|
}
|
|
|
|
|
2012-06-04 09:21:17 +00:00
|
|
|
$this->process->execute('svn status --ignore-externals', $output, $path);
|
2012-06-25 23:18:10 +00:00
|
|
|
|
2012-08-18 12:34:24 +00:00
|
|
|
return preg_match('{^ *[^X ] +}m', $output) ? $output : null;
|
2012-01-22 19:08:45 +00:00
|
|
|
}
|
2012-03-22 16:19:10 +00:00
|
|
|
|
2012-03-22 17:41:10 +00:00
|
|
|
/**
|
2012-03-24 23:29:14 +00:00
|
|
|
* Execute an SVN command and try to fix up the process with credentials
|
|
|
|
* if necessary.
|
2012-03-22 17:41:10 +00:00
|
|
|
*
|
2013-06-13 11:28:24 +00:00
|
|
|
* @param string $baseUrl Base URL of the repository
|
|
|
|
* @param string $command SVN command to run
|
|
|
|
* @param string $url SVN url
|
|
|
|
* @param string $cwd Working directory
|
|
|
|
* @param string $path Target for a checkout
|
2013-06-13 00:05:44 +00:00
|
|
|
* @throws \RuntimeException
|
2012-03-22 17:41:10 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-02-04 12:22:46 +00:00
|
|
|
protected function execute(PackageInterface $package, $baseUrl, $command, $url, $cwd = null, $path = null)
|
2012-03-22 17:41:10 +00:00
|
|
|
{
|
2014-05-30 15:14:43 +00:00
|
|
|
$util = new SvnUtil($baseUrl, $this->io, $this->config);
|
2015-12-14 12:23:15 +00:00
|
|
|
$util->setCacheCredentials($this->cacheCredentials);
|
2012-03-24 23:29:14 +00:00
|
|
|
try {
|
2012-04-04 07:36:04 +00:00
|
|
|
return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose());
|
2012-03-24 23:29:14 +00:00
|
|
|
} catch (\RuntimeException $e) {
|
|
|
|
throw new \RuntimeException(
|
2020-02-04 12:06:20 +00:00
|
|
|
$package->getPrettyName().' could not be downloaded, '.$e->getMessage()
|
2012-03-24 23:29:14 +00:00
|
|
|
);
|
2012-03-22 17:41:10 +00:00
|
|
|
}
|
2012-03-22 16:19:10 +00:00
|
|
|
}
|
2012-06-25 13:23:54 +00:00
|
|
|
|
2012-10-07 13:19:55 +00:00
|
|
|
/**
|
2012-10-10 11:46:49 +00:00
|
|
|
* {@inheritDoc}
|
2012-10-07 13:19:55 +00:00
|
|
|
*/
|
2013-07-31 21:27:46 +00:00
|
|
|
protected function cleanChanges(PackageInterface $package, $path, $update)
|
2012-10-07 13:19:55 +00:00
|
|
|
{
|
2013-07-31 21:27:46 +00:00
|
|
|
if (!$changes = $this->getLocalChanges($package, $path)) {
|
2013-02-28 18:44:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-07 13:19:55 +00:00
|
|
|
if (!$this->io->isInteractive()) {
|
2013-03-01 22:47:24 +00:00
|
|
|
if (true === $this->config->get('discard-changes')) {
|
|
|
|
return $this->discardChanges($path);
|
2013-02-28 16:10:04 +00:00
|
|
|
}
|
2013-03-01 22:47:24 +00:00
|
|
|
|
2013-07-31 21:27:46 +00:00
|
|
|
return parent::cleanChanges($package, $path, $update);
|
2012-10-07 13:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$changes = array_map(function ($elem) {
|
|
|
|
return ' '.$elem;
|
|
|
|
}, preg_split('{\s*\r?\n\s*}', $changes));
|
2018-01-02 03:24:27 +00:00
|
|
|
$countChanges = count($changes);
|
2020-02-04 12:06:20 +00:00
|
|
|
$this->io->writeError(sprintf(' <error>'.$package->getPrettyName().' has modified file%s:</error>', $countChanges === 1 ? '' : 's'));
|
2015-02-06 12:52:44 +00:00
|
|
|
$this->io->writeError(array_slice($changes, 0, 10));
|
2018-01-02 03:24:27 +00:00
|
|
|
if ($countChanges > 10) {
|
2019-12-29 17:14:38 +00:00
|
|
|
$remainingChanges = $countChanges - 10;
|
2018-01-02 03:24:27 +00:00
|
|
|
$this->io->writeError(
|
|
|
|
sprintf(
|
2019-12-29 17:14:38 +00:00
|
|
|
' <info>'.$remainingChanges.' more file%s modified, choose "v" to view the full list</info>',
|
|
|
|
$remainingChanges === 1 ? '' : 's'
|
2018-01-02 03:24:27 +00:00
|
|
|
)
|
|
|
|
);
|
2012-10-07 13:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
switch ($this->io->ask(' <info>Discard changes [y,n,v,?]?</info> ', '?')) {
|
|
|
|
case 'y':
|
2013-02-28 16:10:04 +00:00
|
|
|
$this->discardChanges($path);
|
2012-10-07 13:19:55 +00:00
|
|
|
break 2;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
throw new \RuntimeException('Update aborted');
|
|
|
|
|
|
|
|
case 'v':
|
2015-02-06 12:52:44 +00:00
|
|
|
$this->io->writeError($changes);
|
2012-10-07 13:19:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
2015-02-06 12:52:44 +00:00
|
|
|
$this->io->writeError(array(
|
2012-10-07 13:19:55 +00:00
|
|
|
' 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',
|
|
|
|
' ? - print help',
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-18 14:01:44 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function getCommitLogs($fromReference, $toReference, $path)
|
2012-06-25 13:23:54 +00:00
|
|
|
{
|
2020-05-03 17:50:53 +00:00
|
|
|
if (preg_match('{@(\d+)$}', $fromReference) && preg_match('{@(\d+)$}', $toReference)) {
|
2018-01-12 09:42:46 +00:00
|
|
|
// retrieve the svn base url from the checkout folder
|
2018-01-15 08:35:49 +00:00
|
|
|
$command = sprintf('svn info --non-interactive --xml %s', ProcessExecutor::escape($path));
|
2018-01-12 09:42:46 +00:00
|
|
|
if (0 !== $this->process->execute($command, $output, $path)) {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()
|
|
|
|
);
|
|
|
|
}
|
2018-01-15 08:35:49 +00:00
|
|
|
|
|
|
|
$urlPattern = '#<url>(.*)</url>#';
|
|
|
|
if (preg_match($urlPattern, $output, $matches)) {
|
|
|
|
$baseUrl = $matches[1];
|
|
|
|
} else {
|
|
|
|
throw new \RuntimeException(
|
|
|
|
'Unable to determine svn url for path '. $path
|
|
|
|
);
|
|
|
|
}
|
2018-01-12 09:42:46 +00:00
|
|
|
|
2014-02-20 15:26:34 +00:00
|
|
|
// strip paths from references and only keep the actual revision
|
|
|
|
$fromRevision = preg_replace('{.*@(\d+)$}', '$1', $fromReference);
|
|
|
|
$toRevision = preg_replace('{.*@(\d+)$}', '$1', $toReference);
|
2012-12-05 15:55:26 +00:00
|
|
|
|
2018-04-12 12:20:34 +00:00
|
|
|
$command = sprintf('svn log -r%s:%s --incremental', ProcessExecutor::escape($fromRevision), ProcessExecutor::escape($toRevision));
|
2012-06-25 13:23:54 +00:00
|
|
|
|
2018-01-12 09:42:46 +00:00
|
|
|
$util = new SvnUtil($baseUrl, $this->io, $this->config);
|
|
|
|
$util->setCacheCredentials($this->cacheCredentials);
|
|
|
|
try {
|
|
|
|
return $util->executeLocal($command, $path, null, $this->io->isVerbose());
|
|
|
|
} catch (\RuntimeException $e) {
|
2014-02-20 15:26:34 +00:00
|
|
|
throw new \RuntimeException(
|
2018-01-12 09:42:46 +00:00
|
|
|
'Failed to execute ' . $command . "\n\n".$e->getMessage()
|
2014-02-20 15:26:34 +00:00
|
|
|
);
|
|
|
|
}
|
2012-06-25 13:23:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 09:42:46 +00:00
|
|
|
return "Could not retrieve changes between $fromReference and $toReference due to missing revision information";
|
2012-06-25 13:23:54 +00:00
|
|
|
}
|
2013-02-28 16:10:04 +00:00
|
|
|
|
|
|
|
protected function discardChanges($path)
|
|
|
|
{
|
|
|
|
if (0 !== $this->process->execute('svn revert -R .', $output, $path)) {
|
|
|
|
throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput());
|
|
|
|
}
|
|
|
|
}
|
2016-02-04 01:11:18 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-13 17:43:57 +00:00
|
|
|
* {@inheritDoc}
|
2016-02-04 01:11:18 +00:00
|
|
|
*/
|
|
|
|
protected function hasMetadataRepository($path)
|
|
|
|
{
|
|
|
|
return is_dir($path.'/.svn');
|
|
|
|
}
|
2012-03-22 16:19:10 +00:00
|
|
|
}
|