Avoid blocking when guessing default branch name, fixes #9001

main
Jordi Boggiano 4 years ago
parent 09d7acee3c
commit e76fc2dc39
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC

@ -20,6 +20,7 @@ use Composer\Util\Git as GitUtil;
use Composer\Util\HttpDownloader; use Composer\Util\HttpDownloader;
use Composer\Util\ProcessExecutor; use Composer\Util\ProcessExecutor;
use Composer\Util\Svn as SvnUtil; use Composer\Util\Svn as SvnUtil;
use Composer\Util\Platform;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
@ -117,9 +118,17 @@ class VersionGuesser
*/ */
public function getDefaultBranchName($path) public function getDefaultBranchName($path)
{ {
GitUtil::cleanEnv(); if (version_compare(GitUtil::getVersion($this->process), '2.3.0-rc0', '>=')) {
if (0 === $this->process->execute('git remote show origin', $output, $path) || 0 === $this->process->execute('git remote show upstream', $output, $path)) { GitUtil::cleanEnv();
if (preg_match('{^ HEAD branch: (.+)$}m', $output, $match)) { $oldVal = getenv('GIT_SSH_COMMAND');
putenv("GIT_SSH_COMMAND=ssh".(Platform::isWindows() ? '.exe' : '')." -o StrictHostKeyChecking=yes");
$hasGitRemote = 0 === $this->process->execute('git remote show origin', $output, $path);
if ($oldVal) {
putenv("GIT_SSH_COMMAND=$oldVal");
} else {
putenv("GIT_SSH_COMMAND");
}
if ($hasGitRemote && preg_match('{^ HEAD branch: (.+)$}m', $output, $match)) {
return trim($match[1]); return trim($match[1]);
} }
} }
@ -287,7 +296,7 @@ class VersionGuesser
foreach ($branches as $candidate) { foreach ($branches as $candidate) {
// do not compare against itself or other feature branches // do not compare against itself or other feature branches
if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . ($defaultBranch ? '|'.preg_quote($defaultBranch) : '').'|master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) { if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . ($defaultBranch ? '|'.preg_quote($defaultBranch) : '').'|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}', $candidate, $match)) {
continue; continue;
} }

Loading…
Cancel
Save