cleaner implementation of status output with support for path repositories

main
Steve Buzonas 8 years ago
parent 124852f15d
commit 8d766c8eb2

@ -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,6 +73,11 @@ 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) {
@ -78,31 +93,64 @@ EOT
if ($changes = $downloader->getLocalChanges($package, $targetDir, true)) {
$errors[$targetDir] = $changes;
}
}
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;
}
if ($downloader instanceof DvcsDownloaderInterface) {
if ($unpushed = $downloader->getUnpushedChanges($package, $targetDir)) {
$unpushedChanges[$targetDir] = $unpushed;
$currentVersion = $guesser->guessVersion($dumper->dump($package), $targetDir);
if ($previousRef && $currentVersion['commit'] !== $previousRef) {
$vcsVersionChanges[$targetDir] = array(
'previous' => array(
'version' => $package->getVersion(),
'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 +170,34 @@ 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')) {
$currentVersion = $changes['current']['version'];
$previousVersion = $changes['previous']['version'];
if ($io->isVeryVerbose()) {
$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);
}
}

@ -23,7 +23,7 @@ use Composer\Config;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface, VcsCapableDownloaderInterface
{
private $hasStashedChanges = false;
private $hasDiscardedChanges = false;
@ -196,6 +196,25 @@ class GitDownloader extends VcsDownloader implements DvcsDownloaderInterface
return $unpushedChanges;
}
/**
* {@inheritDoc}
*/
public function getVcsReference(PackageInterface $package, $path)
{
if (!$this->hasMetadataRepository($path)) {
return;
}
GitUtil::cleanEnv();
$command = 'git log --pretty="%H" -n1 HEAD';
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}
*/

@ -77,6 +77,23 @@ class HgDownloader extends VcsDownloader
return trim($output) ?: null;
}
/**
* {@inheritDoc}
*/
public function getVcsReference(PackageInterface $package, $path)
{
if (!$this->hasMetadataRepository($path)) {
return;
}
$command = 'hg parent --template ' . escapeshellarg('{node}');
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}
*/

@ -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'];
}
}
}

@ -81,6 +81,24 @@ class SvnDownloader extends VcsDownloader
return preg_match('{^ *[^X ] +}m', $output) ? $output : null;
}
/**
* {@inheritDoc}
*/
public function getVcsReference(PackageInterface $package, $path)
{
if (!$this->hasMetadataRepository($path)) {
return;
}
SvnUtil::cleanEnv();
if (0 !== $this->process->execute('svnversion', $output, $path)) {
throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput());
}
return trim($output) ?: null;
}
/**
* Execute an SVN command and try to fix up the process with credentials
* if necessary.

@ -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);
}

@ -125,6 +125,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);
}

Loading…
Cancel
Save