Skip to content

Commit

Permalink
GitDriver: try to fetch default branch from remote (composer#10687)
Browse files Browse the repository at this point in the history
The initial clone determined what the default branch of the cache git repository was. Changing it on the remote didn't have any impact on the local data. However, cloning it on a different machine would then store a different default branch on that machine. This could lead to different results for the same command on different machines.
  • Loading branch information
glaubinix authored and emahorvat52 committed Jan 18, 2023
1 parent daf1f82 commit d667973
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Composer/Repository/Vcs/GitDriver.php
Expand Up @@ -13,6 +13,7 @@
namespace Composer\Repository\Vcs;

use Composer\Pcre\Preg;
use Composer\Util\Platform;
use Composer\Util\ProcessExecutor;
use Composer\Util\Filesystem;
use Composer\Util\Url;
Expand Down Expand Up @@ -93,6 +94,17 @@ public function getRootIdentifier(): string
if (null === $this->rootIdentifier) {
$this->rootIdentifier = 'master';

if (!(bool) Platform::getEnv('COMPOSER_DISABLE_NETWORK')) {
try {
$this->process->execute('git remote show origin', $output, $this->repoDir);
if (Preg::isMatch('{^\s*HEAD branch:\s(.+)\s*$}m', $output, $matches)) {
return $this->rootIdentifier = $matches[1];
}
} catch (\Exception $e) {
$this->io->writeError('<error>Failed to fetch root identifier from remote: ' . $e->getMessage() . '</error>', true, IOInterface::DEBUG);
}
}

// select currently checked out branch if master is not available
$this->process->execute('git branch --no-color', $output, $this->repoDir);
$branches = $this->process->splitLines($output);
Expand Down

0 comments on commit d667973

Please sign in to comment.