VersionGuesser.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. * @return array versionData, 'version', 'pretty_version' and 'commit' keys
  55. */
  56. public function guessVersion(array $packageConfig, $path)
  57. {
  58. if (function_exists('proc_open')) {
  59. $versionData = $this->guessGitVersion($packageConfig, $path);
  60. if (null !== $versionData && null !== $versionData['version']) {
  61. return $versionData;
  62. }
  63. $versionData = $this->guessHgVersion($packageConfig, $path);
  64. if (null !== $versionData && null !== $versionData['version']) {
  65. return $versionData;
  66. }
  67. $versionData = $this->guessFossilVersion($packageConfig, $path);
  68. if (null !== $versionData && null !== $versionData['version']) {
  69. return $versionData;
  70. }
  71. return $this->guessSvnVersion($packageConfig, $path);
  72. }
  73. }
  74. private function guessGitVersion(array $packageConfig, $path)
  75. {
  76. GitUtil::cleanEnv();
  77. $commit = null;
  78. $version = null;
  79. $prettyVersion = null;
  80. $isDetached = false;
  81. // try to fetch current version from git branch
  82. if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output, $path)) {
  83. $branches = array();
  84. $isFeatureBranch = false;
  85. // find current branch and collect all branch names
  86. foreach ($this->process->splitLines($output) as $branch) {
  87. if ($branch && preg_match('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at \S+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
  88. if ($match[1] === '(no branch)' || substr($match[1], 0, 10) === '(detached ' || substr($match[1], 0, 17) === '(HEAD detached at') {
  89. $version = 'dev-' . $match[2];
  90. $prettyVersion = $version;
  91. $isFeatureBranch = true;
  92. $isDetached = true;
  93. } else {
  94. $version = $this->versionParser->normalizeBranch($match[1]);
  95. $prettyVersion = 'dev-' . $match[1];
  96. $isFeatureBranch = 0 === strpos($version, 'dev-');
  97. if ('9999999-dev' === $version) {
  98. $version = $prettyVersion;
  99. }
  100. }
  101. if ($match[2]) {
  102. $commit = $match[2];
  103. }
  104. }
  105. if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
  106. if (preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+) .*$}', $branch, $match)) {
  107. $branches[] = $match[1];
  108. }
  109. }
  110. }
  111. if ($isFeatureBranch) {
  112. // try to find the best (nearest) version branch to assume this feature's version
  113. $result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path);
  114. $version = $result['version'];
  115. $prettyVersion = $result['pretty_version'];
  116. }
  117. }
  118. if (!$version || $isDetached) {
  119. $result = $this->versionFromGitTags($path);
  120. if ($result) {
  121. $version = $result['version'];
  122. $prettyVersion = $result['pretty_version'];
  123. }
  124. }
  125. if (!$commit) {
  126. $command = 'git log --pretty="%H" -n1 HEAD';
  127. if (0 === $this->process->execute($command, $output, $path)) {
  128. $commit = trim($output) ?: null;
  129. }
  130. }
  131. return array('version' => $version, 'commit' => $commit, 'pretty_version' => $prettyVersion);
  132. }
  133. private function versionFromGitTags($path)
  134. {
  135. // try to fetch current version from git tags
  136. if (0 === $this->process->execute('git describe --exact-match --tags', $output, $path)) {
  137. try {
  138. $version = $this->versionParser->normalize(trim($output));
  139. return array('version' => $version, 'pretty_version' => trim($output));
  140. } catch (\Exception $e) {
  141. }
  142. }
  143. return null;
  144. }
  145. private function guessHgVersion(array $packageConfig, $path)
  146. {
  147. // try to fetch current version from hg branch
  148. if (0 === $this->process->execute('hg branch', $output, $path)) {
  149. $branch = trim($output);
  150. $version = $this->versionParser->normalizeBranch($branch);
  151. $isFeatureBranch = 0 === strpos($version, 'dev-');
  152. if ('9999999-dev' === $version) {
  153. $version = 'dev-' . $branch;
  154. }
  155. if (!$isFeatureBranch) {
  156. return array('version' => $version, 'commit' => null, 'pretty_version' => $version);
  157. }
  158. // re-use the HgDriver to fetch branches (this properly includes bookmarks)
  159. $driver = new HgDriver(array('url' => $path), new NullIO(), $this->config, $this->process);
  160. $branches = array_keys($driver->getBranches());
  161. // try to find the best (nearest) version branch to assume this feature's version
  162. $result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path);
  163. $result['commit'] = '';
  164. return $result;
  165. }
  166. }
  167. private function guessFeatureVersion(array $packageConfig, $version, array $branches, $scmCmdline, $path)
  168. {
  169. $prettyVersion = $version;
  170. // ignore feature branches if they have no branch-alias or self.version is used
  171. // and find the branch they came from to use as a version instead
  172. if ((isset($packageConfig['extra']['branch-alias']) && !isset($packageConfig['extra']['branch-alias'][$version]))
  173. || strpos(json_encode($packageConfig), '"self.version"')
  174. ) {
  175. $branch = preg_replace('{^dev-}', '', $version);
  176. $length = PHP_INT_MAX;
  177. $nonFeatureBranches = '';
  178. if (!empty($packageConfig['non-feature-branches'])) {
  179. $nonFeatureBranches = implode('|', $packageConfig['non-feature-branches']);
  180. }
  181. foreach ($branches as $candidate) {
  182. // return directly, if branch is configured to be non-feature branch
  183. if ($candidate === $branch && preg_match('{^(' . $nonFeatureBranches . ')$}', $candidate)) {
  184. break;
  185. }
  186. // do not compare against other feature branches
  187. if ($candidate === $branch || !preg_match('{^(master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) {
  188. continue;
  189. }
  190. $cmdLine = str_replace(array('%candidate%', '%branch%'), array($candidate, $branch), $scmCmdline);
  191. if (0 !== $this->process->execute($cmdLine, $output, $path)) {
  192. continue;
  193. }
  194. if (strlen($output) < $length) {
  195. $length = strlen($output);
  196. $version = $this->versionParser->normalizeBranch($candidate);
  197. $prettyVersion = 'dev-' . $match[1];
  198. if ('9999999-dev' === $version) {
  199. $version = $prettyVersion;
  200. }
  201. }
  202. }
  203. }
  204. return array('version' => $version, 'pretty_version' => $prettyVersion);
  205. }
  206. private function guessFossilVersion(array $packageConfig, $path)
  207. {
  208. $version = null;
  209. $prettyVersion = null;
  210. // try to fetch current version from fossil
  211. if (0 === $this->process->execute('fossil branch list', $output, $path)) {
  212. $branch = trim($output);
  213. $version = $this->versionParser->normalizeBranch($branch);
  214. $prettyVersion = 'dev-' . $branch;
  215. if ('9999999-dev' === $version) {
  216. $version = $prettyVersion;
  217. }
  218. }
  219. // try to fetch current version from fossil tags
  220. if (0 === $this->process->execute('fossil tag list', $output, $path)) {
  221. try {
  222. $version = $this->versionParser->normalize(trim($output));
  223. $prettyVersion = trim($output);
  224. } catch (\Exception $e) {
  225. }
  226. }
  227. return array('version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion);
  228. }
  229. private function guessSvnVersion(array $packageConfig, $path)
  230. {
  231. SvnUtil::cleanEnv();
  232. // try to fetch current version from svn
  233. if (0 === $this->process->execute('svn info --xml', $output, $path)) {
  234. $trunkPath = isset($packageConfig['trunk-path']) ? preg_quote($packageConfig['trunk-path'], '#') : 'trunk';
  235. $branchesPath = isset($packageConfig['branches-path']) ? preg_quote($packageConfig['branches-path'], '#') : 'branches';
  236. $tagsPath = isset($packageConfig['tags-path']) ? preg_quote($packageConfig['tags-path'], '#') : 'tags';
  237. $urlPattern = '#<url>.*/(' . $trunkPath . '|(' . $branchesPath . '|' . $tagsPath . ')/(.*))</url>#';
  238. if (preg_match($urlPattern, $output, $matches)) {
  239. if (isset($matches[2]) && ($branchesPath === $matches[2] || $tagsPath === $matches[2])) {
  240. // we are in a branches path
  241. $version = $this->versionParser->normalizeBranch($matches[3]);
  242. $prettyVersion = 'dev-' . $matches[3];
  243. if ('9999999-dev' === $version) {
  244. $version = $prettyVersion;
  245. }
  246. return array('version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion);
  247. }
  248. $prettyVersion = trim($matches[1]);
  249. $version = $this->versionParser->normalize($prettyVersion);
  250. return array('version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion);
  251. }
  252. }
  253. }
  254. }