Skip to content

Commit

Permalink
--patch-only option for show and outdated commands (#10589)
Browse files Browse the repository at this point in the history
Fixes #10503

Co-authored-by: Svyatoslav <tregubov.s@asteq.ru>
Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
  • Loading branch information
3 people committed Mar 15, 2022
1 parent f125fc1 commit f863855
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Composer/Command/OutdatedCommand.php
Expand Up @@ -39,6 +39,7 @@ protected function configure(): void
new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'),
new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code when there are outdated packages'),
new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates. Use with the --outdated option.'),
new InputOption('patch-only', 'p', InputOption::VALUE_NONE, 'Show only packages that have patch SemVer-compatible updates. Use with the --outdated option.'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'),
new InputOption('ignore', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore specified package(s). Use it with the --outdated option if you don\'t want to be informed about new versions of some packages.'),
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables search in require-dev packages.'),
Expand Down Expand Up @@ -84,6 +85,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($input->getOption('minor-only')) {
$args['--minor-only'] = true;
}
if ($input->getOption('patch-only')) {
$args['--patch-only'] = true;
}
if ($input->getOption('locked')) {
$args['--locked'] = true;
}
Expand Down
23 changes: 20 additions & 3 deletions src/Composer/Command/ShowCommand.php
Expand Up @@ -89,6 +89,7 @@ protected function configure()
new InputOption('outdated', 'o', InputOption::VALUE_NONE, 'Show the latest version but only for packages that are outdated'),
new InputOption('ignore', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore specified package(s). Use it with the --outdated option if you don\'t want to be informed about new versions of some packages.'),
new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates. Use with the --outdated option.'),
new InputOption('patch-only', null, InputOption::VALUE_NONE, 'Show only packages that have patch SemVer-compatible updates. Use with the --outdated option.'),
new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'),
new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code when there are outdated packages'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'),
Expand Down Expand Up @@ -139,6 +140,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

if ($input->getOption('patch-only') && $input->getOption('minor-only')) {
$io->writeError('The --patch-only option is not usable in combination with --minor-only');

return 1;
}

if ($input->getOption('tree') && $input->getOption('latest')) {
$io->writeError('The --tree (-t) option is not usable in combination with --latest (-l)');

Expand Down Expand Up @@ -289,7 +296,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$latestPackage = null;
if ($input->getOption('latest')) {
$latestPackage = $this->findLatestPackage($package, $composer, $platformRepo, $input->getOption('minor-only'), $platformReqFilter);
$latestPackage = $this->findLatestPackage($package, $composer, $platformRepo, $input->getOption('minor-only'), $input->getOption('patch-only'), $platformReqFilter);
}
if (
$input->getOption('outdated')
Expand Down Expand Up @@ -398,6 +405,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$showAllTypes = $input->getOption('all');
$showLatest = $input->getOption('latest');
$showMinorOnly = $input->getOption('minor-only');
$showPatchOnly = $input->getOption('patch-only');
$ignoredPackages = array_map('strtolower', $input->getOption('ignore'));
$indent = $showAllTypes ? ' ' : '';
/** @var PackageInterface[] $latestPackages */
Expand All @@ -414,7 +422,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($showLatest && $showVersion) {
foreach ($packages[$type] as $package) {
if (is_object($package)) {
$latestPackage = $this->findLatestPackage($package, $composer, $platformRepo, $showMinorOnly, $platformReqFilter);
$latestPackage = $this->findLatestPackage($package, $composer, $platformRepo, $showMinorOnly, $showPatchOnly, $platformReqFilter);
if ($latestPackage === null) {
continue;
}
Expand Down Expand Up @@ -1288,7 +1296,7 @@ private function writeTreeLine(string $line): void
/**
* Given a package, this finds the latest package matching it
*/
private function findLatestPackage(PackageInterface $package, Composer $composer, PlatformRepository $platformRepo, bool $minorOnly, PlatformRequirementFilterInterface $platformReqFilter): ?PackageInterface
private function findLatestPackage(PackageInterface $package, Composer $composer, PlatformRepository $platformRepo, bool $minorOnly, bool $patchOnly, PlatformRequirementFilterInterface $platformReqFilter): ?PackageInterface
{
// find the latest version allowed in this repo set
$name = $package->getName();
Expand All @@ -1313,6 +1321,15 @@ private function findLatestPackage(PackageInterface $package, Composer $composer
$targetVersion = '^' . $package->getVersion();
}

if ($targetVersion === null && $patchOnly) {
$trimmedVersion = Preg::replace('{(\.0)+$}D', '', $package->getVersion());
$partsNeeded = substr($trimmedVersion, 0, 1) === '0' ? 4 : 3;
while (substr_count($trimmedVersion, '.') + 1 < $partsNeeded) {
$trimmedVersion .= '.0';
}
$targetVersion = '~' . $trimmedVersion;
}

$candidate = $versionSelector->findBestCandidate($name, $targetVersion, $bestStability, $platformReqFilter);
while ($candidate instanceof AliasPackage) {
$candidate = $candidate->getAliasOf();
Expand Down

0 comments on commit f863855

Please sign in to comment.