getUpstream() public method

Get the upstream for a branch.
public getUpstream ( string $branch = null, string | null $dir = null, boolean $mustRun = false ) : string | false
$branch string The name of the branch to get the upstream for. Defaults to the current branch.
$dir string | null The path to a Git repository.
$mustRun boolean Enable exceptions if the Git command fails.
return string | false The upstream, in the form remote/branch, or false if no upstream is found.
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $project = $this->getCurrentProject();
     if (!$project) {
         throw new RootNotFoundException();
     }
     $projectRoot = $this->getProjectRoot();
     $repositoryDir = $projectRoot . '/' . LocalProject::REPOSITORY_DIR;
     $gitHelper = new GitHelper(new ShellHelper($output));
     $gitHelper->setDefaultRepositoryDir($repositoryDir);
     $specifiedEnvironmentId = $input->getArgument('environment');
     if ($specifiedEnvironmentId != '0' && !($specifiedEnvironment = $this->getEnvironment($specifiedEnvironmentId, $project))) {
         $this->stdErr->writeln("Environment not found: <error>{$specifiedEnvironmentId}</error>");
         return 1;
     }
     $specifiedBranch = $input->getArgument('branch');
     if ($specifiedBranch) {
         if (!$gitHelper->branchExists($specifiedBranch)) {
             $this->stdErr->writeln("Branch not found: <error>{$specifiedBranch}</error>");
             return 1;
         }
     } else {
         $specifiedBranch = $gitHelper->getCurrentBranch();
     }
     // Check whether the branch is mapped by default (its name or its Git
     // upstream is the same as the remote environment ID).
     $mappedByDefault = isset($specifiedEnvironment) && $specifiedEnvironment->status != 'inactive' && $specifiedEnvironmentId === $specifiedBranch;
     if ($specifiedEnvironmentId != '0' && !$mappedByDefault) {
         $upstream = $gitHelper->getUpstream($specifiedBranch);
         if (strpos($upstream, '/')) {
             list(, $upstream) = explode('/', $upstream, 2);
         }
         if ($upstream === $specifiedEnvironmentId) {
             $mappedByDefault = true;
         }
         if (!$mappedByDefault && $gitHelper->branchExists($specifiedEnvironmentId)) {
             $this->stdErr->writeln("A local branch already exists named <comment>{$specifiedEnvironmentId}</comment>");
         }
     }
     // Perform the mapping or unmapping.
     $config = $this->getProjectConfig($projectRoot);
     $config += ['mapping' => []];
     if ($mappedByDefault || $specifiedEnvironmentId == '0') {
         unset($config['mapping'][$specifiedBranch]);
         $this->setProjectConfig('mapping', $config['mapping'], $projectRoot);
     } else {
         if (isset($config['mapping']) && ($current = array_search($specifiedEnvironmentId, $config['mapping'])) !== false) {
             unset($config['mapping'][$current]);
         }
         $config['mapping'][$specifiedBranch] = $specifiedEnvironmentId;
         $this->setProjectConfig('mapping', $config['mapping'], $projectRoot);
     }
     // Check the success of the operation.
     if (isset($config['mapping'][$specifiedBranch])) {
         $actualRemoteEnvironment = $config['mapping'][$specifiedBranch];
         $this->stdErr->writeln("The local branch <info>{$specifiedBranch}</info> is mapped to the remote environment <info>{$actualRemoteEnvironment}</info>");
     } elseif ($mappedByDefault) {
         $actualRemoteEnvironment = $specifiedBranch;
         $this->stdErr->writeln("The local branch <info>{$specifiedBranch}</info> is mapped to the default remote environment, <info>{$specifiedBranch}</info>");
     } else {
         $this->stdErr->writeln("The local branch <info>{$specifiedBranch}</info> is not mapped to a remote environment");
     }
     $success = !empty($actualRemoteEnvironment) ? $actualRemoteEnvironment == $specifiedEnvironmentId : $specifiedEnvironmentId == '0';
     return $success ? 0 : 1;
 }