From b1edf3dc27ec9527aea289f60f68a7f6b8ecd127 Mon Sep 17 00:00:00 2001 From: bochkovprivate Date: Wed, 29 Jun 2016 08:34:42 +0200 Subject: [PATCH] Fix fetching from Mercurial repos: ssh, branches --- PHPCI/Model/Build/MercurialBuild.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/PHPCI/Model/Build/MercurialBuild.php b/PHPCI/Model/Build/MercurialBuild.php index 0f38940b..8e59ea95 100644 --- a/PHPCI/Model/Build/MercurialBuild.php +++ b/PHPCI/Model/Build/MercurialBuild.php @@ -42,7 +42,7 @@ class MercurialBuild extends Build } if (!$success) { - $builder->logFailure('Failed to clone remote git repository.'); + $builder->logFailure('Failed to clone remote hg repository.'); return false; } @@ -57,16 +57,34 @@ class MercurialBuild extends Build return $builder->executeCommand('hg clone %s "%s" -r %s', $this->getCloneUrl(), $cloneTo, $this->getBranch()); } + /** + * Create an SSH key file on disk for this build. + * @param $cloneTo + * @return string + */ + protected function writeSshKey($cloneTo) + { + $keyPath = dirname($cloneTo . '/temp'); + $keyFile = $keyPath . '.key'; + + // Write the contents of this project's hg key to the file: + file_put_contents($keyFile, $this->getProject()->getSshPrivateKey()); + chmod($keyFile, 0600); + + // Return the filename: + return $keyFile; + } + /** * Use an SSH-based Mercurial clone. */ protected function cloneBySsh(Builder $builder, $cloneTo) { - $keyFile = $this->writeSshKey(); + $keyFile = $this->writeSshKey($cloneTo); - // Do the git clone: - $cmd = 'hg clone --ssh "ssh -i '.$keyFile.'" %s "%s"'; - $success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo); + // Do the hg clone: + $cmd = 'hg clone --ssh "ssh -i '.$keyFile.'" %s "%s" -r %s'; + $success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo, $this->getBranch()); if ($success) { $success = $this->postCloneSetup($builder, $cloneTo);