StatusCommand.php 8.1 KB

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