Platformsh\Cli\Local\LocalProject::ensureGitRemote PHP Method

ensureGitRemote() public method

Ensure there are appropriate Git remotes in the repository.
public ensureGitRemote ( string $dir, string $url )
$dir string
$url string
    public function ensureGitRemote($dir, $url)
    {
        if (!file_exists($dir . '/.git')) {
            throw new \InvalidArgumentException('The directory is not a Git repository');
        }
        $gitHelper = new GitHelper();
        $gitHelper->ensureInstalled();
        $gitHelper->setDefaultRepositoryDir($dir);
        $currentUrl = $gitHelper->getConfig("remote." . $this->config->get('detection.git_remote_name') . ".url", $dir);
        if (!$currentUrl) {
            $gitHelper->execute(['remote', 'add', $this->config->get('detection.git_remote_name'), $url], $dir, true);
        } elseif ($currentUrl != $url) {
            $gitHelper->execute(['remote', 'set-url', $this->config->get('detection.git_remote_name'), $url], $dir, true);
        }
        // Add an origin remote too.
        if ($this->config->get('detection.git_remote_name') !== 'origin' && !$gitHelper->getConfig("remote.origin.url", $dir)) {
            $gitHelper->execute(['remote', 'add', 'origin', $url]);
        }
    }

Usage Example

 /**
  * @param string $sourceDir
  *
  * @return string
  */
 protected function createDummyProject($sourceDir)
 {
     if (!is_dir($sourceDir)) {
         throw new \InvalidArgumentException("Not a directory: {$sourceDir}");
     }
     $projectRoot = $this->createTempSubDir('project');
     // Set up the project.
     $fsHelper = new FilesystemHelper();
     $fsHelper->copyAll($sourceDir, $projectRoot);
     // @todo perhaps make some of these steps unnecessary
     $local = new LocalProject();
     $cwd = getcwd();
     chdir($projectRoot);
     exec('git init');
     chdir($cwd);
     $local->ensureGitRemote($projectRoot, 'testProjectId');
     $local->writeCurrentProjectConfig(['id' => 'testProjectId'], $projectRoot);
     return $projectRoot;
 }
All Usage Examples Of Platformsh\Cli\Local\LocalProject::ensureGitRemote