From 0a4540324fbd8ab3fdbe80a1d14c3808d7040812 Mon Sep 17 00:00:00 2001 From: Patrick Dawkins Date: Mon, 14 Oct 2024 20:31:22 +0100 Subject: [PATCH] After branching, only set the upstream if the remote exists --- .../Environment/EnvironmentBranchCommand.php | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Command/Environment/EnvironmentBranchCommand.php b/src/Command/Environment/EnvironmentBranchCommand.php index 8f8cb50ce..ef08ae2ae 100644 --- a/src/Command/Environment/EnvironmentBranchCommand.php +++ b/src/Command/Environment/EnvironmentBranchCommand.php @@ -198,17 +198,26 @@ protected function execute(InputInterface $input, OutputInterface $output) /** @var \Platformsh\Cli\Service\ActivityMonitor $activityMonitor */ $activityMonitor = $this->getService('activity_monitor'); $remoteSuccess = $activityMonitor->waitMultiple($activities, $selectedProject); + $this->api()->clearEnvironmentsCache($selectedProject->id); + } - // If a new local branch has been created, set it to track the - // remote branch. This requires first fetching the new branch from - // the remote. - if ($remoteSuccess && $checkoutLocally && $createdNew) { - $upstreamRemote = $this->config()->get('detection.git_remote_name'); - $git->fetch($upstreamRemote, $branchName, $selectedProject->getGitUrl(), $projectRoot); - $git->setUpstream($upstreamRemote . '/' . $branchName, $branchName, $projectRoot); + // If a new local branch has been created, set its upstream. + // + // This will only be done if the repository already has a named remote, + // matching the configured detection.git_remote_name, and set to the + // project's Git URL. + if ($remoteSuccess && $checkoutLocally && $createdNew) { + $gitUrl = $selectedProject->getGitUrl(); + $remoteName = $this->config()->get('detection.git_remote_name'); + if ($gitUrl && $git->getConfig(sprintf('remote.%s.url', $remoteName), $projectRoot) === $gitUrl) { + $this->stdErr->writeln(sprintf( + 'Setting the upstream for the local branch to: %s/%s', + $remoteName, $branchName + )); + if ($git->fetch($remoteName, $branchName, $gitUrl, $projectRoot)) { + $git->setUpstream($remoteName . '/' . $branchName, $branchName, $projectRoot); + } } - - $this->api()->clearEnvironmentsCache($selectedProject->id); } return $remoteSuccess ? 0 : 1;