private function cloneToBuildDir($buildDir)
{
$gitRoot = $this->gitHelper->getRoot($this->appRoot, true);
$ref = $this->gitHelper->execute(['rev-parse', 'HEAD'], $gitRoot, true);
$cloneArgs = ['--recursive', '--shared'];
$tmpRepo = $buildDir . '-repo';
if (file_exists($tmpRepo)) {
$this->fsHelper->remove($tmpRepo, true);
}
$this->gitHelper->cloneRepo($gitRoot, $tmpRepo, $cloneArgs, true);
$this->gitHelper->checkOut($ref, $tmpRepo, true, true);
$this->fsHelper->remove($tmpRepo . '/.git');
$appDir = $tmpRepo . '/' . substr($this->appRoot, strlen($gitRoot));
if (!rename($appDir, $buildDir)) {
throw new \RuntimeException(sprintf('Failed to move app from %s to %s', $appDir, $buildDir));
}
$this->fsHelper->remove($tmpRepo);
}