Fix root package handling of default-branch
parent
923f198a1f
commit
6349764c2d
|
@ -110,6 +110,11 @@ class RootPackageLoader extends ArrayLoader
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$defaultBranch = $this->versionGuesser->getDefaultBranchName($cwd ?: getcwd());
|
||||||
|
if ($defaultBranch && $config['version'] === 'dev-'.$defaultBranch) {
|
||||||
|
$config['default-branch'] = true;
|
||||||
|
}
|
||||||
|
|
||||||
$realPackage = $package = parent::load($config, $class);
|
$realPackage = $package = parent::load($config, $class);
|
||||||
if ($realPackage instanceof AliasPackage) {
|
if ($realPackage instanceof AliasPackage) {
|
||||||
$realPackage = $package->getAliasOf();
|
$realPackage = $package->getAliasOf();
|
||||||
|
|
|
@ -110,6 +110,35 @@ class VersionGuesser
|
||||||
return $versionData;
|
return $versionData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to find name of default branch from VCS info
|
||||||
|
*
|
||||||
|
* @param string $path Path to guess into
|
||||||
|
*/
|
||||||
|
public function getDefaultBranchName($path)
|
||||||
|
{
|
||||||
|
GitUtil::cleanEnv();
|
||||||
|
if (0 === $this->process->execute('git remote show origin', $output, $path) || 0 === $this->process->execute('git remote show upstream', $output, $path)) {
|
||||||
|
if (preg_match('{^ HEAD branch: (.+)$}m', $output, $match)) {
|
||||||
|
return trim($match[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_dir($path.'/.git')) {
|
||||||
|
return 'master';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_dir($path.'/.hg')) {
|
||||||
|
return 'default';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_dir($path.'/.svn')) {
|
||||||
|
return 'trunk';
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private function guessGitVersion(array $packageConfig, $path)
|
private function guessGitVersion(array $packageConfig, $path)
|
||||||
{
|
{
|
||||||
GitUtil::cleanEnv();
|
GitUtil::cleanEnv();
|
||||||
|
@ -155,16 +184,8 @@ class VersionGuesser
|
||||||
$featureVersion = $version;
|
$featureVersion = $version;
|
||||||
$featurePrettyVersion = $prettyVersion;
|
$featurePrettyVersion = $prettyVersion;
|
||||||
|
|
||||||
// try to find name of default branch from git info
|
|
||||||
$defaultBranch = null;
|
|
||||||
if (0 === $this->process->execute('git remote show origin', $output) || 0 === $this->process->execute('git remote show upstream', $output)) {
|
|
||||||
if (preg_match('{^ HEAD branch: (.+)$}m', $output, $match)) {
|
|
||||||
$defaultBranch = trim($match[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// try to find the best (nearest) version branch to assume this feature's version
|
// 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, $defaultBranch);
|
$result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path);
|
||||||
$version = $result['version'];
|
$version = $result['version'];
|
||||||
$prettyVersion = $result['pretty_version'];
|
$prettyVersion = $result['pretty_version'];
|
||||||
}
|
}
|
||||||
|
@ -231,7 +252,7 @@ class VersionGuesser
|
||||||
$branches = array_keys($driver->getBranches());
|
$branches = array_keys($driver->getBranches());
|
||||||
|
|
||||||
// try to find the best (nearest) version branch to assume this feature's version
|
// 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, 'default');
|
$result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path);
|
||||||
$result['commit'] = '';
|
$result['commit'] = '';
|
||||||
$result['feature_version'] = $version;
|
$result['feature_version'] = $version;
|
||||||
$result['feature_pretty_version'] = $version;
|
$result['feature_pretty_version'] = $version;
|
||||||
|
@ -240,7 +261,7 @@ class VersionGuesser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function guessFeatureVersion(array $packageConfig, $version, array $branches, $scmCmdline, $path, $defaultBranch)
|
private function guessFeatureVersion(array $packageConfig, $version, array $branches, $scmCmdline, $path)
|
||||||
{
|
{
|
||||||
$prettyVersion = $version;
|
$prettyVersion = $version;
|
||||||
|
|
||||||
|
@ -262,6 +283,8 @@ class VersionGuesser
|
||||||
return array('version' => $version, 'pretty_version' => $prettyVersion);
|
return array('version' => $version, 'pretty_version' => $prettyVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$defaultBranch = $this->getDefaultBranchName($path);
|
||||||
|
|
||||||
foreach ($branches as $candidate) {
|
foreach ($branches as $candidate) {
|
||||||
// do not compare against itself or other feature branches
|
// do not compare against itself or other feature branches
|
||||||
if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . ($defaultBranch ? '|'.preg_quote($defaultBranch) : '').'|master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) {
|
if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . ($defaultBranch ? '|'.preg_quote($defaultBranch) : '').'|master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) {
|
||||||
|
|
|
@ -105,6 +105,8 @@ class RootPackageLoaderTest extends TestCase
|
||||||
'pretty_version' => '3.0-dev',
|
'pretty_version' => '3.0-dev',
|
||||||
'commit' => 'aabbccddee',
|
'commit' => 'aabbccddee',
|
||||||
));
|
));
|
||||||
|
$versionGuesser->getDefaultBranchName(Argument::cetera())
|
||||||
|
->willReturn('main');
|
||||||
$config = new Config;
|
$config = new Config;
|
||||||
$config->merge(array('repositories' => array('packagist' => false)));
|
$config->merge(array('repositories' => array('packagist' => false)));
|
||||||
$loader = new RootPackageLoader($manager->reveal(), $config, null, $versionGuesser->reveal());
|
$loader = new RootPackageLoader($manager->reveal(), $config, null, $versionGuesser->reveal());
|
||||||
|
@ -113,6 +115,28 @@ class RootPackageLoaderTest extends TestCase
|
||||||
$this->assertEquals('3.0-dev', $package->getPrettyVersion());
|
$this->assertEquals('3.0-dev', $package->getPrettyVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDefaultBranchIsSetForRootPackageInDefaultBranch()
|
||||||
|
{
|
||||||
|
// see #6845
|
||||||
|
$manager = $this->prophesize('\\Composer\\Repository\\RepositoryManager');
|
||||||
|
$versionGuesser = $this->prophesize('\\Composer\\Package\\Version\\VersionGuesser');
|
||||||
|
$versionGuesser->guessVersion(Argument::cetera())
|
||||||
|
->willReturn(array(
|
||||||
|
'name' => 'A',
|
||||||
|
'version' => 'dev-main',
|
||||||
|
'pretty_version' => 'dev-main',
|
||||||
|
'commit' => 'aabbccddee',
|
||||||
|
));
|
||||||
|
$versionGuesser->getDefaultBranchName(Argument::cetera())
|
||||||
|
->willReturn('main');
|
||||||
|
$config = new Config;
|
||||||
|
$config->merge(array('repositories' => array('packagist' => false)));
|
||||||
|
$loader = new RootPackageLoader($manager->reveal(), $config, null, $versionGuesser->reveal());
|
||||||
|
$package = $loader->load(array());
|
||||||
|
|
||||||
|
$this->assertTrue($package->isDefaultBranch());
|
||||||
|
}
|
||||||
|
|
||||||
public function testFeatureBranchPrettyVersion()
|
public function testFeatureBranchPrettyVersion()
|
||||||
{
|
{
|
||||||
if (!function_exists('proc_open')) {
|
if (!function_exists('proc_open')) {
|
||||||
|
@ -147,6 +171,17 @@ class RootPackageLoaderTest extends TestCase
|
||||||
$executor
|
$executor
|
||||||
->expects($this->at(1))
|
->expects($this->at(1))
|
||||||
->method('execute')
|
->method('execute')
|
||||||
|
->willReturnCallback(function ($command, &$output) use ($self) {
|
||||||
|
$self->assertEquals('git remote show origin', $command);
|
||||||
|
$output = " HEAD branch: master";
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
})
|
||||||
|
;
|
||||||
|
|
||||||
|
$executor
|
||||||
|
->expects($this->at(2))
|
||||||
|
->method('execute')
|
||||||
->willReturnCallback(function ($command, &$output) use ($self) {
|
->willReturnCallback(function ($command, &$output) use ($self) {
|
||||||
$self->assertEquals('git rev-list master..latest-production', $command);
|
$self->assertEquals('git rev-list master..latest-production', $command);
|
||||||
$output = "";
|
$output = "";
|
||||||
|
|
Loading…
Reference in New Issue