Skip to content

Commit 0a45403

Browse files
committed
After branching, only set the upstream if the remote exists
1 parent 4476026 commit 0a45403

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/Command/Environment/EnvironmentBranchCommand.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
198198
/** @var \Platformsh\Cli\Service\ActivityMonitor $activityMonitor */
199199
$activityMonitor = $this->getService('activity_monitor');
200200
$remoteSuccess = $activityMonitor->waitMultiple($activities, $selectedProject);
201+
$this->api()->clearEnvironmentsCache($selectedProject->id);
202+
}
201203

202-
// If a new local branch has been created, set it to track the
203-
// remote branch. This requires first fetching the new branch from
204-
// the remote.
205-
if ($remoteSuccess && $checkoutLocally && $createdNew) {
206-
$upstreamRemote = $this->config()->get('detection.git_remote_name');
207-
$git->fetch($upstreamRemote, $branchName, $selectedProject->getGitUrl(), $projectRoot);
208-
$git->setUpstream($upstreamRemote . '/' . $branchName, $branchName, $projectRoot);
204+
// If a new local branch has been created, set its upstream.
205+
//
206+
// This will only be done if the repository already has a named remote,
207+
// matching the configured detection.git_remote_name, and set to the
208+
// project's Git URL.
209+
if ($remoteSuccess && $checkoutLocally && $createdNew) {
210+
$gitUrl = $selectedProject->getGitUrl();
211+
$remoteName = $this->config()->get('detection.git_remote_name');
212+
if ($gitUrl && $git->getConfig(sprintf('remote.%s.url', $remoteName), $projectRoot) === $gitUrl) {
213+
$this->stdErr->writeln(sprintf(
214+
'Setting the upstream for the local branch to: <info>%s/%s</info>',
215+
$remoteName, $branchName
216+
));
217+
if ($git->fetch($remoteName, $branchName, $gitUrl, $projectRoot)) {
218+
$git->setUpstream($remoteName . '/' . $branchName, $branchName, $projectRoot);
219+
}
209220
}
210-
211-
$this->api()->clearEnvironmentsCache($selectedProject->id);
212221
}
213222

214223
return $remoteSuccess ? 0 : 1;

0 commit comments

Comments
 (0)