forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitBuild.php
More file actions
186 lines (156 loc) · 5.25 KB
/
Copy pathGitBuild.php
File metadata and controls
186 lines (156 loc) · 5.25 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace PHPCensor\Model\Build;
use PHPCensor\Model\Build;
use PHPCensor\Builder;
use Psr\Log\LogLevel;
/**
* Remote Git Build Model
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class GitBuild extends Build
{
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
return $this->getProject()->getReference();
}
/**
* Create a working copy by cloning, copying, or similar.
*
* @param Builder $builder
* @param string $buildPath
*
* @return bool
*
* @throws \Exception
*/
public function createWorkingCopy(Builder $builder, $buildPath)
{
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
$success = $this->cloneBySsh($builder, $buildPath);
} else {
$success = $this->cloneByHttp($builder, $buildPath);
}
if ($success) {
$success = $this->mergeBranches($builder, $buildPath);
}
if (!$success) {
$builder->logFailure('Failed to clone remote git repository.');
return false;
}
return $this->handleConfig($builder, $buildPath);
}
/**
* @param Builder $builder
* @param string $buildPath
*
* @return bool
*/
protected function mergeBranches(Builder $builder, $buildPath)
{
$branches = $this->getExtra('branches');
if (!empty($branches)) {
$cmd = 'cd "%s" && git merge --quiet origin/%s';
foreach ($branches as $branch) {
$success = $builder->executeCommand($cmd, $buildPath, $branch);
if (!$success) {
$builder->log('Fail merge branch origin/' . $branch, LogLevel::ERROR);
return false;
}
$builder->log('Merged branch origin/' . $branch, LogLevel::INFO);
}
}
return true;
}
/**
* Use an HTTP-based git clone.
*
* @param Builder $builder
* @param string $cloneTo
*
* @return bool
*/
protected function cloneByHttp(Builder $builder, $cloneTo)
{
$cmd = 'cd .. && git clone --recursive ';
$buildSettings = $builder->getConfig('build_settings');
if ($buildSettings && isset($buildSettings['clone_depth']) && (0 < (int)$buildSettings['clone_depth'])) {
$cmd .= ' --depth ' . intval($buildSettings['clone_depth']) . ' ';
}
$cmd .= ' -b "%s" "%s" "%s"';
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
if ($success) {
$success = $this->postCloneSetup($builder, $cloneTo);
}
return $success;
}
/**
* Use an SSH-based git clone.
*
* @param Builder $builder
* @param string $cloneTo
*
* @return bool
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$keyFile = $this->writeSshKey();
$gitSshWrapper = $this->writeSshWrapper($keyFile);
// Do the git clone:
$cmd = 'cd .. && git clone --recursive ';
$buildSettings = $builder->getConfig('build_settings');
if ($buildSettings && isset($buildSettings['clone_depth']) && (0 < (int)$buildSettings['clone_depth'])) {
$cmd .= ' --depth ' . intval($buildSettings['clone_depth']) . ' ';
}
$cmd .= ' -b "%s" "%s" "%s"';
$cmd = 'export GIT_SSH="' . $gitSshWrapper . '" && ' . $cmd;
$success = $builder->executeCommand($cmd, $this->getBranch(), $this->getCloneUrl(), $cloneTo);
if ($success) {
$extra = [
'git_ssh_wrapper' => $gitSshWrapper
];
$success = $this->postCloneSetup($builder, $cloneTo, $extra);
}
// Remove the key file and git wrapper:
unlink($keyFile);
unlink($gitSshWrapper);
return $success;
}
/**
* Handle any post-clone tasks, like switching branches.
*
* @param Builder $builder
* @param string $cloneTo
* @param array $extra
*
* @return bool
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
$commitId = $this->getCommitId();
$chdir = 'cd "%s"';
if (empty($this->getEnvironment()) && !empty($commitId)) {
$cmd = $chdir . ' && git checkout %s --quiet';
$success = $builder->executeCommand($cmd, $cloneTo, $commitId);
}
// Always update the commit hash with the actual HEAD hash
if ($builder->executeCommand($chdir . ' && git rev-parse HEAD', $cloneTo)) {
$commitId = trim($builder->getLastOutput());
$this->setCommitId($commitId);
if ($builder->executeCommand($chdir . ' && git log -1 --pretty=format:%%s %s', $cloneTo, $commitId)) {
$this->setCommitMessage(trim($builder->getLastOutput()));
}
if ($builder->executeCommand($chdir . ' && git log -1 --pretty=format:%%ae %s', $cloneTo, $commitId)) {
$this->setCommitterEmail(trim($builder->getLastOutput()));
}
}
return $success;
}
}