2013-09-04 14:54:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-12 18:26:17 +02:00
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link https://www.phptesting.org/
|
|
|
|
*/
|
2013-09-04 14:54:26 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Model\Build;
|
|
|
|
|
|
|
|
use PHPCI\Model\Build;
|
|
|
|
use PHPCI\Builder;
|
|
|
|
|
|
|
|
/**
|
2015-02-23 16:58:14 +01:00
|
|
|
* Mercurial Build Model
|
|
|
|
* @author Pavel Gopanenko <pavelgopanenko@gmail.com>
|
|
|
|
* @package PHPCI
|
|
|
|
* @subpackage Core
|
|
|
|
*/
|
2013-09-04 14:54:26 +02:00
|
|
|
class MercurialBuild extends Build
|
|
|
|
{
|
|
|
|
/**
|
2015-02-23 16:58:14 +01:00
|
|
|
* Get the URL to be used to clone this remote repository.
|
|
|
|
*/
|
2013-09-04 14:54:26 +02:00
|
|
|
protected function getCloneUrl()
|
|
|
|
{
|
|
|
|
return $this->getProject()->getReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-23 16:58:14 +01:00
|
|
|
* Create a working copy by cloning, copying, or similar.
|
|
|
|
*/
|
2013-09-04 14:54:26 +02:00
|
|
|
public function createWorkingCopy(Builder $builder, $buildPath)
|
|
|
|
{
|
2015-02-23 16:58:14 +01:00
|
|
|
$key = trim($this->getProject()->getSshPublicKey());
|
|
|
|
|
|
|
|
if (!empty($key) && strpos($this->getProject()->getReference(), 'ssh') > -1) {
|
|
|
|
$success = $this->cloneBySsh($builder, $buildPath);
|
|
|
|
} else {
|
|
|
|
$success = $this->cloneByHttp($builder, $buildPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$success) {
|
|
|
|
$builder->logFailure('Failed to clone remote git repository.');
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-04 14:54:26 +02:00
|
|
|
|
2014-03-15 06:22:59 +01:00
|
|
|
return $this->handleConfig($builder, $buildPath);
|
2013-09-04 14:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-23 16:58:14 +01:00
|
|
|
* Use a HTTP-based Mercurial clone.
|
|
|
|
*/
|
2013-10-10 02:01:06 +02:00
|
|
|
protected function cloneByHttp(Builder $builder, $cloneTo)
|
2013-09-04 14:54:26 +02:00
|
|
|
{
|
2013-10-10 02:01:06 +02:00
|
|
|
return $builder->executeCommand('hg clone %s "%s" -r %s', $this->getCloneUrl(), $cloneTo, $this->getBranch());
|
2013-09-04 14:54:26 +02:00
|
|
|
}
|
2015-02-23 16:58:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use an SSH-based Mercurial clone.
|
|
|
|
*/
|
|
|
|
protected function cloneBySsh(Builder $builder, $cloneTo)
|
|
|
|
{
|
|
|
|
$keyFile = $this->writeSshKey();
|
|
|
|
|
|
|
|
// Do the git clone:
|
|
|
|
$cmd = 'hg clone --ssh "ssh -i '.$keyFile.'" %s "%s"';
|
|
|
|
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
|
|
|
|
|
|
|
|
if ($success) {
|
|
|
|
$success = $this->postCloneSetup($builder, $cloneTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the key file:
|
|
|
|
unlink($keyFile);
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle post-clone tasks (switching branch, etc.)
|
|
|
|
* @param Builder $builder
|
|
|
|
* @param $cloneTo
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function postCloneSetup(Builder $builder, $cloneTo)
|
|
|
|
{
|
|
|
|
$success = true;
|
|
|
|
$commit = $this->getCommitId();
|
|
|
|
|
|
|
|
// Allow switching to a specific branch:
|
|
|
|
if (!empty($commit) && $commit != 'Manual') {
|
|
|
|
$cmd = 'cd "%s" && hg checkout %s';
|
|
|
|
$success = $builder->executeCommand($cmd, $cloneTo, $this->getBranch());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
2013-09-04 14:54:26 +02:00
|
|
|
}
|