#4828 Using version guesser to pull out the commit has for git repositories. The return value is now an array with version and commit values.
parent
4c0e163977
commit
f6899e5c38
|
@ -71,8 +71,11 @@ class RootPackageLoader extends ArrayLoader
|
||||||
// override with env var if available
|
// override with env var if available
|
||||||
if (getenv('COMPOSER_ROOT_VERSION')) {
|
if (getenv('COMPOSER_ROOT_VERSION')) {
|
||||||
$version = getenv('COMPOSER_ROOT_VERSION');
|
$version = getenv('COMPOSER_ROOT_VERSION');
|
||||||
|
$commit = null;
|
||||||
} else {
|
} else {
|
||||||
$version = $this->versionGuesser->guessVersion($config, $cwd ?: getcwd());
|
$versionData = $this->versionGuesser->guessVersion($config, $cwd ?: getcwd());
|
||||||
|
$version = $versionData['version'];
|
||||||
|
$commit = $versionData['commit'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$version) {
|
if (!$version) {
|
||||||
|
@ -81,6 +84,13 @@ class RootPackageLoader extends ArrayLoader
|
||||||
}
|
}
|
||||||
|
|
||||||
$config['version'] = $version;
|
$config['version'] = $version;
|
||||||
|
if($commit){
|
||||||
|
$config['source'] = array(
|
||||||
|
'type' => '',
|
||||||
|
'url' => '',
|
||||||
|
'reference' => $commit
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$realPackage = $package = parent::load($config, $class);
|
$realPackage = $package = parent::load($config, $class);
|
||||||
|
|
|
@ -44,9 +44,9 @@ class VersionGuesser
|
||||||
private $versionParser;
|
private $versionParser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Config $config
|
* @param Config $config
|
||||||
* @param ProcessExecutor $process
|
* @param ProcessExecutor $process
|
||||||
* @param VersionParser $versionParser
|
* @param VersionParser $versionParser
|
||||||
*/
|
*/
|
||||||
public function __construct(Config $config, ProcessExecutor $process, SemverVersionParser $versionParser)
|
public function __construct(Config $config, ProcessExecutor $process, SemverVersionParser $versionParser)
|
||||||
{
|
{
|
||||||
|
@ -56,20 +56,22 @@ class VersionGuesser
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $packageConfig
|
* @param array $packageConfig
|
||||||
* @param string $path Path to guess into
|
* @param string $path Path to guess into
|
||||||
|
*
|
||||||
|
* @return array versionData, 'version' and 'commit' keys
|
||||||
*/
|
*/
|
||||||
public function guessVersion(array $packageConfig, $path)
|
public function guessVersion(array $packageConfig, $path)
|
||||||
{
|
{
|
||||||
if (function_exists('proc_open')) {
|
if (function_exists('proc_open')) {
|
||||||
$version = $this->guessGitVersion($packageConfig, $path);
|
$versionData = $this->guessGitVersion($packageConfig, $path);
|
||||||
if (null !== $version) {
|
if (null !== $versionData) {
|
||||||
return $version;
|
return $versionData;
|
||||||
}
|
}
|
||||||
|
|
||||||
$version = $this->guessHgVersion($packageConfig, $path);
|
$versionData = $this->guessHgVersion($packageConfig, $path);
|
||||||
if (null !== $version) {
|
if (null !== $versionData) {
|
||||||
return $version;
|
return $versionData;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->guessSvnVersion($packageConfig, $path);
|
return $this->guessSvnVersion($packageConfig, $path);
|
||||||
|
@ -79,34 +81,34 @@ class VersionGuesser
|
||||||
private function guessGitVersion(array $packageConfig, $path)
|
private function guessGitVersion(array $packageConfig, $path)
|
||||||
{
|
{
|
||||||
GitUtil::cleanEnv();
|
GitUtil::cleanEnv();
|
||||||
|
$version = null;
|
||||||
// try to fetch current version from git tags
|
$commit = null;
|
||||||
if (0 === $this->process->execute('git describe --exact-match --tags', $output, $path)) {
|
$version = $this->versionFromGitTags($path);
|
||||||
try {
|
|
||||||
return $this->versionParser->normalize(trim($output));
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// try to fetch current version from git branch
|
// try to fetch current version from git branch
|
||||||
if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output, $path)) {
|
if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output, $path)) {
|
||||||
$branches = array();
|
$branches = array();
|
||||||
$isFeatureBranch = false;
|
$isFeatureBranch = false;
|
||||||
$version = null;
|
|
||||||
|
|
||||||
// find current branch and collect all branch names
|
// find current branch and collect all branch names
|
||||||
foreach ($this->process->splitLines($output) as $branch) {
|
foreach ($this->process->splitLines($output) as $branch) {
|
||||||
if ($branch && preg_match('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
|
if ($branch && preg_match('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
|
||||||
if ($match[1] === '(no branch)' || substr($match[1], 0, 10) === '(detached ') {
|
if (!$version) {
|
||||||
$version = 'dev-'.$match[2];
|
if ($match[1] === '(no branch)' || substr($match[1], 0, 10) === '(detached ') {
|
||||||
$isFeatureBranch = true;
|
$version = 'dev-' . $match[2];
|
||||||
} else {
|
$isFeatureBranch = true;
|
||||||
$version = $this->versionParser->normalizeBranch($match[1]);
|
} else {
|
||||||
$isFeatureBranch = 0 === strpos($version, 'dev-');
|
$version = $this->versionParser->normalizeBranch($match[1]);
|
||||||
if ('9999999-dev' === $version) {
|
$isFeatureBranch = 0 === strpos($version, 'dev-');
|
||||||
$version = 'dev-'.$match[1];
|
if ('9999999-dev' === $version) {
|
||||||
|
$version = 'dev-' . $match[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($match[2]) {
|
||||||
|
$commit = $match[2];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
|
if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
|
||||||
|
@ -116,15 +118,26 @@ class VersionGuesser
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$isFeatureBranch) {
|
if ($isFeatureBranch) {
|
||||||
return $version;
|
// try to find the best (nearest) version branch to assume this feature's version
|
||||||
|
$version = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array('version' => $version, 'commit' => $commit);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return $this->versionParser->normalize(trim($output));
|
||||||
|
} catch (\Exception $e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to find the best (nearest) version branch to assume this feature's version
|
|
||||||
$version = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path);
|
|
||||||
|
|
||||||
return $version;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function guessHgVersion(array $packageConfig, $path)
|
private function guessHgVersion(array $packageConfig, $path)
|
||||||
|
@ -136,7 +149,7 @@ class VersionGuesser
|
||||||
$isFeatureBranch = 0 === strpos($version, 'dev-');
|
$isFeatureBranch = 0 === strpos($version, 'dev-');
|
||||||
|
|
||||||
if ('9999999-dev' === $version) {
|
if ('9999999-dev' === $version) {
|
||||||
$version = 'dev-'.$branch;
|
$version = 'dev-' . $branch;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$isFeatureBranch) {
|
if (!$isFeatureBranch) {
|
||||||
|
@ -150,7 +163,7 @@ class VersionGuesser
|
||||||
// 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
|
||||||
$version = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path);
|
$version = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path);
|
||||||
|
|
||||||
return $version;
|
return array('version' => $version, 'commit' => '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,7 +202,7 @@ class VersionGuesser
|
||||||
$length = strlen($output);
|
$length = strlen($output);
|
||||||
$version = $this->versionParser->normalizeBranch($candidate);
|
$version = $this->versionParser->normalizeBranch($candidate);
|
||||||
if ('9999999-dev' === $version) {
|
if ('9999999-dev' === $version) {
|
||||||
$version = 'dev-'.$match[1];
|
$version = 'dev-' . $match[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,21 +221,23 @@ class VersionGuesser
|
||||||
$branchesPath = isset($packageConfig['branches-path']) ? preg_quote($packageConfig['branches-path'], '#') : 'branches';
|
$branchesPath = isset($packageConfig['branches-path']) ? preg_quote($packageConfig['branches-path'], '#') : 'branches';
|
||||||
$tagsPath = isset($packageConfig['tags-path']) ? preg_quote($packageConfig['tags-path'], '#') : 'tags';
|
$tagsPath = isset($packageConfig['tags-path']) ? preg_quote($packageConfig['tags-path'], '#') : 'tags';
|
||||||
|
|
||||||
$urlPattern = '#<url>.*/('.$trunkPath.'|('.$branchesPath.'|'. $tagsPath .')/(.*))</url>#';
|
$urlPattern = '#<url>.*/(' . $trunkPath . '|(' . $branchesPath . '|' . $tagsPath . ')/(.*))</url>#';
|
||||||
|
|
||||||
if (preg_match($urlPattern, $output, $matches)) {
|
if (preg_match($urlPattern, $output, $matches)) {
|
||||||
if (isset($matches[2]) && ($branchesPath === $matches[2] || $tagsPath === $matches[2])) {
|
if (isset($matches[2]) && ($branchesPath === $matches[2] || $tagsPath === $matches[2])) {
|
||||||
// we are in a branches path
|
// we are in a branches path
|
||||||
$version = $this->versionParser->normalizeBranch($matches[3]);
|
$version = $this->versionParser->normalizeBranch($matches[3]);
|
||||||
if ('9999999-dev' === $version) {
|
if ('9999999-dev' === $version) {
|
||||||
$version = 'dev-'.$matches[3];
|
$version = 'dev-' . $matches[3];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $version;
|
return array('version' => $version, 'commit' => '');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->versionParser->normalize(trim($matches[1]));
|
$version = $this->versionParser->normalize(trim($matches[1]));
|
||||||
|
return array('version' => $version, 'commit' => '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,7 +129,7 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!isset($package['version'])) {
|
if (!isset($package['version'])) {
|
||||||
$package['version'] = $this->versionGuesser->guessVersion($package, $path) ?: 'dev-master';
|
$package['version'] = $this->versionGuesser->guessVersion($package, $path)['version'] ?: 'dev-master';
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = '';
|
$output = '';
|
||||||
|
|
|
@ -25,6 +25,47 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGuessVersionReturnsData()
|
||||||
|
{
|
||||||
|
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
|
||||||
|
$anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
|
||||||
|
|
||||||
|
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
|
||||||
|
->setMethods(array('execute'))
|
||||||
|
->disableArgumentCloning()
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock()
|
||||||
|
;
|
||||||
|
|
||||||
|
$executor
|
||||||
|
->expects($this->at(0))
|
||||||
|
->method('execute')
|
||||||
|
->with('git describe --exact-match --tags')
|
||||||
|
->willReturn(1)
|
||||||
|
;
|
||||||
|
|
||||||
|
$self = $this;
|
||||||
|
|
||||||
|
$executor
|
||||||
|
->expects($this->at(1))
|
||||||
|
->method('execute')
|
||||||
|
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
|
||||||
|
$self->assertEquals('git branch --no-color --no-abbrev -v', $command);
|
||||||
|
$output = "* master $commitHash Commit message\n(no branch) $anotherCommitHash Commit message\n";
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
})
|
||||||
|
;
|
||||||
|
|
||||||
|
$config = new Config;
|
||||||
|
$config->merge(array('repositories' => array('packagist' => false)));
|
||||||
|
$guesser = new VersionGuesser($config, $executor, new VersionParser());
|
||||||
|
$versionArray = $guesser->guessVersion(array(), 'dummy/path');
|
||||||
|
|
||||||
|
$this->assertEquals("dev-master", $versionArray['version']);
|
||||||
|
$this->assertEquals($commitHash, $versionArray['commit']);
|
||||||
|
}
|
||||||
|
|
||||||
public function testDetachedHeadBecomesDevHash()
|
public function testDetachedHeadBecomesDevHash()
|
||||||
{
|
{
|
||||||
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
|
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
|
||||||
|
@ -59,7 +100,7 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase
|
||||||
$config = new Config;
|
$config = new Config;
|
||||||
$config->merge(array('repositories' => array('packagist' => false)));
|
$config->merge(array('repositories' => array('packagist' => false)));
|
||||||
$guesser = new VersionGuesser($config, $executor, new VersionParser());
|
$guesser = new VersionGuesser($config, $executor, new VersionParser());
|
||||||
$version = $guesser->guessVersion(array(), 'dummy/path');
|
$version = $guesser->guessVersion(array(), 'dummy/path')['version'];
|
||||||
|
|
||||||
$this->assertEquals("dev-$commitHash", $version);
|
$this->assertEquals("dev-$commitHash", $version);
|
||||||
}
|
}
|
||||||
|
@ -89,7 +130,7 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase
|
||||||
$config = new Config;
|
$config = new Config;
|
||||||
$config->merge(array('repositories' => array('packagist' => false)));
|
$config->merge(array('repositories' => array('packagist' => false)));
|
||||||
$guesser = new VersionGuesser($config, $executor, new VersionParser());
|
$guesser = new VersionGuesser($config, $executor, new VersionParser());
|
||||||
$version = $guesser->guessVersion(array(), 'dummy/path');
|
$version = $guesser->guessVersion(array(), 'dummy/path')['version'];
|
||||||
|
|
||||||
$this->assertEquals("2.0.5.0-alpha2", $version);
|
$this->assertEquals("2.0.5.0-alpha2", $version);
|
||||||
}
|
}
|
||||||
|
@ -130,7 +171,7 @@ class VersionGuesserTest extends \PHPUnit_Framework_TestCase
|
||||||
$config = new Config;
|
$config = new Config;
|
||||||
$config->merge(array('repositories' => array('packagist' => false)));
|
$config->merge(array('repositories' => array('packagist' => false)));
|
||||||
$guesser = new VersionGuesser($config, $executor, new VersionParser());
|
$guesser = new VersionGuesser($config, $executor, new VersionParser());
|
||||||
$version = $guesser->guessVersion(array(), 'dummy/path');
|
$version = $guesser->guessVersion(array(), 'dummy/path')['version'];
|
||||||
|
|
||||||
$this->assertEquals("dev-foo", $version);
|
$this->assertEquals("dev-foo", $version);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue