/** * Check whether a branch exists, locally in Git or on the remote. * * @param string $branch * @param Project $project * @param GitHelper $gitHelper * * @return string|false */ protected function branchExists($branch, Project $project, GitHelper $gitHelper) { // Check if the Git branch exists locally. $candidates = array_unique(array(Environment::sanitizeId($branch), $branch)); foreach ($candidates as $candidate) { if ($gitHelper->branchExists($candidate)) { return $candidate; } } // Check if the environment exists by title or ID. This is usually faster // than running 'git ls-remote'. $environments = $this->getEnvironments($project); foreach ($environments as $environment) { if ($environment['title'] == $branch || $environment['id'] == $branch) { return $environment['id']; } } return false; }
/** * Get the current environment if the user is in a project directory. * * @param Project $expectedProject The expected project. * * @return Environment|false The current environment. */ public function getCurrentEnvironment(Project $expectedProject = null) { if (!($projectRoot = $this->getProjectRoot()) || !($project = $this->getCurrentProject()) || $expectedProject !== null && $expectedProject->id !== $project->id) { return false; } $gitHelper = $this->getHelper('git'); $gitHelper->setDefaultRepositoryDir($this->getProjectRoot() . '/' . LocalProject::REPOSITORY_DIR); $currentBranch = $gitHelper->getCurrentBranch(); // Check if there is a manual mapping set for the current branch. if ($currentBranch) { $config = $this->getProjectConfig($projectRoot); if (!empty($config['mapping'][$currentBranch])) { $environment = $this->getEnvironment($config['mapping'][$currentBranch], $project); if ($environment) { $this->debug('Found mapped environment for branch ' . $currentBranch . ': ' . $environment->id); return $environment; } else { unset($config['mapping'][$currentBranch]); $this->setProjectConfig('mapping', $config['mapping'], $projectRoot); } } } // Check whether the user has a Git upstream set to a Platform // environment ID. $upstream = $gitHelper->getUpstream(); if ($upstream && strpos($upstream, '/') !== false) { list(, $potentialEnvironment) = explode('/', $upstream, 2); $environment = $this->getEnvironment($potentialEnvironment, $project); if ($environment) { $this->debug('Selected environment ' . $potentialEnvironment . ', based on Git upstream: ' . $upstream); return $environment; } } // There is no Git remote set, or it's set to a non-Platform URL. // Fall back to trying the current branch name. if ($currentBranch) { $currentBranchSanitized = Environment::sanitizeId($currentBranch); $environment = $this->getEnvironment($currentBranchSanitized, $project); if ($environment) { $this->debug('Selected environment ' . $currentBranchSanitized . ', based on branch name:' . $currentBranch); return $environment; } } return false; }
protected function execute(InputInterface $input, OutputInterface $output) { $this->envArgName = 'parent'; $this->validateInput($input, true); $selectedProject = $this->getSelectedProject(); $branchName = $input->getArgument('name'); if (empty($branchName)) { if ($input->isInteractive()) { // List environments. return $this->runOtherCommand('environments', array('--project' => $selectedProject->id)); } $this->stdErr->writeln("<error>You must specify the name of the new branch.</error>"); return 1; } $machineName = Environment::sanitizeId($branchName); $environmentId = $this->getSelectedEnvironment()['id']; if ($machineName == $environmentId) { $this->stdErr->writeln("<comment>Already on {$machineName}</comment>"); return 1; } if ($environment = $this->getEnvironment($machineName, $selectedProject)) { $checkout = $this->getHelper('question')->confirm("The environment <comment>{$machineName}</comment> already exists. Check out?", $input, $this->stdErr); if ($checkout) { return $this->runOtherCommand('environment:checkout', array('id' => $environment->id)); } return 1; } if (!$this->getSelectedEnvironment()->operationAvailable('branch')) { $this->stdErr->writeln("Operation not available: The environment <error>{$environmentId}</error> can't be branched."); return 1; } $force = $input->getOption('force'); $projectRoot = $this->getProjectRoot(); if (!$projectRoot && $force) { $this->stdErr->writeln("<comment>This command was run from outside your local project root, the new Platform.sh branch cannot be checked out in your local Git repository." . " Make sure to run 'platform checkout' or 'git checkout' in your repository directory to switch to the branch you are expecting.</comment>"); $local_error = true; } elseif (!$projectRoot) { $this->stdErr->writeln("<error>You must run this command inside the project root, or specify --force.</error>"); return 1; } $selectedEnvironment = $this->getSelectedEnvironment(); $this->stdErr->writeln("Creating a new environment <info>{$branchName}</info>, branched from <info>{$selectedEnvironment['title']}</info>"); $activity = $selectedEnvironment->branch($branchName, $machineName); // Clear the environments cache, as branching has started. $this->clearEnvironmentsCache($selectedProject); if ($projectRoot) { $gitHelper = new GitHelper(new ShellHelper($this->stdErr)); $gitHelper->setDefaultRepositoryDir($projectRoot . '/' . LocalProject::REPOSITORY_DIR); // If the Git branch already exists locally, just check it out. $existsLocally = $gitHelper->branchExists($machineName); if ($existsLocally) { $this->stdErr->writeln("Checking out <info>{$machineName}</info> locally"); if (!$gitHelper->checkOut($machineName)) { $this->stdErr->writeln('<error>Failed to check out branch locally: ' . $machineName . '</error>'); $local_error = true; if (!$force) { return 1; } } } else { // Create a new branch, using the current or specified environment as the parent if it exists locally. $parent = $this->getSelectedEnvironment()['id']; if (!$gitHelper->branchExists($parent)) { $parent = null; } $this->stdErr->writeln("Creating local branch <info>{$machineName}</info>"); if (!$gitHelper->checkOutNew($machineName, $parent)) { $this->stdErr->writeln('<error>Failed to create branch locally: ' . $machineName . '</error>'); $local_error = true; if (!$force) { return 1; } } } } $remoteSuccess = true; if (!$input->getOption('no-wait')) { $remoteSuccess = ActivityUtil::waitAndLog($activity, $this->stdErr, "The environment <info>{$branchName}</info> has been branched.", '<error>Branching failed</error>'); // Clear the environments cache again. $this->clearEnvironmentsCache($selectedProject); } $build = $input->getOption('build'); if (empty($local_error) && $build && $projectRoot) { // Build the new branch. try { $buildSettings = array('environmentId' => $machineName, 'verbosity' => $output->getVerbosity()); $builder = new LocalBuild($buildSettings, $output); $builder->buildProject($projectRoot); } catch (\Exception $e) { $this->stdErr->writeln("<comment>The new branch could not be built: \n" . $e->getMessage() . "</comment>"); return 1; } } $this->clearEnvironmentsCache(); return $remoteSuccess ? 0 : 1; }
/** * Get the current environment if the user is in a project directory. * * @param Project $project The current project. * * @return Environment|false The current environment */ public function getCurrentEnvironment(Project $project) { if (!$this->getProjectRoot()) { return false; } // Check whether the user has a Git upstream set to a Platform // environment ID. $gitHelper = $this->getHelper('git'); $gitHelper->setDefaultRepositoryDir($this->getProjectRoot() . '/' . LocalProject::REPOSITORY_DIR); $upstream = $gitHelper->getUpstream(); if ($upstream && strpos($upstream, '/') !== false) { list(, $potentialEnvironment) = explode('/', $upstream, 2); $environment = $this->getEnvironment($potentialEnvironment, $project); if ($environment) { return $environment; } } // There is no Git remote set, or it's set to a non-Platform URL. // Fall back to trying the current branch name. $currentBranch = $gitHelper->getCurrentBranch(); if ($currentBranch) { $currentBranchSanitized = Environment::sanitizeId($currentBranch); $environment = $this->getEnvironment($currentBranchSanitized, $project); if ($environment) { return $environment; } } return false; }
/** * Get the current environment if the user is in a project directory. * * @param Project $expectedProject The expected project. * @param bool|null $refresh Whether to refresh the environments or projects * cache. * * @return Environment|false The current environment. */ public function getCurrentEnvironment(Project $expectedProject = null, $refresh = null) { if (!($projectRoot = $this->getProjectRoot()) || !($project = $this->getCurrentProject()) || $expectedProject !== null && $expectedProject->id !== $project->id) { return false; } $gitHelper = $this->getHelper('git'); $gitHelper->setDefaultRepositoryDir($this->getProjectRoot()); $config = $this->localProject->getProjectConfig($projectRoot); // Check if there is a manual mapping set for the current branch. if (!empty($config['mapping']) && ($currentBranch = $gitHelper->getCurrentBranch()) && !empty($config['mapping'][$currentBranch])) { $environment = $this->api()->getEnvironment($config['mapping'][$currentBranch], $project, $refresh); if ($environment) { $this->debug('Found mapped environment for branch ' . $currentBranch . ': ' . $environment->id); return $environment; } else { unset($config['mapping'][$currentBranch]); $this->localProject->writeCurrentProjectConfig($config, $projectRoot); } } // Check whether the user has a Git upstream set to a remote environment // ID. $upstream = $gitHelper->getUpstream(); if ($upstream && strpos($upstream, '/') !== false) { list(, $potentialEnvironment) = explode('/', $upstream, 2); $environment = $this->api()->getEnvironment($potentialEnvironment, $project, $refresh); if ($environment) { $this->debug('Selected environment ' . $potentialEnvironment . ', based on Git upstream: ' . $upstream); return $environment; } } // There is no Git remote set. Fall back to trying the current branch // name. if (!empty($currentBranch) || ($currentBranch = $gitHelper->getCurrentBranch())) { $environment = $this->api()->getEnvironment($currentBranch, $project, $refresh); if (!$environment) { // Try a sanitized version of the branch name too. $currentBranchSanitized = Environment::sanitizeId($currentBranch); $environment = $this->api()->getEnvironment($currentBranchSanitized, $project, $refresh); } if ($environment) { $this->debug('Selected environment ' . $environment->id . ' based on branch name: ' . $currentBranch); return $environment; } } return false; }