Git.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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) {
  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) {
  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')) {
  212. return false;
  213. }
  214. // update the repo if it is a valid git repository
  215. if (is_dir($dir) && 0 === $this->process->execute('git rev-parse --git-dir', $output, $dir) && trim($output) === '.') {
  216. try {
  217. $commandCallable = function ($url) {
  218. $sanitizedUrl = preg_replace('{://([^@]+?):(.+?)@}', '://', $url);
  219. 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));
  220. };
  221. $this->runCommand($commandCallable, $url, $dir);
  222. } catch (\Exception $e) {
  223. $this->io->writeError('<error>Sync mirror failed: ' . $e->getMessage() . '</error>', true, IOInterface::DEBUG);
  224. return false;
  225. }
  226. return true;
  227. }
  228. // clean up directory and do a fresh clone into it
  229. $this->filesystem->removeDirectory($dir);
  230. $commandCallable = function ($url) use ($dir) {
  231. return sprintf('git clone --mirror %s %s', ProcessExecutor::escape($url), ProcessExecutor::escape($dir));
  232. };
  233. $this->runCommand($commandCallable, $url, $dir, true);
  234. return true;
  235. }
  236. public function fetchRefOrSyncMirror($url, $dir, $ref)
  237. {
  238. if ($this->checkRefIsInMirror($url, $dir, $ref)) {
  239. return true;
  240. }
  241. if ($this->syncMirror($url, $dir)) {
  242. return $this->checkRefIsInMirror($url, $dir, $ref);
  243. }
  244. return false;
  245. }
  246. private function checkRefIsInMirror($url, $dir, $ref)
  247. {
  248. if (is_dir($dir) && 0 === $this->process->execute('git rev-parse --git-dir', $output, $dir) && trim($output) === '.') {
  249. $escapedRef = ProcessExecutor::escape($ref.'^{commit}');
  250. $exitCode = $this->process->execute(sprintf('git rev-parse --quiet --verify %s', $escapedRef), $ignoredOutput, $dir);
  251. if ($exitCode === 0) {
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. private function isAuthenticationFailure($url, &$match)
  258. {
  259. if (!preg_match('{^(https?://)([^/]+)(.*)$}i', $url, $match)) {
  260. return false;
  261. }
  262. $authFailures = array(
  263. 'fatal: Authentication failed',
  264. 'remote error: Invalid username or password.',
  265. 'error: 401 Unauthorized',
  266. 'fatal: unable to access',
  267. 'fatal: could not read Username',
  268. );
  269. $errorOutput = $this->process->getErrorOutput();
  270. foreach ($authFailures as $authFailure) {
  271. if (strpos($errorOutput, $authFailure) !== false) {
  272. return true;
  273. }
  274. }
  275. return false;
  276. }
  277. public static function cleanEnv()
  278. {
  279. if (PHP_VERSION_ID < 50400 && ini_get('safe_mode') && false === strpos(ini_get('safe_mode_allowed_env_vars'), 'GIT_ASKPASS')) {
  280. 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');
  281. }
  282. // added in git 1.7.1, prevents prompting the user for username/password
  283. if (getenv('GIT_ASKPASS') !== 'echo') {
  284. putenv('GIT_ASKPASS=echo');
  285. unset($_SERVER['GIT_ASKPASS']);
  286. }
  287. // clean up rogue git env vars in case this is running in a git hook
  288. if (getenv('GIT_DIR')) {
  289. putenv('GIT_DIR');
  290. unset($_SERVER['GIT_DIR']);
  291. }
  292. if (getenv('GIT_WORK_TREE')) {
  293. putenv('GIT_WORK_TREE');
  294. unset($_SERVER['GIT_WORK_TREE']);
  295. }
  296. // Run processes with predictable LANGUAGE
  297. if (getenv('LANGUAGE') !== 'C') {
  298. putenv('LANGUAGE=C');
  299. }
  300. // clean up env for OSX, see https://github.com/composer/composer/issues/2146#issuecomment-35478940
  301. putenv("DYLD_LIBRARY_PATH");
  302. unset($_SERVER['DYLD_LIBRARY_PATH']);
  303. }
  304. public static function getGitHubDomainsRegex(Config $config)
  305. {
  306. return '(' . implode('|', array_map('preg_quote', $config->get('github-domains'))) . ')';
  307. }
  308. public static function getGitLabDomainsRegex(Config $config)
  309. {
  310. return '(' . implode('|', array_map('preg_quote', $config->get('gitlab-domains'))) . ')';
  311. }
  312. private function throwException($message, $url)
  313. {
  314. // git might delete a directory when it fails and php will not know
  315. clearstatcache();
  316. if (0 !== $this->process->execute('git --version', $ignoredOutput)) {
  317. 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()));
  318. }
  319. throw new \RuntimeException(Url::sanitize($message));
  320. }
  321. /**
  322. * Retrieves the current git version.
  323. *
  324. * @return string|null The git version number.
  325. */
  326. public function getVersion()
  327. {
  328. if (isset(self::$version)) {
  329. return self::$version;
  330. }
  331. if (0 !== $this->process->execute('git --version', $output)) {
  332. return;
  333. }
  334. if (preg_match('/^git version (\d+(?:\.\d+)+)/m', $output, $matches)) {
  335. return self::$version = $matches[1];
  336. }
  337. }
  338. }