VersionGuesser.php 11 KB

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