VersionGuesser.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Package\Version;
  12. use Composer\Config;
  13. use Composer\Repository\Vcs\HgDriver;
  14. use Composer\IO\NullIO;
  15. use Composer\Semver\VersionParser as SemverVersionParser;
  16. use Composer\Util\Git as GitUtil;
  17. use Composer\Util\ProcessExecutor;
  18. use Composer\Util\Svn as SvnUtil;
  19. /**
  20. * Try to guess the current version number based on different VCS configuration.
  21. *
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. * @author Samuel Roze <samuel.roze@gmail.com>
  24. */
  25. class VersionGuesser
  26. {
  27. /**
  28. * @var Config
  29. */
  30. private $config;
  31. /**
  32. * @var ProcessExecutor
  33. */
  34. private $process;
  35. /**
  36. * @var SemverVersionParser
  37. */
  38. private $versionParser;
  39. /**
  40. * @param Config $config
  41. * @param ProcessExecutor $process
  42. * @param SemverVersionParser $versionParser
  43. */
  44. public function __construct(Config $config, ProcessExecutor $process, SemverVersionParser $versionParser)
  45. {
  46. $this->config = $config;
  47. $this->process = $process;
  48. $this->versionParser = $versionParser;
  49. }
  50. /**
  51. * @param array $packageConfig
  52. * @param string $path Path to guess into
  53. */
  54. public function guessVersion(array $packageConfig, $path)
  55. {
  56. if (function_exists('proc_open')) {
  57. $version = $this->guessGitVersion($packageConfig, $path);
  58. if (null !== $version) {
  59. return $version;
  60. }
  61. $version = $this->guessHgVersion($packageConfig, $path);
  62. if (null !== $version) {
  63. return $version;
  64. }
  65. return $this->guessSvnVersion($packageConfig, $path);
  66. }
  67. }
  68. private function guessGitVersion(array $packageConfig, $path)
  69. {
  70. GitUtil::cleanEnv();
  71. // try to fetch current version from git tags
  72. if (0 === $this->process->execute('git describe --exact-match --tags', $output, $path)) {
  73. try {
  74. return $this->versionParser->normalize(trim($output));
  75. } catch (\Exception $e) {
  76. }
  77. }
  78. // try to fetch current version from git branch
  79. if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output, $path)) {
  80. $branches = array();
  81. $isFeatureBranch = false;
  82. $version = null;
  83. // find current branch and collect all branch names
  84. foreach ($this->process->splitLines($output) as $branch) {
  85. if ($branch && preg_match('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
  86. if ($match[1] === '(no branch)' || substr($match[1], 0, 10) === '(detached ') {
  87. $version = 'dev-'.$match[2];
  88. $isFeatureBranch = true;
  89. } else {
  90. $version = $this->versionParser->normalizeBranch($match[1]);
  91. $isFeatureBranch = 0 === strpos($version, 'dev-');
  92. if ('9999999-dev' === $version) {
  93. $version = 'dev-'.$match[1];
  94. }
  95. }
  96. }
  97. if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
  98. if (preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
  99. $branches[] = $match[1];
  100. }
  101. }
  102. }
  103. if (!$isFeatureBranch) {
  104. return $version;
  105. }
  106. // try to find the best (nearest) version branch to assume this feature's version
  107. $version = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path);
  108. return $version;
  109. }
  110. }
  111. private function guessHgVersion(array $packageConfig, $path)
  112. {
  113. // try to fetch current version from hg branch
  114. if (0 === $this->process->execute('hg branch', $output, $path)) {
  115. $branch = trim($output);
  116. $version = $this->versionParser->normalizeBranch($branch);
  117. $isFeatureBranch = 0 === strpos($version, 'dev-');
  118. if ('9999999-dev' === $version) {
  119. $version = 'dev-'.$branch;
  120. }
  121. if (!$isFeatureBranch) {
  122. return $version;
  123. }
  124. // re-use the HgDriver to fetch branches (this properly includes bookmarks)
  125. $driver = new HgDriver(array('url' => $path), new NullIO(), $this->config, $this->process);
  126. $branches = array_keys($driver->getBranches());
  127. // try to find the best (nearest) version branch to assume this feature's version
  128. $version = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path);
  129. return $version;
  130. }
  131. }
  132. private function guessFeatureVersion(array $packageConfig, $version, array $branches, $scmCmdline, $path)
  133. {
  134. // ignore feature branches if they have no branch-alias or self.version is used
  135. // and find the branch they came from to use as a version instead
  136. if ((isset($packageConfig['extra']['branch-alias']) && !isset($packageConfig['extra']['branch-alias'][$version]))
  137. || strpos(json_encode($packageConfig), '"self.version"')
  138. ) {
  139. $branch = preg_replace('{^dev-}', '', $version);
  140. $length = PHP_INT_MAX;
  141. $nonFeatureBranches = '';
  142. if (!empty($packageConfig['non-feature-branches'])) {
  143. $nonFeatureBranches = implode('|', $packageConfig['non-feature-branches']);
  144. }
  145. foreach ($branches as $candidate) {
  146. // return directly, if branch is configured to be non-feature branch
  147. if ($candidate === $branch && preg_match('{^(' . $nonFeatureBranches . ')$}', $candidate)) {
  148. return $version;
  149. }
  150. // do not compare against other feature branches
  151. if ($candidate === $branch || !preg_match('{^(master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) {
  152. continue;
  153. }
  154. $cmdLine = str_replace(array('%candidate%', '%branch%'), array($candidate, $branch), $scmCmdline);
  155. if (0 !== $this->process->execute($cmdLine, $output, $path)) {
  156. continue;
  157. }
  158. if (strlen($output) < $length) {
  159. $length = strlen($output);
  160. $version = $this->versionParser->normalizeBranch($candidate);
  161. if ('9999999-dev' === $version) {
  162. $version = 'dev-'.$match[1];
  163. }
  164. }
  165. }
  166. }
  167. return $version;
  168. }
  169. private function guessSvnVersion(array $packageConfig, $path)
  170. {
  171. SvnUtil::cleanEnv();
  172. // try to fetch current version from svn
  173. if (0 === $this->process->execute('svn info --xml', $output, $path)) {
  174. $trunkPath = isset($packageConfig['trunk-path']) ? preg_quote($packageConfig['trunk-path'], '#') : 'trunk';
  175. $branchesPath = isset($packageConfig['branches-path']) ? preg_quote($packageConfig['branches-path'], '#') : 'branches';
  176. $tagsPath = isset($packageConfig['tags-path']) ? preg_quote($packageConfig['tags-path'], '#') : 'tags';
  177. $urlPattern = '#<url>.*/('.$trunkPath.'|('.$branchesPath.'|'. $tagsPath .')/(.*))</url>#';
  178. if (preg_match($urlPattern, $output, $matches)) {
  179. if (isset($matches[2]) && ($branchesPath === $matches[2] || $tagsPath === $matches[2])) {
  180. // we are in a branches path
  181. $version = $this->versionParser->normalizeBranch($matches[3]);
  182. if ('9999999-dev' === $version) {
  183. $version = 'dev-'.$matches[3];
  184. }
  185. return $version;
  186. }
  187. return $this->versionParser->normalize(trim($matches[1]));
  188. }
  189. }
  190. }
  191. }