forked from composer/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SvnDriver.php
407 lines (354 loc) · 12.5 KB
/
SvnDriver.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php declare(strict_types=1);
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Repository\Vcs;
use Composer\Cache;
use Composer\Config;
use Composer\Json\JsonFile;
use Composer\Pcre\Preg;
use Composer\Util\ProcessExecutor;
use Composer\Util\Filesystem;
use Composer\Util\Url;
use Composer\Util\Svn as SvnUtil;
use Composer\IO\IOInterface;
use Composer\Downloader\TransportException;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Till Klampaeckel <till@php.net>
*/
class SvnDriver extends VcsDriver
{
/** @var string */
protected $baseUrl;
/** @var array<int|string, string> Map of tag name to identifier */
protected $tags;
/** @var array<int|string, string> Map of branch name to identifier */
protected $branches;
/** @var ?string */
protected $rootIdentifier;
/** @var string|false */
protected $trunkPath = 'trunk';
/** @var string */
protected $branchesPath = 'branches';
/** @var string */
protected $tagsPath = 'tags';
/** @var string */
protected $packagePath = '';
/** @var bool */
protected $cacheCredentials = true;
/**
* @var \Composer\Util\Svn
*/
private $util;
/**
* @inheritDoc
*/
public function initialize(): void
{
$this->url = $this->baseUrl = rtrim(self::normalizeUrl($this->url), '/');
SvnUtil::cleanEnv();
if (isset($this->repoConfig['trunk-path'])) {
$this->trunkPath = $this->repoConfig['trunk-path'];
}
if (isset($this->repoConfig['branches-path'])) {
$this->branchesPath = $this->repoConfig['branches-path'];
}
if (isset($this->repoConfig['tags-path'])) {
$this->tagsPath = $this->repoConfig['tags-path'];
}
if (array_key_exists('svn-cache-credentials', $this->repoConfig)) {
$this->cacheCredentials = (bool) $this->repoConfig['svn-cache-credentials'];
}
if (isset($this->repoConfig['package-path'])) {
$this->packagePath = '/' . trim($this->repoConfig['package-path'], '/');
}
if (false !== ($pos = strrpos($this->url, '/' . $this->trunkPath))) {
$this->baseUrl = substr($this->url, 0, $pos);
}
$this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.Preg::replace('{[^a-z0-9.]}i', '-', Url::sanitize($this->baseUrl)));
$this->cache->setReadOnly($this->config->get('cache-read-only'));
$this->getBranches();
$this->getTags();
}
/**
* @inheritDoc
*/
public function getRootIdentifier(): string
{
return $this->rootIdentifier ?: $this->trunkPath;
}
/**
* @inheritDoc
*/
public function getUrl(): string
{
return $this->url;
}
/**
* @inheritDoc
*/
public function getSource(string $identifier): array
{
return array('type' => 'svn', 'url' => $this->baseUrl, 'reference' => $identifier);
}
/**
* @inheritDoc
*/
public function getDist(string $identifier): ?array
{
return null;
}
/**
* @inheritDoc
*/
protected function shouldCache(string $identifier): bool
{
return $this->cache && Preg::isMatch('{@\d+$}', $identifier);
}
/**
* @inheritDoc
*/
public function getComposerInformation(string $identifier): ?array
{
if (!isset($this->infoCache[$identifier])) {
if ($this->shouldCache($identifier) && $res = $this->cache->read($identifier.'.json')) {
return $this->infoCache[$identifier] = JsonFile::parseJson($res);
}
try {
$composer = $this->getBaseComposerInformation($identifier);
} catch (TransportException $e) {
$message = $e->getMessage();
if (stripos($message, 'path not found') === false && stripos($message, 'svn: warning: W160013') === false) {
throw $e;
}
// remember a not-existent composer.json
$composer = '';
}
if ($this->shouldCache($identifier)) {
$this->cache->write($identifier.'.json', json_encode($composer));
}
$this->infoCache[$identifier] = $composer;
}
return $this->infoCache[$identifier];
}
/**
* @param string $file
* @param string $identifier
*/
public function getFileContent(string $file, string $identifier): ?string
{
$identifier = '/' . trim($identifier, '/') . '/';
Preg::match('{^(.+?)(@\d+)?/$}', $identifier, $match);
if (!empty($match[2])) {
$path = $match[1];
$rev = $match[2];
} else {
$path = $identifier;
$rev = '';
}
try {
$resource = $path.$file;
$output = $this->execute('svn cat', $this->baseUrl . $resource . $rev);
if (!trim($output)) {
return null;
}
} catch (\RuntimeException $e) {
throw new TransportException($e->getMessage());
}
return $output;
}
/**
* @inheritDoc
*/
public function getChangeDate(string $identifier): ?\DateTimeImmutable
{
$identifier = '/' . trim($identifier, '/') . '/';
Preg::match('{^(.+?)(@\d+)?/$}', $identifier, $match);
if (!empty($match[2])) {
$path = $match[1];
$rev = $match[2];
} else {
$path = $identifier;
$rev = '';
}
$output = $this->execute('svn info', $this->baseUrl . $path . $rev);
foreach ($this->process->splitLines($output) as $line) {
if ($line && Preg::isMatch('{^Last Changed Date: ([^(]+)}', $line, $match)) {
return new \DateTimeImmutable($match[1], new \DateTimeZone('UTC'));
}
}
return null;
}
/**
* @inheritDoc
*/
public function getTags(): array
{
if (null === $this->tags) {
$tags = array();
if ($this->tagsPath !== false) {
$output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->tagsPath);
if ($output) {
foreach ($this->process->splitLines($output) as $line) {
$line = trim($line);
if ($line && Preg::isMatch('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
if (isset($match[1], $match[2]) && $match[2] !== './') {
$tags[rtrim($match[2], '/')] = $this->buildIdentifier(
'/' . $this->tagsPath . '/' . $match[2],
$match[1]
);
}
}
}
}
}
$this->tags = $tags;
}
return $this->tags;
}
/**
* @inheritDoc
*/
public function getBranches(): array
{
if (null === $this->branches) {
$branches = array();
if (false === $this->trunkPath) {
$trunkParent = $this->baseUrl . '/';
} else {
$trunkParent = $this->baseUrl . '/' . $this->trunkPath;
}
$output = $this->execute('svn ls --verbose', $trunkParent);
if ($output) {
foreach ($this->process->splitLines($output) as $line) {
$line = trim($line);
if ($line && Preg::isMatch('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
if (isset($match[1], $match[2]) && $match[2] === './') {
$branches['trunk'] = $this->buildIdentifier(
'/' . $this->trunkPath,
$match[1]
);
$this->rootIdentifier = $branches['trunk'];
break;
}
}
}
}
unset($output);
if ($this->branchesPath !== false) {
$output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->branchesPath);
if ($output) {
foreach ($this->process->splitLines(trim($output)) as $line) {
$line = trim($line);
if ($line && Preg::isMatch('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) {
if (isset($match[1], $match[2]) && $match[2] !== './') {
$branches[rtrim($match[2], '/')] = $this->buildIdentifier(
'/' . $this->branchesPath . '/' . $match[2],
$match[1]
);
}
}
}
}
}
$this->branches = $branches;
}
return $this->branches;
}
/**
* @inheritDoc
*/
public static function supports(IOInterface $io, Config $config, string $url, bool $deep = false): bool
{
$url = self::normalizeUrl($url);
if (Preg::isMatch('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) {
return true;
}
// proceed with deep check for local urls since they are fast to process
if (!$deep && !Filesystem::isLocalPath($url)) {
return false;
}
$process = new ProcessExecutor($io);
$exit = $process->execute(
"svn info --non-interactive -- ".ProcessExecutor::escape($url),
$ignoredOutput
);
if ($exit === 0) {
// This is definitely a Subversion repository.
return true;
}
// Subversion client 1.7 and older
if (false !== stripos($process->getErrorOutput(), 'authorization failed:')) {
// This is likely a remote Subversion repository that requires
// authentication. We will handle actual authentication later.
return true;
}
// Subversion client 1.8 and newer
if (false !== stripos($process->getErrorOutput(), 'Authentication failed')) {
// This is likely a remote Subversion or newer repository that requires
// authentication. We will handle actual authentication later.
return true;
}
return false;
}
/**
* An absolute path (leading '/') is converted to a file:// url.
*
* @param string $url
*
* @return string
*/
protected static function normalizeUrl(string $url): string
{
$fs = new Filesystem();
if ($fs->isAbsolutePath($url)) {
return 'file://' . strtr($url, '\\', '/');
}
return $url;
}
/**
* Execute an SVN command and try to fix up the process with credentials
* if necessary.
*
* @param string $command The svn command to run.
* @param string $url The SVN URL.
* @throws \RuntimeException
* @return string
*/
protected function execute(string $command, string $url): string
{
if (null === $this->util) {
$this->util = new SvnUtil($this->baseUrl, $this->io, $this->config, $this->process);
$this->util->setCacheCredentials($this->cacheCredentials);
}
try {
return $this->util->execute($command, $url);
} catch (\RuntimeException $e) {
if (null === $this->util->binaryVersion()) {
throw new \RuntimeException('Failed to load '.$this->url.', svn was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput());
}
throw new \RuntimeException(
'Repository '.$this->url.' could not be processed, '.$e->getMessage()
);
}
}
/**
* Build the identifier respecting "package-path" config option
*
* @param string $baseDir The path to trunk/branch/tag
* @param string $revision The revision mark to add to identifier
*
* @return string
*/
protected function buildIdentifier(string $baseDir, string $revision): string
{
return rtrim($baseDir, '/') . $this->packagePath . '/@' . $revision;
}
}