1
0
Fork 0

Add detection of the current version from git if possible

pull/635/merge
Jordi Boggiano 2012-04-29 18:03:54 +02:00
parent 7b2d3ff6a2
commit adb4188e12
1 changed files with 23 additions and 2 deletions

View File

@ -14,6 +14,7 @@ namespace Composer\Package\Loader;
use Composer\Package\Version\VersionParser;
use Composer\Repository\RepositoryManager;
use Composer\Util\ProcessExecutor;
/**
* ArrayLoader built for the sole purpose of loading the root package
@ -25,10 +26,12 @@ use Composer\Repository\RepositoryManager;
class RootPackageLoader extends ArrayLoader
{
private $manager;
private $process;
public function __construct(RepositoryManager $manager, VersionParser $parser = null)
public function __construct(RepositoryManager $manager, VersionParser $parser = null, ProcessExecutor $process = null)
{
$this->manager = $manager;
$this->process = $process ?: new ProcessExecutor();
parent::__construct($parser);
}
@ -38,7 +41,25 @@ class RootPackageLoader extends ArrayLoader
$config['name'] = '__root__';
}
if (!isset($config['version'])) {
$config['version'] = '1.0.0';
$version = '1.0.0';
// try to fetch current version from git branch
if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output)) {
foreach ($this->process->splitLines($output) as $branch) {
if ($branch && preg_match('{^(?:\* ) *(?:[^/ ]+?/)?(\S+) *[a-f0-9]+ .*$}', $branch, $match)) {
$version = 'dev-'.$match[1];
if (isset($config['extra']['branch-alias'][$version])
&& substr($config['extra']['branch-alias'][$version], -4) === '-dev'
) {
$targetBranch = $config['extra']['branch-alias'][$version];
$normalized = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4));
$version = preg_replace('{(\.9{7})+}', '.x', $normalized);
}
}
}
}
$config['version'] = $version;
}
$package = parent::load($config);