Skip to content

Commit

Permalink
Clear up selection of the target branch when it does not already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Aug 14, 2023
1 parent b991313 commit 6b249d7
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Command/Environment/EnvironmentPushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function configure()
->setAliases(['push'])
->setDescription('Push code to an environment')
->addArgument('source', InputArgument::OPTIONAL, 'The source ref: a branch name or commit hash', 'HEAD')
->addOption('target', null, InputOption::VALUE_REQUIRED, 'The target branch name')
->addOption('target', null, InputOption::VALUE_REQUIRED, 'The target branch name. Defaults to the current branch.')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Allow non-fast-forward updates')
->addOption('force-with-lease', null, InputOption::VALUE_NONE, 'Allow non-fast-forward updates, if the remote-tracking branch is up to date')
->addOption('set-upstream', 'u', InputOption::VALUE_NONE, 'Set the target environment as the upstream for the source branch')
Expand Down Expand Up @@ -98,11 +98,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
$target = $input->getOption('target');
} elseif ($this->hasSelectedEnvironment()) {
$target = $this->getSelectedEnvironment()->id;
} elseif ($currentBranch = $git->getCurrentBranch()) {
$target = $currentBranch;
} else {
$this->stdErr->writeln('Could not determine target environment name.');
return 1;
$allEnvironments = $this->api()->getEnvironments($project);
$currentBranch = $git->getCurrentBranch();
if ($currentBranch !== false && isset($allEnvironments[$currentBranch])) {
$target = $currentBranch;
} else {
$default = $currentBranch !== false ? $currentBranch : null;
$this->stdErr->writeln('Pushing to a new branch will create a new environment (inactive, by default).');
$target = $questionHelper->askInput('Enter the target branch name', $default, array_keys($allEnvironments));
if ($target === null) {
$this->stdErr->writeln('A target branch name (<error>--target</error>) is required.');
return 1;
}
}
}

/** @var Environment|false $targetEnvironment The target environment, which may not exist yet. */
Expand All @@ -114,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$questionText = sprintf(
'Are you sure you want to push to the %s branch?',
$targetEnvironment
? $this->api()->getEnvironmentLabel($targetEnvironment, 'comment')
? $this->api()->getEnvironmentLabel($targetEnvironment, 'comment', false)
: '<comment>' . $target . '</comment>'
);
if (!$questionHelper->confirm($questionText)) {
Expand Down

0 comments on commit 6b249d7

Please sign in to comment.