StatusCommand.php 8.2 KB

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