Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitDriver: try to fetch default branch from remote (#10687)
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
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composer\Test\Repository\Vcs; | ||
|
||
use Composer\Config; | ||
use Composer\IO\IOInterface; | ||
use Composer\Repository\Vcs\GitDriver; | ||
use Composer\Test\TestCase; | ||
use Composer\Util\Filesystem; | ||
use Composer\Util\Platform; | ||
|
||
class GitDriverTest extends TestCase | ||
{ | ||
/** @var Config */ | ||
private $config; | ||
/** @var string */ | ||
private $home; | ||
/** @var false|string */ | ||
private $networkEnv; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->home = self::getUniqueTmpDirectory(); | ||
$this->config = new Config(); | ||
$this->config->merge([ | ||
'config' => [ | ||
'home' => $this->home, | ||
], | ||
]); | ||
$this->networkEnv = Platform::getEnv('COMPOSER_DISABLE_NETWORK'); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
$fs = new Filesystem; | ||
$fs->removeDirectory($this->home); | ||
if ($this->networkEnv === false) { | ||
Platform::clearEnv('COMPOSER_DISABLE_NETWORK'); | ||
} else { | ||
Platform::putEnv('COMPOSER_DISABLE_NETWORK', $this->networkEnv); | ||
} | ||
} | ||
|
||
public function testGetRootIdentifierFromRemote(): void | ||
{ | ||
$process = $this->getProcessExecutorMock(); | ||
$io = $this->getMockBuilder(IOInterface::class)->getMock(); | ||
|
||
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getHttpDownloaderMock(), $process); | ||
|
||
$stdout = <<<GIT | ||
* remote origin | ||
Fetch URL: https://example.org/acme.git | ||
Push URL: https://example.org/acme.git | ||
HEAD branch: main | ||
Remote branches: | ||
1.10 tracked | ||
2.2 tracked | ||
main tracked | ||
GIT; | ||
|
||
$process | ||
->expects([[ | ||
'cmd' => 'git remote show origin', | ||
'stdout' => $stdout, | ||
]]); | ||
|
||
$this->assertSame('main', $driver->getRootIdentifier()); | ||
} | ||
|
||
public function testGetRootIdentifierFromLocalWithNetworkDisabled(): void | ||
{ | ||
Platform::putEnv('COMPOSER_DISABLE_NETWORK', '1'); | ||
|
||
$process = $this->getProcessExecutorMock(); | ||
$io = $this->getMockBuilder(IOInterface::class)->getMock(); | ||
|
||
$driver = new GitDriver(['url' => 'https://example.org/acme.git'], $io, $this->config, $this->getHttpDownloaderMock(), $process); | ||
|
||
$stdout = <<<GIT | ||
* main | ||
2.2 | ||
1.10 | ||
GIT; | ||
|
||
$process | ||
->expects([[ | ||
'cmd' => 'git branch --no-color', | ||
'stdout' => $stdout, | ||
]]); | ||
|
||
$this->assertSame('main', $driver->getRootIdentifier()); | ||
} | ||
} |