2015-01-26 19:12:48 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Model\Build;
|
2015-01-26 19:12:48 +01:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Builder;
|
2015-01-26 19:12:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remote Subversion Build Model
|
2018-02-28 04:00:10 +01:00
|
|
|
*
|
2017-02-07 13:50:09 +01:00
|
|
|
* @author Nadir Dzhilkibaev <imam.sharif@gmail.com>
|
2015-01-26 19:12:48 +01:00
|
|
|
*/
|
2018-02-28 04:00:10 +01:00
|
|
|
class SvnBuild extends Build
|
2015-01-26 19:12:48 +01:00
|
|
|
{
|
|
|
|
protected $svnCommand = 'svn export -q --non-interactive ';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the URL to be used to clone this remote repository.
|
|
|
|
*/
|
|
|
|
protected function getCloneUrl()
|
|
|
|
{
|
2017-06-03 12:19:08 +02:00
|
|
|
$url = rtrim($this->getProject()->getReference(), '/') . '/';
|
|
|
|
$branch = ltrim($this->getBranch(), '/');
|
2018-02-28 04:00:10 +01:00
|
|
|
|
2017-06-03 12:19:08 +02:00
|
|
|
// For empty default branch or default branch name like "/trunk" or "trunk" (-> "trunk")
|
2015-01-26 19:12:48 +01:00
|
|
|
if (empty($branch) || $branch == 'trunk') {
|
|
|
|
$url .= 'trunk';
|
2017-06-03 12:19:08 +02:00
|
|
|
// For default branch with standard default branch directory ("branches") like "/branch-1" or "branch-1"
|
|
|
|
// (-> "branches/branch-1")
|
|
|
|
} elseif (false === strpos($branch, '/')) {
|
2015-01-26 19:12:48 +01:00
|
|
|
$url .= 'branches/' . $branch;
|
2017-06-03 12:19:08 +02:00
|
|
|
// For default branch with non-standard branch directory like "/branch/branch-1" or "branch/branch-1"
|
|
|
|
// (-> "branch/branch-1")
|
|
|
|
} else {
|
|
|
|
$url .= $branch;
|
2015-01-26 19:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Builder $builder
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function extendSvnCommandFromConfig(Builder $builder)
|
|
|
|
{
|
|
|
|
$cmd = $this->svnCommand;
|
|
|
|
|
|
|
|
$svn = $builder->getConfig('svn');
|
|
|
|
if ($svn) {
|
|
|
|
foreach ($svn as $key => $value) {
|
2018-04-15 10:48:50 +02:00
|
|
|
$cmd .= " --${key} ${value} ";
|
2015-01-26 19:12:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-15 10:48:50 +02:00
|
|
|
$buildSettings = $builder->getConfig('build_settings');
|
|
|
|
if ($buildSettings && isset($buildSettings['clone_depth'])) {
|
|
|
|
$cmd .= ' --depth ' . intval($buildSettings['clone_depth']) . ' ';
|
2015-01-26 19:12:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->svnCommand = $cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a working copy by cloning, copying, or similar.
|
|
|
|
*/
|
|
|
|
public function createWorkingCopy(Builder $builder, $buildPath)
|
|
|
|
{
|
|
|
|
$this->extendSvnCommandFromConfig($builder);
|
|
|
|
|
|
|
|
$key = trim($this->getProject()->getSshPrivateKey());
|
|
|
|
|
|
|
|
if (!empty($key)) {
|
|
|
|
$success = $this->cloneBySsh($builder, $buildPath);
|
|
|
|
} else {
|
|
|
|
$success = $this->cloneByHttp($builder, $buildPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$success) {
|
|
|
|
$builder->logFailure('Failed to export remote subversion repository.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->handleConfig($builder, $buildPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use an HTTP-based svn export.
|
|
|
|
*/
|
|
|
|
protected function cloneByHttp(Builder $builder, $cloneTo)
|
|
|
|
{
|
|
|
|
$cmd = $this->svnCommand;
|
|
|
|
|
2017-04-13 17:00:29 +02:00
|
|
|
if (!empty($this->getCommitId())) {
|
2015-01-26 19:12:48 +01:00
|
|
|
$cmd .= ' -r %s %s "%s"';
|
|
|
|
$success = $builder->executeCommand($cmd, $this->getCommitId(), $this->getCloneUrl(), $cloneTo);
|
|
|
|
} else {
|
|
|
|
$cmd .= ' %s "%s"';
|
|
|
|
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use an SSH-based svn export.
|
|
|
|
*/
|
|
|
|
protected function cloneBySsh(Builder $builder, $cloneTo)
|
|
|
|
{
|
2017-03-19 04:16:14 +01:00
|
|
|
$cmd = $this->svnCommand . ' %s "%s"';
|
2017-02-01 16:27:47 +01:00
|
|
|
$keyFile = $this->writeSshKey($cloneTo);
|
|
|
|
$sshWrapper = $this->writeSshWrapper($cloneTo, $keyFile);
|
|
|
|
$cmd = 'export SVN_SSH="' . $sshWrapper . '" && ' . $cmd;
|
2015-01-26 19:12:48 +01:00
|
|
|
|
|
|
|
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
|
|
|
|
|
2017-02-01 16:27:47 +01:00
|
|
|
// Remove the key file and svn wrapper:
|
|
|
|
unlink($keyFile);
|
|
|
|
unlink($sshWrapper);
|
2015-01-26 19:12:48 +01:00
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
}
|