浏览代码

Add detection of the current version from git if possible

Jordi Boggiano 13 年之前
父节点
当前提交
adb4188e12
共有 1 个文件被更改,包括 23 次插入2 次删除
  1. 23 2
      src/Composer/Package/Loader/RootPackageLoader.php

+ 23 - 2
src/Composer/Package/Loader/RootPackageLoader.php

@@ -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);