|
13 | 13 |
|
14 | 14 | use Composer\Command\BaseCommand;
|
15 | 15 | use Composer\IO\IOInterface;
|
| 16 | +use Composer\Util\ProcessExecutor; |
16 | 17 | use Symfony\Component\Console\Exception\RuntimeException;
|
17 | 18 | use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
18 | 19 | use Symfony\Component\Console\Input\InputArgument;
|
@@ -60,11 +61,20 @@ protected function configure()
|
60 | 61 | protected function execute(InputInterface $input, OutputInterface $output): int
|
61 | 62 | {
|
62 | 63 | $win = '\\' === \DIRECTORY_SEPARATOR;
|
| 64 | + $runtimeExceptionClass = class_exists(RuntimeException::class) ? RuntimeException::class : \RuntimeException::class; |
63 | 65 | if (!@is_executable(strtok(exec($win ? 'where git' : 'command -v git'), \PHP_EOL))) {
|
64 |
| - throw new RuntimeException('Cannot run "recipes:update": git not found.'); |
| 66 | + throw new $runtimeExceptionClass('Cannot run "recipes:update": git not found.'); |
65 | 67 | }
|
66 | 68 |
|
67 | 69 | $io = $this->getIO();
|
| 70 | + if (!$this->isIndexClean($io)) { |
| 71 | + $io->write([ |
| 72 | + ' Cannot run <comment>recipes:update</comment>: Your git index contains uncommitted changes.', |
| 73 | + ' Please commit or stash them and try again!', |
| 74 | + ]); |
| 75 | + |
| 76 | + return 1; |
| 77 | + } |
68 | 78 |
|
69 | 79 | $packageName = $input->getArgument('package');
|
70 | 80 | $symfonyLock = $this->flex->getLock();
|
@@ -376,4 +386,18 @@ private function askForPackage(IOInterface $io, Lock $symfonyLock): ?string
|
376 | 386 |
|
377 | 387 | return $outdatedRecipes[$choice];
|
378 | 388 | }
|
| 389 | + |
| 390 | + private function isIndexClean(IOInterface $io): bool |
| 391 | + { |
| 392 | + $processExecutor = new ProcessExecutor($io); |
| 393 | + if (0 !== $processExecutor->execute('git diff-files --quiet --ignore-submodules')) { |
| 394 | + return false; |
| 395 | + } |
| 396 | + |
| 397 | + if (0 !== $processExecutor->execute('git diff-index --cached --quiet --ignore-submodules HEAD --')) { |
| 398 | + return false; |
| 399 | + } |
| 400 | + |
| 401 | + return true; |
| 402 | + } |
379 | 403 | }
|
0 commit comments