-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathHgBuild.php
More file actions
129 lines (109 loc) · 3.13 KB
/
Copy pathHgBuild.php
File metadata and controls
129 lines (109 loc) · 3.13 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
<?php
namespace PHPCensor\Model\Build;
use Exception;
use PHPCensor\Builder;
use PHPCensor\Common\Application\ConfigurationInterface;
use PHPCensor\Model\Build;
use PHPCensor\StoreRegistry;
/**
* Mercurial Build Model
*
* @package PHP Censor
* @subpackage Application
*
* @author Pavel Gopanenko <pavelgopanenko@gmail.com>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class HgBuild extends Build
{
protected ConfigurationInterface $configuration;
public function __construct(
ConfigurationInterface $configuration,
StoreRegistry $storeRegistry,
array $initialData = []
) {
parent::__construct($storeRegistry, $initialData);
$this->configuration = $configuration;
}
/**
* 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 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) {
$builder->logFailure('Failed to clone remote hg repository.');
return false;
}
return $this->handleConfig($builder, $buildPath);
}
/**
* Use a HTTP-based hg clone.
*
* @param string $cloneTo
*
* @return bool
*/
protected function cloneByHttp(Builder $builder, $cloneTo)
{
return $builder->executeCommand('hg clone %s "%s" -r %s', $this->getCloneUrl(), $cloneTo, \escapeshellarg($this->getBranch()));
}
/**
* Use an SSH-based hg clone.
*
* @param string $cloneTo
*
* @return bool
*/
protected function cloneBySsh(Builder $builder, $cloneTo)
{
$keyFile = $this->writeSshKey();
// Do the hg clone:
$cmd = 'hg clone --ssh "ssh -i ' . $keyFile . '" %s "%s" -r %s';
$success = $builder->executeCommand($cmd, $this->getCloneUrl(), $cloneTo, \escapeshellarg($this->getBranch()));
if ($success) {
$success = $this->postCloneSetup($builder, $cloneTo);
}
// Remove the key file:
\unlink($keyFile);
return $success;
}
/**
* Handle post-clone tasks (switching branch, etc.)
*
* @param string $cloneTo
*
* @return bool
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
$commitId = $this->getCommitId();
// Allow switching to a specific branch:
if (!empty($commitId)) {
$cmd = 'cd "%s" && hg checkout %s';
$success = $builder->executeCommand($cmd, $cloneTo, \escapeshellarg($this->getBranch()));
}
return $success;
}
}