StatusCommand.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Composer\Downloader\ChangeReportInterface;
  16. use Composer\Downloader\DvcsDownloaderInterface;
  17. use Composer\Downloader\VcsCapableDownloaderInterface;
  18. use Composer\Package\Dumper\ArrayDumper;
  19. use Composer\Package\Version\VersionGuesser;
  20. use Composer\Package\Version\VersionParser;
  21. use Composer\Plugin\CommandEvent;
  22. use Composer\Plugin\PluginEvents;
  23. use Composer\Script\ScriptEvents;
  24. use Composer\Util\ProcessExecutor;
  25. /**
  26. * @author Tiago Ribeiro <tiago.ribeiro@seegno.com>
  27. * @author Rui Marinho <rui.marinho@seegno.com>
  28. */
  29. class StatusCommand extends BaseCommand
  30. {
  31. const EXIT_CODE_ERRORS = 1;
  32. const EXIT_CODE_UNPUSHED_CHANGES = 2;
  33. const EXIT_CODE_VERSION_CHANGES = 4;
  34. protected function configure()
  35. {
  36. $this
  37. ->setName('status')
  38. ->setDescription('Shows a list of locally modified packages.')
  39. ->setDefinition(array(
  40. new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Show modified files for each directory that contains changes.'),
  41. ))
  42. ->setHelp(<<<EOT
  43. The status command displays a list of dependencies that have
  44. been modified locally.
  45. EOT
  46. )
  47. ;
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. // init repos
  52. $composer = $this->getComposer();
  53. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'status', $input, $output);
  54. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  55. $installedRepo = $composer->getRepositoryManager()->getLocalRepository();
  56. $dm = $composer->getDownloadManager();
  57. $im = $composer->getInstallationManager();
  58. // Dispatch pre-status-command
  59. $composer->getEventDispatcher()->dispatchScript(ScriptEvents::PRE_STATUS_CMD, true);
  60. $errors = array();
  61. $io = $this->getIO();
  62. $unpushedChanges = array();
  63. $vcsVersionChanges = array();
  64. $parser = new VersionParser;
  65. $guesser = new VersionGuesser($composer->getConfig(), new ProcessExecutor($io), $parser);
  66. $dumper = new ArrayDumper;
  67. // list packages
  68. foreach ($installedRepo->getCanonicalPackages() as $package) {
  69. $downloader = $dm->getDownloaderForInstalledPackage($package);
  70. $targetDir = $im->getInstallPath($package);
  71. if ($downloader instanceof ChangeReportInterface) {
  72. if (is_link($targetDir)) {
  73. $errors[$targetDir] = $targetDir . ' is a symbolic link.';
  74. }
  75. if ($changes = $downloader->getLocalChanges($package, $targetDir)) {
  76. $errors[$targetDir] = $changes;
  77. }
  78. }
  79. if ($downloader instanceof VcsCapableDownloaderInterface) {
  80. if ($currentRef = $downloader->getVcsReference($package, $targetDir)) {
  81. switch ($package->getInstallationSource()) {
  82. case 'source':
  83. $previousRef = $package->getSourceReference();
  84. break;
  85. case 'dist':
  86. $previousRef = $package->getDistReference();
  87. break;
  88. default:
  89. $previousRef = null;
  90. }
  91. $currentVersion = $guesser->guessVersion($dumper->dump($package), $targetDir);
  92. if ($previousRef && $currentVersion && $currentVersion['commit'] !== $previousRef) {
  93. $vcsVersionChanges[$targetDir] = array(
  94. 'previous' => array(
  95. 'version' => $package->getPrettyVersion(),
  96. 'ref' => $previousRef,
  97. ),
  98. 'current' => array(
  99. 'version' => $currentVersion['pretty_version'],
  100. 'ref' => $currentVersion['commit'],
  101. ),
  102. );
  103. }
  104. }
  105. }
  106. if ($downloader instanceof DvcsDownloaderInterface) {
  107. if ($unpushed = $downloader->getUnpushedChanges($package, $targetDir)) {
  108. $unpushedChanges[$targetDir] = $unpushed;
  109. }
  110. }
  111. }
  112. // output errors/warnings
  113. if (!$errors && !$unpushedChanges && !$vcsVersionChanges) {
  114. $io->writeError('<info>No local changes</info>');
  115. return 0;
  116. }
  117. if ($errors) {
  118. $io->writeError('<error>You have changes in the following dependencies:</error>');
  119. foreach ($errors as $path => $changes) {
  120. if ($input->getOption('verbose')) {
  121. $indentedChanges = implode("\n", array_map(function ($line) {
  122. return ' ' . ltrim($line);
  123. }, explode("\n", $changes)));
  124. $io->write('<info>'.$path.'</info>:');
  125. $io->write($indentedChanges);
  126. } else {
  127. $io->write($path);
  128. }
  129. }
  130. }
  131. if ($unpushedChanges) {
  132. $io->writeError('<warning>You have unpushed changes on the current branch in the following dependencies:</warning>');
  133. foreach ($unpushedChanges as $path => $changes) {
  134. if ($input->getOption('verbose')) {
  135. $indentedChanges = implode("\n", array_map(function ($line) {
  136. return ' ' . ltrim($line);
  137. }, explode("\n", $changes)));
  138. $io->write('<info>'.$path.'</info>:');
  139. $io->write($indentedChanges);
  140. } else {
  141. $io->write($path);
  142. }
  143. }
  144. }
  145. if ($vcsVersionChanges) {
  146. $io->writeError('<warning>You have version variations in the following dependencies:</warning>');
  147. foreach ($vcsVersionChanges as $path => $changes) {
  148. if ($input->getOption('verbose')) {
  149. // If we don't can't find a version, use the ref instead.
  150. $currentVersion = $changes['current']['version'] ?: $changes['current']['ref'];
  151. $previousVersion = $changes['previous']['version'] ?: $changes['previous']['ref'];
  152. if ($io->isVeryVerbose()) {
  153. // Output the ref regardless of whether or not it's being used as the version
  154. $currentVersion .= sprintf(' (%s)', $changes['current']['ref']);
  155. $previousVersion .= sprintf(' (%s)', $changes['previous']['ref']);
  156. }
  157. $io->write('<info>'.$path.'</info>:');
  158. $io->write(sprintf(' From <comment>%s</comment> to <comment>%s</comment>', $previousVersion, $currentVersion));
  159. } else {
  160. $io->write($path);
  161. }
  162. }
  163. }
  164. if (($errors || $unpushedChanges || $vcsVersionChanges) && !$input->getOption('verbose')) {
  165. $io->writeError('Use --verbose (-v) to see a list of files');
  166. }
  167. // Dispatch post-status-command
  168. $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_STATUS_CMD, true);
  169. return ($errors ? self::EXIT_CODE_ERRORS : 0) + ($unpushedChanges ? self::EXIT_CODE_UNPUSHED_CHANGES : 0) + ($vcsVersionChanges ? self::EXIT_CODE_VERSION_CHANGES : 0);
  170. }
  171. }