/** * Test GitHelper::getCurrentBranch(). */ public function testGetCurrentBranch() { $this->gitHelper->checkOutNew('test'); $this->assertEquals('test', $this->gitHelper->getCurrentBranch()); }
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; }