PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/Util/Git.php

http://github.com/composer/composer
PHP | 408 lines | 304 code | 69 blank | 35 comment | 79 complexity | c4abd7fb10483608b3f7022ed7a53310 MD5 | raw file
  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\Util;
  12. use Composer\Config;
  13. use Composer\IO\IOInterface;
  14. /**
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class Git
  18. {
  19. private static $version;
  20. /** @var IOInterface */
  21. protected $io;
  22. /** @var Config */
  23. protected $config;
  24. /** @var ProcessExecutor */
  25. protected $process;
  26. /** @var Filesystem */
  27. protected $filesystem;
  28. public function __construct(IOInterface $io, Config $config, ProcessExecutor $process, Filesystem $fs)
  29. {
  30. $this->io = $io;
  31. $this->config = $config;
  32. $this->process = $process;
  33. $this->filesystem = $fs;
  34. }
  35. public function runCommand($commandCallable, $url, $cwd, $initialClone = false)
  36. {
  37. // Ensure we are allowed to use this URL by config
  38. $this->config->prohibitUrlByConfig($url, $this->io);
  39. if ($initialClone) {
  40. $origCwd = $cwd;
  41. $cwd = null;
  42. }
  43. if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $url)) {
  44. throw new \InvalidArgumentException('The source URL ' . $url . ' is invalid, ssh URLs should have a port number after ":".' . "\n" . 'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.');
  45. }
  46. if (!$initialClone) {
  47. // capture username/password from URL if there is one and we have no auth configured yet
  48. $this->process->execute('git remote -v', $output, $cwd);
  49. if (preg_match('{^(?:composer|origin)\s+https?://(.+):(.+)@([^/]+)}im', $output, $match) && !$this->io->hasAuthentication($match[3])) {
  50. $this->io->setAuthentication($match[3], rawurldecode($match[1]), rawurldecode($match[2]));
  51. }
  52. }
  53. $protocols = $this->config->get('github-protocols');
  54. if (!is_array($protocols)) {
  55. throw new \RuntimeException('Config value "github-protocols" must be an array, got ' . gettype($protocols));
  56. }
  57. // public github, autoswitch protocols
  58. if (preg_match('{^(?:https?|git)://' . self::getGitHubDomainsRegex($this->config) . '/(.*)}', $url, $match)) {
  59. $messages = array();
  60. foreach ($protocols as $protocol) {
  61. if ('ssh' === $protocol) {
  62. $protoUrl = "git@" . $match[1] . ":" . $match[2];
  63. } else {
  64. $protoUrl = $protocol . "://" . $match[1] . "/" . $match[2];
  65. }
  66. if (0 === $this->process->execute(call_user_func($commandCallable, $protoUrl), $ignoredOutput, $cwd)) {
  67. return;
  68. }
  69. $messages[] = '- ' . $protoUrl . "\n" . preg_replace('#^#m', ' ', $this->process->getErrorOutput());
  70. if ($initialClone && isset($origCwd)) {
  71. $this->filesystem->removeDirectory($origCwd);
  72. }
  73. }
  74. // failed to checkout, first check git accessibility
  75. if (!$this->io->hasAuthentication($match[1]) && !$this->io->isInteractive()) {
  76. $this->throwException('Failed to clone ' . $url . ' via ' . implode(', ', $protocols) . ' protocols, aborting.' . "\n\n" . implode("\n", $messages), $url);
  77. }
  78. }
  79. // if we have a private github url and the ssh protocol is disabled then we skip it and directly fallback to https
  80. $bypassSshForGitHub = preg_match('{^git@' . self::getGitHubDomainsRegex($this->config) . ':(.+?)\.git$}i', $url) && !in_array('ssh', $protocols, true);
  81. $command = call_user_func($commandCallable, $url);
  82. $auth = null;
  83. if ($bypassSshForGitHub || 0 !== $this->process->execute($command, $ignoredOutput, $cwd)) {
  84. $errorMsg = $this->process->getErrorOutput();
  85. // private github repository without ssh key access, try https with auth
  86. if (preg_match('{^git@' . self::getGitHubDomainsRegex($this->config) . ':(.+?)\.git$}i', $url, $match)
  87. || preg_match('{^https?://' . self::getGitHubDomainsRegex($this->config) . '/(.*)}', $url, $match)
  88. ) {
  89. if (!$this->io->hasAuthentication($match[1])) {
  90. $gitHubUtil = new GitHub($this->io, $this->config, $this->process);
  91. $message = 'Cloning failed using an ssh key for authentication, enter your GitHub credentials to access private repos';
  92. if (!$gitHubUtil->authorizeOAuth($match[1]) && $this->io->isInteractive()) {
  93. $gitHubUtil->authorizeOAuthInteractively($match[1], $message);
  94. }
  95. }
  96. if ($this->io->hasAuthentication($match[1])) {
  97. $auth = $this->io->getAuthentication($match[1]);
  98. $authUrl = 'https://' . rawurlencode($auth['username']) . ':' . rawurlencode($auth['password']) . '@' . $match[1] . '/' . $match[2] . '.git';
  99. $command = call_user_func($commandCallable, $authUrl);
  100. if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) {
  101. return;
  102. }
  103. $errorMsg = $this->process->getErrorOutput();
  104. }
  105. } elseif (preg_match('{^https://(bitbucket\.org)/(.*)(\.git)?$}U', $url, $match)) { //bitbucket oauth
  106. $bitbucketUtil = new Bitbucket($this->io, $this->config, $this->process);
  107. if (!$this->io->hasAuthentication($match[1])) {
  108. $message = 'Enter your Bitbucket credentials to access private repos';
  109. if (!$bitbucketUtil->authorizeOAuth($match[1]) && $this->io->isInteractive()) {
  110. $bitbucketUtil->authorizeOAuthInteractively($match[1], $message);
  111. $accessToken = $bitbucketUtil->getToken();
  112. $this->io->setAuthentication($match[1], 'x-token-auth', $accessToken);
  113. }
  114. } else { //We're authenticating with a locally stored consumer.
  115. $auth = $this->io->getAuthentication($match[1]);
  116. //We already have an access_token from a previous request.
  117. if ($auth['username'] !== 'x-token-auth') {
  118. $accessToken = $bitbucketUtil->requestToken($match[1], $auth['username'], $auth['password']);
  119. if (! empty($accessToken)) {
  120. $this->io->setAuthentication($match[1], 'x-token-auth', $accessToken);
  121. }
  122. }
  123. }
  124. if ($this->io->hasAuthentication($match[1])) {
  125. $auth = $this->io->getAuthentication($match[1]);
  126. $authUrl = 'https://' . rawurlencode($auth['username']) . ':' . rawurlencode($auth['password']) . '@' . $match[1] . '/' . $match[2] . '.git';
  127. $command = call_user_func($commandCallable, $authUrl);
  128. if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) {
  129. return;
  130. }
  131. $errorMsg = $this->process->getErrorOutput();
  132. } else { // Falling back to ssh
  133. $sshUrl = 'git@bitbucket.org:' . $match[2] . '.git';
  134. $this->io->writeError(' No bitbucket authentication configured. Falling back to ssh.');
  135. $command = call_user_func($commandCallable, $sshUrl);
  136. if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) {
  137. return;
  138. }
  139. $errorMsg = $this->process->getErrorOutput();
  140. }
  141. } elseif (
  142. preg_match('{^(git)@' . self::getGitLabDomainsRegex($this->config) . ':(.+?\.git)$}i', $url, $match)
  143. || preg_match('{^(https?)://' . self::getGitLabDomainsRegex($this->config) . '/(.*)}', $url, $match)
  144. ) {
  145. if ($match[1] === 'git') {
  146. $match[1] = 'https';
  147. }
  148. if (!$this->io->hasAuthentication($match[2])) {
  149. $gitLabUtil = new GitLab($this->io, $this->config, $this->process);
  150. $message = 'Cloning failed, enter your GitLab credentials to access private repos';
  151. if (!$gitLabUtil->authorizeOAuth($match[2]) && $this->io->isInteractive()) {
  152. $gitLabUtil->authorizeOAuthInteractively($match[1], $match[2], $message);
  153. }
  154. }
  155. if ($this->io->hasAuthentication($match[2])) {
  156. $auth = $this->io->getAuthentication($match[2]);
  157. if ($auth['password'] === 'private-token' || $auth['password'] === 'oauth2' || $auth['password'] === 'gitlab-ci-token') {
  158. $authUrl = $match[1] . '://' . rawurlencode($auth['password']) . ':' . rawurlencode($auth['username']) . '@' . $match[2] . '/' . $match[3]; // swap username and password
  159. } else {
  160. $authUrl = $match[1] . '://' . rawurlencode($auth['username']) . ':' . rawurlencode($auth['password']) . '@' . $match[2] . '/' . $match[3];
  161. }
  162. $command = call_user_func($commandCallable, $authUrl);
  163. if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) {
  164. return;
  165. }
  166. $errorMsg = $this->process->getErrorOutput();
  167. }
  168. } elseif ($this->isAuthenticationFailure($url, $match)) { // private non-github/gitlab/bitbucket repo that failed to authenticate
  169. if (strpos($match[2], '@')) {
  170. list($authParts, $match[2]) = explode('@', $match[2], 2);
  171. }
  172. $storeAuth = false;
  173. if ($this->io->hasAuthentication($match[2])) {
  174. $auth = $this->io->getAuthentication($match[2]);
  175. } elseif ($this->io->isInteractive()) {
  176. $defaultUsername = null;
  177. if (isset($authParts) && $authParts) {
  178. if (false !== strpos($authParts, ':')) {
  179. list($defaultUsername, ) = explode(':', $authParts, 2);
  180. } else {
  181. $defaultUsername = $authParts;
  182. }
  183. }
  184. $this->io->writeError(' Authentication required (<info>' . $match[2] . '</info>):');
  185. $auth = array(
  186. 'username' => $this->io->ask(' Username: ', $defaultUsername),
  187. 'password' => $this->io->askAndHideAnswer(' Password: '),
  188. );
  189. $storeAuth = $this->config->get('store-auths');
  190. }
  191. if ($auth) {
  192. $authUrl = $match[1] . rawurlencode($auth['username']) . ':' . rawurlencode($auth['password']) . '@' . $match[2] . $match[3];
  193. $command = call_user_func($commandCallable, $authUrl);
  194. if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) {
  195. $this->io->setAuthentication($match[2], $auth['username'], $auth['password']);
  196. $authHelper = new AuthHelper($this->io, $this->config);
  197. $authHelper->storeAuth($match[2], $storeAuth);
  198. return;
  199. }
  200. $errorMsg = $this->process->getErrorOutput();
  201. }
  202. }
  203. if ($initialClone && isset($origCwd)) {
  204. $this->filesystem->removeDirectory($origCwd);
  205. }
  206. $this->throwException('Failed to execute ' . $command . "\n\n" . $errorMsg, $url);
  207. }
  208. }
  209. public function syncMirror($url, $dir)
  210. {
  211. if (getenv('COMPOSER_DISABLE_NETWORK') && getenv('COMPOSER_DISABLE_NETWORK') !== 'prime') {
  212. $this->io->writeError('<warning>Aborting git mirror sync of '.$url.' as network is disabled</warning>');
  213. return false;
  214. }
  215. // update the repo if it is a valid git repository
  216. if (is_dir($dir) && 0 === $this->process->execute('git rev-parse --git-dir', $output, $dir) && trim($output) === '.') {
  217. try {
  218. $commandCallable = function ($url) {
  219. $sanitizedUrl = preg_replace('{://([^@]+?):(.+?)@}', '://', $url);
  220. return sprintf('git remote set-url origin %s && git remote update --prune origin && git remote set-url origin %s', ProcessExecutor::escape($url), ProcessExecutor::escape($sanitizedUrl));
  221. };
  222. $this->runCommand($commandCallable, $url, $dir);
  223. } catch (\Exception $e) {
  224. $this->io->writeError('<error>Sync mirror failed: ' . $e->getMessage() . '</error>', true, IOInterface::DEBUG);
  225. return false;
  226. }
  227. return true;
  228. }
  229. // clean up directory and do a fresh clone into it
  230. $this->filesystem->removeDirectory($dir);
  231. $commandCallable = function ($url) use ($dir) {
  232. return sprintf('git clone --mirror %s %s', ProcessExecutor::escape($url), ProcessExecutor::escape($dir));
  233. };
  234. $this->runCommand($commandCallable, $url, $dir, true);
  235. return true;
  236. }
  237. public function fetchRefOrSyncMirror($url, $dir, $ref)
  238. {
  239. if ($this->checkRefIsInMirror($url, $dir, $ref)) {
  240. return true;
  241. }
  242. if ($this->syncMirror($url, $dir)) {
  243. return $this->checkRefIsInMirror($url, $dir, $ref);
  244. }
  245. return false;
  246. }
  247. private function checkRefIsInMirror($url, $dir, $ref)
  248. {
  249. if (is_dir($dir) && 0 === $this->process->execute('git rev-parse --git-dir', $output, $dir) && trim($output) === '.') {
  250. $escapedRef = ProcessExecutor::escape($ref.'^{commit}');
  251. $exitCode = $this->process->execute(sprintf('git rev-parse --quiet --verify %s', $escapedRef), $ignoredOutput, $dir);
  252. if ($exitCode === 0) {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. private function isAuthenticationFailure($url, &$match)
  259. {
  260. if (!preg_match('{^(https?://)([^/]+)(.*)$}i', $url, $match)) {
  261. return false;
  262. }
  263. $authFailures = array(
  264. 'fatal: Authentication failed',
  265. 'remote error: Invalid username or password.',
  266. 'error: 401 Unauthorized',
  267. 'fatal: unable to access',
  268. 'fatal: could not read Username',
  269. );
  270. $errorOutput = $this->process->getErrorOutput();
  271. foreach ($authFailures as $authFailure) {
  272. if (strpos($errorOutput, $authFailure) !== false) {
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. public static function cleanEnv()
  279. {
  280. if (PHP_VERSION_ID < 50400 && ini_get('safe_mode') && false === strpos(ini_get('safe_mode_allowed_env_vars'), 'GIT_ASKPASS')) {
  281. throw new \RuntimeException('safe_mode is enabled and safe_mode_allowed_env_vars does not contain GIT_ASKPASS, can not set env var. You can disable safe_mode with "-dsafe_mode=0" when running composer');
  282. }
  283. // added in git 1.7.1, prevents prompting the user for username/password
  284. if (getenv('GIT_ASKPASS') !== 'echo') {
  285. putenv('GIT_ASKPASS=echo');
  286. unset($_SERVER['GIT_ASKPASS']);
  287. }
  288. // clean up rogue git env vars in case this is running in a git hook
  289. if (getenv('GIT_DIR')) {
  290. putenv('GIT_DIR');
  291. unset($_SERVER['GIT_DIR']);
  292. }
  293. if (getenv('GIT_WORK_TREE')) {
  294. putenv('GIT_WORK_TREE');
  295. unset($_SERVER['GIT_WORK_TREE']);
  296. }
  297. // Run processes with predictable LANGUAGE
  298. if (getenv('LANGUAGE') !== 'C') {
  299. putenv('LANGUAGE=C');
  300. }
  301. // clean up env for OSX, see https://github.com/composer/composer/issues/2146#issuecomment-35478940
  302. putenv("DYLD_LIBRARY_PATH");
  303. unset($_SERVER['DYLD_LIBRARY_PATH']);
  304. }
  305. public static function getGitHubDomainsRegex(Config $config)
  306. {
  307. return '(' . implode('|', array_map('preg_quote', $config->get('github-domains'))) . ')';
  308. }
  309. public static function getGitLabDomainsRegex(Config $config)
  310. {
  311. return '(' . implode('|', array_map('preg_quote', $config->get('gitlab-domains'))) . ')';
  312. }
  313. private function throwException($message, $url)
  314. {
  315. // git might delete a directory when it fails and php will not know
  316. clearstatcache();
  317. if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
  318. throw new \RuntimeException(Url::sanitize('Failed to clone ' . $url . ', git was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()));
  319. }
  320. throw new \RuntimeException(Url::sanitize($message));
  321. }
  322. /**
  323. * Retrieves the current git version.
  324. *
  325. * @return string|null The git version number.
  326. */
  327. public function getVersion()
  328. {
  329. if (isset(self::$version)) {
  330. return self::$version;
  331. }
  332. if (0 !== $this->process->execute('git --version', $output)) {
  333. return;
  334. }
  335. if (preg_match('/^git version (\d+(?:\.\d+)+)/m', $output, $matches)) {
  336. return self::$version = $matches[1];
  337. }
  338. }
  339. }