1
0
Fork 0

Merge remote-tracking branch 'slbmeh/feature/version-status'

pull/5490/head
Jordi Boggiano 2016-07-02 15:38:22 +01:00
commit 44ddcf4da7
5 changed files with 169 additions and 22 deletions

View File

@ -16,10 +16,15 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Downloader\ChangeReportInterface;
use Composer\Downloader\DvcsDownloaderInterface;
use Composer\Downloader\VcsCapableDownloaderInterface;
use Composer\Package\Dumper\ArrayDumper;
use Composer\Package\Version\VersionGuesser;
use Composer\Package\Version\VersionParser;
use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;
use Composer\Script\ScriptEvents;
use Composer\Downloader\DvcsDownloaderInterface;
use Composer\Util\ProcessExecutor;
/**
* @author Tiago Ribeiro <tiago.ribeiro@seegno.com>
@ -27,6 +32,11 @@ use Composer\Downloader\DvcsDownloaderInterface;
*/
class StatusCommand extends BaseCommand
{
const EXIT_CODE_ERRORS = 1;
const EXIT_CODE_UNPUSHED_CHANGES = 2;
const EXIT_CODE_VERSION_CHANGES = 4;
protected function configure()
{
$this
@ -63,14 +73,18 @@ EOT
$errors = array();
$io = $this->getIO();
$unpushedChanges = array();
$vcsVersionChanges = array();
$parser = new VersionParser;
$guesser = new VersionGuesser($composer->getConfig(), new ProcessExecutor($io), $parser);
$dumper = new ArrayDumper;
// list packages
foreach ($installedRepo->getCanonicalPackages() as $package) {
$downloader = $dm->getDownloaderForInstalledPackage($package);
$targetDir = $im->getInstallPath($package);
if ($downloader instanceof ChangeReportInterface) {
$targetDir = $im->getInstallPath($package);
if (is_link($targetDir)) {
$errors[$targetDir] = $targetDir . ' is a symbolic link.';
}
@ -78,31 +92,64 @@ EOT
if ($changes = $downloader->getLocalChanges($package, $targetDir, true)) {
$errors[$targetDir] = $changes;
}
}
if ($downloader instanceof DvcsDownloaderInterface) {
if ($unpushed = $downloader->getUnpushedChanges($package, $targetDir)) {
$unpushedChanges[$targetDir] = $unpushed;
if ($downloader instanceof VcsCapableDownloaderInterface) {
if ($currentRef = $downloader->getVcsReference($package, $targetDir)) {
switch ($package->getInstallationSource()) {
case 'source':
$previousRef = $package->getSourceReference();
break;
case 'dist':
$previousRef = $package->getDistReference();
break;
default:
$previousRef = null;
}
$currentVersion = $guesser->guessVersion($dumper->dump($package), $targetDir);
if ($previousRef && $currentVersion['commit'] !== $previousRef) {
$vcsVersionChanges[$targetDir] = array(
'previous' => array(
'version' => $package->getPrettyVersion(),
'ref' => $previousRef
),
'current' => array(
'version' => $currentVersion['version'],
'ref' => $currentVersion['commit'],
),
);
}
}
}
if ($downloader instanceof DvcsDownloaderInterface) {
if ($unpushed = $downloader->getUnpushedChanges($package, $targetDir)) {
$unpushedChanges[$targetDir] = $unpushed;
}
}
}
// output errors/warnings
if (!$errors && !$unpushedChanges) {
if (!$errors && !$unpushedChanges && !$vcsVersionChanges) {
$io->writeError('<info>No local changes</info>');
} elseif ($errors) {
$io->writeError('<error>You have changes in the following dependencies:</error>');
return 0;
}
foreach ($errors as $path => $changes) {
if ($input->getOption('verbose')) {
$indentedChanges = implode("\n", array_map(function ($line) {
return ' ' . ltrim($line);
}, explode("\n", $changes)));
$io->write('<info>'.$path.'</info>:');
$io->write($indentedChanges);
} else {
$io->write($path);
if ($errors) {
$io->writeError('<error>You have changes in the following dependencies:</error>');
foreach ($errors as $path => $changes) {
if ($input->getOption('verbose')) {
$indentedChanges = implode("\n", array_map(function ($line) {
return ' ' . ltrim($line);
}, explode("\n", $changes)));
$io->write('<info>'.$path.'</info>:');
$io->write($indentedChanges);
} else {
$io->write($path);
}
}
}
@ -122,13 +169,36 @@ EOT
}
}
if (($errors || $unpushedChanges) && !$input->getOption('verbose')) {
if ($vcsVersionChanges) {
$io->writeError('<warning>You have version variations in the following dependencies:</warning>');
foreach ($vcsVersionChanges as $path => $changes) {
if ($input->getOption('verbose')) {
// If we don't can't find a version, use the ref instead.
$currentVersion = $changes['current']['version'] ?: $changes['current']['ref'];
$previousVersion = $changes['previous']['version'] ?: $changes['previous']['ref'];
if ($io->isVeryVerbose()) {
// Output the ref regardless of whether or not it's being used as the version
$currentVersion .= sprintf(' (%s)', $changes['current']['ref']);
$previousVersion .= sprintf(' (%s)', $changes['previous']['ref']);
}
$io->write('<info>'.$path.'</info>:');
$io->write(sprintf(' From <comment>%s</comment> to <comment>%s</comment>', $previousVersion, $currentVersion));
} else {
$io->write($path);
}
}
}
if (($errors || $unpushedChanges || $vcsVersionChanges) && !$input->getOption('verbose')) {
$io->writeError('Use --verbose (-v) to see a list of files');
}
// Dispatch post-status-command
$composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_STATUS_CMD, true);
return ($errors ? 1 : 0) + ($unpushedChanges ? 2 : 0);
return ($errors ? self::EXIT_CODE_ERRORS : 0) + ($unpushedChanges ? self::EXIT_CODE_UNPUSHED_CHANGES : 0) + ($vcsVersionChanges ? self::EXIT_CODE_VERSION_CHANGES : 0);
}
}

View File

@ -12,8 +12,12 @@
namespace Composer\Downloader;
use Composer\Package\Dumper\ArrayDumper;
use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionGuesser;
use Composer\Package\Version\VersionParser;
use Composer\Util\Platform;
use Composer\Util\ProcessExecutor;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
@ -23,7 +27,7 @@ use Symfony\Component\Filesystem\Filesystem;
* @author Samuel Roze <samuel.roze@gmail.com>
* @author Johann Reinke <johann.reinke@gmail.com>
*/
class PathDownloader extends FileDownloader
class PathDownloader extends FileDownloader implements VcsCapableDownloaderInterface
{
const STRATEGY_SYMLINK = 10;
const STRATEGY_MIRROR = 20;
@ -127,4 +131,19 @@ class PathDownloader extends FileDownloader
parent::remove($package, $path);
}
}
/**
* {@inheritDoc}
*/
public function getVcsReference(PackageInterface $package, $path)
{
$parser = new VersionParser;
$guesser = new VersionGuesser($this->config, new ProcessExecutor($this->io), $parser);
$dumper = new ArrayDumper;
$packageConfig = $dumper->dump($package);
if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
return $packageVersion['commit'];
}
}
}

