Skip to content

Commit a58abf5

Browse files
committed
Exiting early in recipes:update if the index is not clean
Avoids problems later when we run "git update-index --refresh"
1 parent 14eb293 commit a58abf5

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Command/UpdateRecipesCommand.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Composer\Command\BaseCommand;
1515
use Composer\IO\IOInterface;
16+
use Composer\Util\ProcessExecutor;
1617
use Symfony\Component\Console\Exception\RuntimeException;
1718
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
1819
use Symfony\Component\Console\Input\InputArgument;
@@ -60,11 +61,20 @@ protected function configure()
6061
protected function execute(InputInterface $input, OutputInterface $output): int
6162
{
6263
$win = '\\' === \DIRECTORY_SEPARATOR;
64+
$runtimeExceptionClass = class_exists(RuntimeException::class) ? RuntimeException::class : \RuntimeException::class;
6365
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.');
6567
}
6668

6769
$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+
}
6878

6979
$packageName = $input->getArgument('package');
7080
$symfonyLock = $this->flex->getLock();
@@ -376,4 +386,18 @@ private function askForPackage(IOInterface $io, Lock $symfonyLock): ?string
376386

377387
return $outdatedRecipes[$choice];
378388
}
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+
}
379403
}

0 commit comments

Comments
 (0)