StatusCommand.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. $composer = $this->getComposer();
  62. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'status', $input, $output);
  63. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  64. // Dispatch pre-status-command
  65. $composer->getEventDispatcher()->dispatchScript(ScriptEvents::PRE_STATUS_CMD, true);
  66. $exitCode = $this->doExecute($input, $output);
  67. // Dispatch post-status-command
  68. $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_STATUS_CMD, true);
  69. return $exitCode;
  70. }
  71. /**
  72. * @param InputInterface $input
  73. * @param OutputInterface $output
  74. * @return int
  75. */
  76. private function doExecute(InputInterface $input, OutputInterface $output)
  77. {
  78. // init repos
  79. $composer = $this->getComposer();
  80. $installedRepo = $composer->getRepositoryManager()->getLocalRepository();
  81. $dm = $composer->getDownloadManager();
  82. $im = $composer->getInstallationManager();
  83. $errors = array();
  84. $io = $this->getIO();
  85. $unpushedChanges = array();
  86. $vcsVersionChanges = array();
  87. $parser = new VersionParser;
  88. $guesser = new VersionGuesser($composer->getConfig(), new ProcessExecutor($io), $parser);
  89. $dumper = new ArrayDumper;
  90. // list packages
  91. foreach ($installedRepo->getCanonicalPackages() as $package) {
  92. $downloader = $dm->getDownloaderForInstalledPackage($package);
  93. $targetDir = $im->getInstallPath($package);
  94. if ($downloader instanceof ChangeReportInterface) {
  95. if (is_link($targetDir)) {
  96. $errors[$targetDir] = $targetDir . ' is a symbolic link.';
  97. }
  98. if ($changes = $downloader->getLocalChanges($package, $targetDir)) {
  99. $errors[$targetDir] = $changes;
  100. }
  101. }
  102. if ($downloader instanceof VcsCapableDownloaderInterface) {
  103. if ($currentRef = $downloader->getVcsReference($package, $targetDir)) {
  104. switch ($package->getInstallationSource()) {
  105. case 'source':
  106. $previousRef = $package->getSourceReference();
  107. break;
  108. case 'dist':
  109. $previousRef = $package->getDistReference();
  110. break;
  111. default:
  112. $previousRef = null;
  113. }
  114. $currentVersion = $guesser->guessVersion($dumper->dump($package), $targetDir);
  115. if ($previousRef && $currentVersion && $currentVersion['commit'] !== $previousRef) {
  116. $vcsVersionChanges[$targetDir] = array(
  117. 'previous' => array(
  118. 'version' => $package->getPrettyVersion(),
  119. 'ref' => $previousRef,
  120. ),
  121. 'current' => array(
  122. 'version' => $currentVersion['pretty_version'],
  123. 'ref' => $currentVersion['commit'],
  124. ),
  125. );
  126. }
  127. }
  128. }
  129. if ($downloader instanceof DvcsDownloaderInterface) {
  130. if ($unpushed = $downloader->getUnpushedChanges($package, $targetDir)) {
  131. $unpushedChanges[$targetDir] = $unpushed;
  132. }
  133. }
  134. }
  135. // output errors/warnings
  136. if (!$errors && !$unpushedChanges && !$vcsVersionChanges) {
  137. $io->writeError('<info>No local changes</info>');
  138. return 0;
  139. }
  140. if ($errors) {
  141. $io->writeError('<error>You have changes in the following dependencies:</error>');
  142. foreach ($errors as $path => $changes) {
  143. if ($input->getOption('verbose')) {
  144. $indentedChanges = implode("\n", array_map(function ($line) {
  145. return ' ' . ltrim($line);
  146. }, explode("\n", $changes)));
  147. $io->write('<info>'.$path.'</info>:');
  148. $io->write($indentedChanges);
  149. } else {
  150. $io->write($path);
  151. }
  152. }
  153. }
  154. if ($unpushedChanges) {
  155. $io->writeError('<warning>You have unpushed changes on the current branch in the following dependencies:</warning>');
  156. foreach ($unpushedChanges as $path => $changes) {
  157. if ($input->getOption('verbose')) {
  158. $indentedChanges = implode("\n", array_map(function ($line) {
  159. return ' ' . ltrim($line);
  160. }, explode("\n", $changes)));
  161. $io->write('<info>'.$path.'</info>:');
  162. $io->write($indentedChanges);
  163. } else {
  164. $io->write($path);
  165. }
  166. }
  167. }
  168. if ($vcsVersionChanges) {
  169. $io->writeError('<warning>You have version variations in the following dependencies:</warning>');
  170. foreach ($vcsVersionChanges as $path => $changes) {
  171. if ($input->getOption('verbose')) {
  172. // If we don't can't find a version, use the ref instead.
  173. $currentVersion = $changes['current']['version'] ?: $changes['current']['ref'];
  174. $previousVersion = $changes['previous']['version'] ?: $changes['previous']['ref'];
  175. if ($io->isVeryVerbose()) {
  176. // Output the ref regardless of whether or not it's being used as the version
  177. $currentVersion .= sprintf(' (%s)', $changes['current']['ref']);
  178. $previousVersion .= sprintf(' (%s)', $changes['previous']['ref']);
  179. }
  180. $io->write('<info>'.$path.'</info>:');
  181. $io->write(sprintf(' From <comment>%s</comment> to <comment>%s</comment>', $previousVersion, $currentVersion));
  182. } else {
  183. $io->write($path);
  184. }
  185. }
  186. }
  187. if (($errors || $unpushedChanges || $vcsVersionChanges) && !$input->getOption('verbose')) {
  188. $io->writeError('Use --verbose (-v) to see a list of files');
  189. }
  190. return ($errors ? self::EXIT_CODE_ERRORS : 0) + ($unpushedChanges ? self::EXIT_CODE_UNPUSHED_CHANGES : 0) + ($vcsVersionChanges ? self::EXIT_CODE_VERSION_CHANGES : 0);
  191. }
  192. }