View File

@ -0,0 +1,33 @@
<?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;
/**
* VCS Capable Downloader interface.
*
* @author Steve Buzonas <steve@fancyguy.com>
*/
interface VcsCapableDownloaderInterface
{
/**
* Gets the VCS Reference for the package at path
*
* @param PackageInterface $package package directory
* @param string $path package directory
* @return string|null reference or null
*/
public function getVcsReference(PackageInterface $package, $path);
}

View File

@ -13,7 +13,10 @@
namespace Composer\Downloader;
use Composer\Config;
use Composer\Package\Dumper\ArrayDumper;
use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionGuesser;
use Composer\Package\Version\VersionParser;
use Composer\Util\ProcessExecutor;
use Composer\IO\IOInterface;
use Composer\Util\Filesystem;
@ -21,7 +24,7 @@ use Composer\Util\Filesystem;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterface
abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterface, VcsCapableDownloaderInterface
{
/** @var IOInterface */
protected $io;
@ -193,6 +196,21 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa
return $this;
}
/**
* {@inheritDoc}
*/
public function getVcsReference(PackageInterface $package, $path)
{
$parser = new VersionParser;
$guesser = new VersionGuesser($this->config, $this->process, $parser);
$dumper = new ArrayDumper;
$packageConfig = $dumper->dump($package);
if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) {
return $packageVersion['commit'];
}
}
/**
* Prompt the user to check if changes should be stashed/removed or the operation aborted
*

View File

@ -130,6 +130,13 @@ class VersionGuesser
$version = $this->versionFromGitTags($path);
}
if (!$commit) {
$command = 'git log --pretty="%H" -n1 HEAD';
if (0 === $this->process->execute($command, $output, $path)) {
$commit = trim($output) ?: null;
}
}
return array('version' => $version, 'commit' => $commit);
}