VersionGuesser.php 13 KB

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