Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --prefer-lowest and --prefer-stable to update command #3450

Merged
merged 3 commits into from
Dec 14, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Composer/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ protected function configure()
new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'),
new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump.'),
new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'),
new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies.'),
new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies.'),
))
->setHelp(<<<EOT
The <info>update</info> command reads the composer.json file from the
Expand Down Expand Up @@ -121,6 +123,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
->setUpdateWhitelist($input->getOption('lock') ? array('lock') : $input->getArgument('packages'))
->setWhitelistDependencies($input->getOption('with-dependencies'))
->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'))
->setPreferStable($input->getOption('prefer-stable'))
->setPreferLowest($input->getOption('prefer-lowest'))
;

if ($input->getOption('no-plugins')) {
Expand Down
7 changes: 5 additions & 2 deletions src/Composer/DependencyResolver/DefaultPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
class DefaultPolicy implements PolicyInterface
{
private $preferStable;
private $preferLowest;

public function __construct($preferStable = false)
public function __construct($preferStable = false, $preferLowest = false)
{
$this->preferStable = $preferStable;
$this->preferLowest = $preferLowest;
}

public function versionCompare(PackageInterface $a, PackageInterface $b, $operator)
Expand Down Expand Up @@ -195,6 +197,7 @@ protected function replaces(PackageInterface $source, PackageInterface $target)

protected function pruneToBestVersion(Pool $pool, $literals)
{
$operator = $this->preferLowest ? '<' : '>';
$bestLiterals = array($literals[0]);
$bestPackage = $pool->literalToPackage($literals[0]);
foreach ($literals as $i => $literal) {
Expand All @@ -204,7 +207,7 @@ protected function pruneToBestVersion(Pool $pool, $literals)

$package = $pool->literalToPackage($literal);

if ($this->versionCompare($package, $bestPackage, '>')) {
if ($this->versionCompare($package, $bestPackage, $operator)) {
$bestPackage = $package;
$bestLiterals = array($literal);
} elseif ($this->versionCompare($package, $bestPackage, '==')) {
Expand Down
34 changes: 32 additions & 2 deletions src/Composer/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class Installer
protected $update = false;
protected $runScripts = true;
protected $ignorePlatformReqs = false;
protected $preferStable = false;
protected $preferLowest = false;
/**
* Array of package names/globs flagged for update
*
Expand Down Expand Up @@ -693,17 +695,19 @@ private function createPool($withDevReqs)

private function createPolicy()
{
$preferLowest = false;
$preferStable = null;
if (!$this->update && $this->locker->isLocked()) {
$preferStable = $this->locker->getPreferStable();
}
// old lock file without prefer stable will return null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding prefer-lowest support for the lock file? I understand it's probably not a common use case or none at all but for completeness and to avoid bad surprises it should be persisted to the lock. See a few lines above..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, not sure to understand... Can you drive me a bit more please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, basically the same as https://github.com/composer/composer/pull/3101/files + nicolas-grekas@4bd748b which fixed some mistakes :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except preferLowest isn't on the root $package so you'll have to pass it in to the locker directly I guess.

// so in this case we use the composer.json info
if (null === $preferStable) {
$preferStable = $this->package->getPreferStable();
$preferStable = $this->preferStable || $this->package->getPreferStable();
$preferLowest = $this->preferLowest;
}

return new DefaultPolicy($preferStable);
return new DefaultPolicy($preferStable, $preferLowest);
}

private function createRequest(Pool $pool, RootPackageInterface $rootPackage, PlatformRepository $platformRepo)
Expand Down Expand Up @@ -1244,6 +1248,32 @@ public function setWhitelistDependencies($updateDependencies = true)
return $this;
}

/**
* Should packages be prefered in a stable version when updating?
*
* @param boolean $preferStable
* @return Installer
*/
public function setPreferStable($preferStable = true)
{
$this->preferStable = (boolean) $preferStable;

return $this;
}

/**
* Should packages be prefered in a lowest version when updating?
*
* @param boolean $preferLowest
* @return Installer
*/
public function setPreferLowest($preferLowest = true)
{
$this->preferLowest = (boolean) $preferLowest;

return $this;
}

/**
* Disables plugins.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Composer/Test/DependencyResolver/DefaultPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,20 @@ protected function mapFromRepo(RepositoryInterface $repo)

return $map;
}

public function testSelectLowest()
{
$policy = new DefaultPolicy(false, true);

$this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0'));
$this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
$this->pool->addRepository($this->repo);

$literals = array($packageA1->getId(), $packageA2->getId());
$expected = array($packageA1->getId());

$selected = $policy->selectPreferedPackages($this->pool, array(), $literals);

$this->assertEquals($expected, $selected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Updates packages to their lowest stable version
--COMPOSER--
{
"repositories": [
{
"type": "package",
"package": [
{ "name": "a/a", "version": "1.0.0-rc1" },
{ "name": "a/a", "version": "1.0.1" },
{ "name": "a/a", "version": "1.1.0" },

{ "name": "a/b", "version": "1.0.0" },
{ "name": "a/b", "version": "1.0.1" },
{ "name": "a/b", "version": "2.0.0" },

{ "name": "a/c", "version": "1.0.0" },
{ "name": "a/c", "version": "2.0.0" }
]
}
],
"require": {
"a/a": "~1.0@dev",
"a/c": "2.*"
},
"require-dev": {
"a/b": "*"
}
}
--INSTALLED--
[
{ "name": "a/a", "version": "1.0.0-rc1" },
{ "name": "a/c", "version": "2.0.0" },
{ "name": "a/b", "version": "1.0.1" }
]
--RUN--
update --prefer-lowest --prefer-stable
--EXPECT--
Updating a/a (1.0.0-rc1) to a/a (1.0.1)
Updating a/b (1.0.1) to a/b (1.0.0)
2 changes: 2 additions & 0 deletions tests/Composer/Test/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ public function testIntegration($file, $message, $condition, $composerConfig, $l
->setDryRun($input->getOption('dry-run'))
->setUpdateWhitelist($input->getArgument('packages'))
->setWhitelistDependencies($input->getOption('with-dependencies'))
->setPreferStable($input->getOption('prefer-stable'))
->setPreferLowest($input->getOption('prefer-lowest'))
->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'));

return $installer->run();
Expand Down