/
VersionGuesser.php
422 lines (359 loc) · 15.9 KB
/
VersionGuesser.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
/*
* 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\Package\Version;
use Composer\Config;
use Composer\Pcre\Preg;
use Composer\Repository\Vcs\HgDriver;
use Composer\IO\NullIO;
use Composer\Semver\VersionParser as SemverVersionParser;
use Composer\Util\Git as GitUtil;
use Composer\Util\HttpDownloader;
use Composer\Util\ProcessExecutor;
use Composer\Util\Svn as SvnUtil;
/**
* Try to guess the current version number based on different VCS configuration.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Samuel Roze <samuel.roze@gmail.com>
*
* @phpstan-type Version array{version: string, commit: string|null, pretty_version: string|null, feature_version?: string|null, feature_pretty_version?: string|null}
*/
class VersionGuesser
{
/**
* @var Config
*/
private $config;
/**
* @var ProcessExecutor
*/
private $process;
/**
* @var SemverVersionParser
*/
private $versionParser;
/**
* @param Config $config
* @param ProcessExecutor $process
* @param SemverVersionParser $versionParser
*/
public function __construct(Config $config, ProcessExecutor $process, SemverVersionParser $versionParser)
{
$this->config = $config;
$this->process = $process;
$this->versionParser = $versionParser;
}
/**
* @param array<string, mixed> $packageConfig
* @param string $path Path to guess into
*
* @return array|null
* @phpstan-return Version|null
*/
public function guessVersion(array $packageConfig, $path)
{
if (!function_exists('proc_open')) {
return null;
}
$versionData = $this->guessGitVersion($packageConfig, $path);
if (null !== $versionData && null !== $versionData['version']) {
return $this->postprocess($versionData);
}
$versionData = $this->guessHgVersion($packageConfig, $path);
if (null !== $versionData && null !== $versionData['version']) {
return $this->postprocess($versionData);
}
$versionData = $this->guessFossilVersion($path);
if (null !== $versionData && null !== $versionData['version']) {
return $this->postprocess($versionData);
}
$versionData = $this->guessSvnVersion($packageConfig, $path);
if (null !== $versionData && null !== $versionData['version']) {
return $this->postprocess($versionData);
}
return null;
}
/**
* @param array $versionData
*
* @phpstan-param Version $versionData
*
* @return array
* @phpstan-return Version
*/
private function postprocess(array $versionData)
{
if (!empty($versionData['feature_version']) && $versionData['feature_version'] === $versionData['version'] && $versionData['feature_pretty_version'] === $versionData['pretty_version']) {
unset($versionData['feature_version'], $versionData['feature_pretty_version']);
}
if ('-dev' === substr($versionData['version'], -4) && Preg::isMatch('{\.9{7}}', $versionData['version'])) {
$versionData['pretty_version'] = Preg::replace('{(\.9{7})+}', '.x', $versionData['version']);
}
if (!empty($versionData['feature_version']) && '-dev' === substr($versionData['feature_version'], -4) && Preg::isMatch('{\.9{7}}', $versionData['feature_version'])) {
$versionData['feature_pretty_version'] = Preg::replace('{(\.9{7})+}', '.x', $versionData['feature_version']);
}
return $versionData;
}
/**
* @param array<string, mixed> $packageConfig
* @param string $path
*
* @return array{version: string|null, commit: string|null, pretty_version: string|null, feature_version?: string|null, feature_pretty_version?: string|null}
*/
private function guessGitVersion(array $packageConfig, $path)
{
GitUtil::cleanEnv();
$commit = null;
$version = null;
$prettyVersion = null;
$featureVersion = null;
$featurePrettyVersion = null;
$isDetached = false;
// try to fetch current version from git branch
if (0 === $this->process->execute(['git', 'branch', '-a', '--no-color', '--no-abbrev', '-v'], $output, $path)) {
$branches = array();
$isFeatureBranch = false;
// find current branch and collect all branch names
foreach ($this->process->splitLines($output) as $branch) {
if ($branch && Preg::isMatch('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at \S+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
if (
$match[1] === '(no branch)'
|| strpos($match[1], '(detached ') === 0
|| strpos($match[1], '(HEAD detached at') === 0
) {
$version = 'dev-' . $match[2];
$prettyVersion = $version;
$isFeatureBranch = true;
$isDetached = true;
} else {
$version = $this->versionParser->normalizeBranch($match[1]);
$prettyVersion = 'dev-' . $match[1];
$isFeatureBranch = $this->isFeatureBranch($packageConfig, $match[1]);
}
if ($match[2]) {
$commit = $match[2];
}
}
if ($branch && !Preg::isMatch('{^ *.+/HEAD }', $branch)) {
if (Preg::isMatch('{^(?:\* )? *((?:remotes/(?:origin|upstream)/)?[^\s/]+) *([a-f0-9]+) .*$}', $branch, $match)) {
$branches[] = $match[1];
}
}
}
if ($isFeatureBranch) {
$featureVersion = $version;
$featurePrettyVersion = $prettyVersion;
// try to find the best (nearest) version branch to assume this feature's version
$result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path);
$version = $result['version'];
$prettyVersion = $result['pretty_version'];
}
}
if (!$version || $isDetached) {
$result = $this->versionFromGitTags($path);
if ($result) {
$version = $result['version'];
$prettyVersion = $result['pretty_version'];
$featureVersion = null;
$featurePrettyVersion = null;
}
}
if (!$commit) {
$command = 'git log --pretty="%H" -n1 HEAD'.GitUtil::getNoShowSignatureFlag($this->process);
if (0 === $this->process->execute($command, $output, $path)) {
$commit = trim($output) ?: null;
}
}
if ($featureVersion) {
return array('version' => $version, 'commit' => $commit, 'pretty_version' => $prettyVersion, 'feature_version' => $featureVersion, 'feature_pretty_version' => $featurePrettyVersion);
}
return array('version' => $version, 'commit' => $commit, 'pretty_version' => $prettyVersion);
}
/**
* @param string $path
*
* @return array{version: string, pretty_version: string}|null
*/
private function versionFromGitTags($path)
{
// try to fetch current version from git tags
if (0 === $this->process->execute('git describe --exact-match --tags', $output, $path)) {
try {
$version = $this->versionParser->normalize(trim($output));
return array('version' => $version, 'pretty_version' => trim($output));
} catch (\Exception $e) {
}
}
return null;
}
/**
* @param array<string, mixed> $packageConfig
* @param string $path
*
* @return array{version: string|null, commit: ''|null, pretty_version: string|null, feature_version?: string|null, feature_pretty_version?: string|null}|null
*/
private function guessHgVersion(array $packageConfig, $path)
{
// try to fetch current version from hg branch
if (0 === $this->process->execute('hg branch', $output, $path)) {
$branch = trim($output);
$version = $this->versionParser->normalizeBranch($branch);
$isFeatureBranch = 0 === strpos($version, 'dev-');
if (VersionParser::DEFAULT_BRANCH_ALIAS === $version) {
return array('version' => $version, 'commit' => null, 'pretty_version' => 'dev-'.$branch);
}
if (!$isFeatureBranch) {
return array('version' => $version, 'commit' => null, 'pretty_version' => $version);
}
// re-use the HgDriver to fetch branches (this properly includes bookmarks)
$io = new NullIO();
$driver = new HgDriver(array('url' => $path), $io, $this->config, new HttpDownloader($io, $this->config), $this->process);
$branches = array_keys($driver->getBranches());
// try to find the best (nearest) version branch to assume this feature's version
$result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path);
$result['commit'] = '';
$result['feature_version'] = $version;
$result['feature_pretty_version'] = $version;
return $result;
}
return null;
}
/**
* @param array<string, mixed> $packageConfig
* @param string|null $version
* @param string[] $branches
* @param string $scmCmdline
* @param string $path
*
* @phpstan-param non-empty-string $scmCmdline
*
* @return array{version: string|null, pretty_version: string|null}
*/
private function guessFeatureVersion(array $packageConfig, $version, array $branches, $scmCmdline, $path)
{
$prettyVersion = $version;
// ignore feature branches if they have no branch-alias or self.version is used
// and find the branch they came from to use as a version instead
if (!isset($packageConfig['extra']['branch-alias'][$version])
|| strpos(json_encode($packageConfig), '"self.version"')
) {
$branch = Preg::replace('{^dev-}', '', $version);
$length = PHP_INT_MAX;
// return directly, if branch is configured to be non-feature branch
if (!$this->isFeatureBranch($packageConfig, $branch)) {
return array('version' => $version, 'pretty_version' => $prettyVersion);
}
// sort local branches first then remote ones
// and sort numeric branches below named ones, to make sure if the branch has the same distance from main and 1.10 and 1.9 for example, main is picked
// and sort using natural sort so that 1.10 will appear before 1.9
usort($branches, function ($a, $b) {
$aRemote = 0 === strpos($a, 'remotes/');
$bRemote = 0 === strpos($b, 'remotes/');
if ($aRemote !== $bRemote) {
return $aRemote ? 1 : -1;
}
return strnatcasecmp($b, $a);
});
foreach ($branches as $candidate) {
$candidateVersion = Preg::replace('{^remotes/\S+/}', '', $candidate);
// do not compare against itself or other feature branches
if ($candidate === $branch || $this->isFeatureBranch($packageConfig, $candidateVersion)) {
continue;
}
$cmdLine = str_replace(array('%candidate%', '%branch%'), array($candidate, $branch), $scmCmdline);
if (0 !== $this->process->execute($cmdLine, $output, $path)) {
continue;
}
if (strlen($output) < $length) {
$length = strlen($output);
$version = $this->versionParser->normalizeBranch($candidateVersion);
$prettyVersion = 'dev-' . $candidateVersion;
if ($length === 0) {
break;
}
}
}
}
return array('version' => $version, 'pretty_version' => $prettyVersion);
}
/**
* @param array<string, mixed> $packageConfig
* @param string|null $branchName
*
* @return bool
*/
private function isFeatureBranch(array $packageConfig, $branchName)
{
$nonFeatureBranches = '';
if (!empty($packageConfig['non-feature-branches'])) {
$nonFeatureBranches = implode('|', $packageConfig['non-feature-branches']);
}
return !Preg::isMatch('{^(' . $nonFeatureBranches . '|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}', $branchName, $match);
}
/**
* @param string $path
*
* @return array{version: string|null, commit: '', pretty_version: string|null}
*/
private function guessFossilVersion($path)
{
$version = null;
$prettyVersion = null;
// try to fetch current version from fossil
if (0 === $this->process->execute('fossil branch list', $output, $path)) {
$branch = trim($output);
$version = $this->versionParser->normalizeBranch($branch);
$prettyVersion = 'dev-' . $branch;
}
// try to fetch current version from fossil tags
if (0 === $this->process->execute('fossil tag list', $output, $path)) {
try {
$version = $this->versionParser->normalize(trim($output));
$prettyVersion = trim($output);
} catch (\Exception $e) {
}
}
return array('version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion);
}
/**
* @param array<string, mixed> $packageConfig
* @param string $path
*
* @return array{version: string, commit: '', pretty_version: string}|null
*/
private function guessSvnVersion(array $packageConfig, $path)
{
SvnUtil::cleanEnv();
// try to fetch current version from svn
if (0 === $this->process->execute('svn info --xml', $output, $path)) {
$trunkPath = isset($packageConfig['trunk-path']) ? preg_quote($packageConfig['trunk-path'], '#') : 'trunk';
$branchesPath = isset($packageConfig['branches-path']) ? preg_quote($packageConfig['branches-path'], '#') : 'branches';
$tagsPath = isset($packageConfig['tags-path']) ? preg_quote($packageConfig['tags-path'], '#') : 'tags';
$urlPattern = '#<url>.*/(' . $trunkPath . '|(' . $branchesPath . '|' . $tagsPath . ')/(.*))</url>#';
if (Preg::isMatch($urlPattern, $output, $matches)) {
if (isset($matches[2]) && ($branchesPath === $matches[2] || $tagsPath === $matches[2])) {
// we are in a branches path
$version = $this->versionParser->normalizeBranch($matches[3]);
$prettyVersion = 'dev-' . $matches[3];
return array('version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion);
}
$prettyVersion = trim($matches[1]);
if ($prettyVersion === 'trunk') {
$version = 'dev-trunk';
} else {
$version = $this->versionParser->normalize($prettyVersion);
}
return array('version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion);
}
}
return null;
}
}