-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathSvnBuild.php
More file actions
145 lines (122 loc) · 3.99 KB
/
Copy pathSvnBuild.php
File metadata and controls
145 lines (122 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace PHPCensor\Model\Build;
use Exception;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
/**
* Remote Subversion Build Model
*
* @package PHP Censor
* @subpackage Application
*
* @author Nadir Dzhilkibaev <imam.sharif@gmail.com>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class SvnBuild extends TypedBuild
{
protected string $svnCommand = 'svn export -q --non-interactive ';
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
$url = \rtrim($this->getProject()->getReference(), '/') . '/';
$branch = \ltrim($this->getBranch(), '/');
// For empty default branch or default branch name like "/trunk" or "trunk" (-> "trunk")
if (empty($branch) || $branch === 'trunk') {
$url .= 'trunk';
// For default branch with standard default branch directory ("branches") like "/branch-1" or "branch-1"
// (-> "branches/branch-1")
} elseif (false === \strpos($branch, '/')) {
$url .= 'branches/' . $branch;
// For default branch with non-standard branch directory like "/branch/branch-1" or "branch/branch-1"
// (-> "branch/branch-1")
} else {
$url .= $branch;
}
return $url;
}
/**
* @return void
*/
protected function extendSvnCommandFromConfig(Builder $builder)
{
$cmd = $this->svnCommand;
$buildSettings = $builder->getConfig('build_settings');
if ($buildSettings) {
if (isset($buildSettings['svn']) && \is_array($buildSettings['svn'])) {
foreach ($buildSettings['svn'] as $key => $value) {
$cmd .= " --${key} ${value} ";
}
}
if (isset($buildSettings['clone_depth']) && 0 < (int)$buildSettings['clone_depth']) {
$cmd .= ' --depth ' . \intval($buildSettings['clone_depth']) . ' ';
}
}
$this->svnCommand = $cmd;
}
/**
* Create a working copy by cloning, copying, or similar.
*
* @param string $buildPath
*
* @return bool
*
* @throws Exception
*/
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.
*
* @param string $cloneTo
*
* @return bool
*/
protected function cloneByHttp(Builder $builder, $cloneTo)
{
$cmd = $this->svnCommand;
if (!empty($this->getCommitId())) {
$cmd .= ' -r %s %s "%s"';
$success = $builder->executeCommand($cmd, \escapeshellarg($this->getCommitId()), $this->getCloneUrl(), $cloneTo);
} else {
$cmd .= ' %s "%s"';
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
}
return $success;
}
/**
* Use an SSH-based svn export.
*
* @param string $cloneTo
*
* @return bool
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$cmd = $this->svnCommand . ' %s "%s"';
$keyFile = $this->writeSshKey();
$sshWrapper = $this->writeSshWrapper($keyFile);
$cmd = 'export SVN_SSH="' . $sshWrapper . '" && ' . $cmd;
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo);
// Remove the key file and svn wrapper:
\unlink($keyFile);
\unlink($sshWrapper);
return $success;
}
}