Skip to content

Commit

Permalink
Add config option for update reference in path repository
Browse files Browse the repository at this point in the history
  • Loading branch information
sveneld authored and Sveneld committed Feb 6, 2022
1 parent 3ae1111 commit 321016c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 5 deletions.
19 changes: 19 additions & 0 deletions doc/05-repositories.md
Expand Up @@ -755,6 +755,25 @@ variables are parsed in both Windows and Linux/Mac notations. For example
> **Note:** Repository paths can also contain wildcards like `*` and `?`.
> For details, see the [PHP glob function](https://php.net/glob).
You can configure the behavior of reference building for a package. Field reference showing when something changed in package.
There are the following options:
- null - reference will be always null
- config - reference is building basing on hash of composer.json and repo config
- auto (used by default) - reference is building basing on hash of composer.json and repo config, but if the package folder contain git repository, then reference will be equal to the last commit hash in this folder
```json
{
"repositories": [
{
"type": "path",
"url": "../../packages/my-package",
"options": {
"reference": "auto"
}
}
]
}
```

## Disabling Packagist.org

You can disable the default Packagist.org repository by adding this to your
Expand Down
22 changes: 17 additions & 5 deletions src/Composer/Repository/PathRepository.php
Expand Up @@ -56,6 +56,13 @@
* "symlink": false
* }
* },
* {
* "type": "path",
* "url": "../../relative/path/to/package/",
* "options": {
* "reference": "none"
* }
* },
* ]
* @endcode
*
Expand All @@ -81,7 +88,7 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn

/**
* @var mixed[]
* @phpstan-var array{url: string, options?: array{symlink?: bool, relative?: bool, versions?: array<string, string>}}
* @phpstan-var array{url: string, options?: array{symlink?: bool, reference?: string, relative?: bool, versions?: array<string, string>}}
*/
private $repoConfig;

Expand All @@ -91,14 +98,14 @@ class PathRepository extends ArrayRepository implements ConfigurableRepositoryIn
private $process;

/**
* @var array{symlink?: bool, relative?: bool, versions?: array<string, string>}
* @var array{symlink?: bool, reference?: string, relative: bool, versions?: array<string, string>}
*/
private $options;

/**
* Initializes path repository.
*
* @param array{url?: string, options?: array{symlink?: bool, relative?: bool, versions?: array<string, string>}} $repoConfig
* @param array{url?: string, options?: array{symlink?: bool, reference?: string, relative?: bool, versions?: array<string, string>}} $repoConfig
* @param IOInterface $io
* @param Config $config
*/
Expand Down Expand Up @@ -171,8 +178,13 @@ protected function initialize()
$package['dist'] = array(
'type' => 'path',
'url' => $url,
'reference' => sha1($json . serialize($this->options)),
);
$reference = $this->options['reference'] ?? 'auto';
if ('none' === $reference) {
$package['dist']['reference'] = null;
} elseif ('config' === $reference || 'auto' === $reference) {
$package['dist']['reference'] = sha1($json . serialize($this->options));
}
$package['transport-options'] = $this->options;
unset($package['transport-options']['versions']);

Expand All @@ -193,7 +205,7 @@ protected function initialize()
}

$output = '';
if (is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H'.GitUtil::getNoShowSignatureFlag($this->process), $output, $path)) {
if ('auto' === $reference && is_dir($path . DIRECTORY_SEPARATOR . '.git') && 0 === $this->process->execute('git log -n1 --pretty=%H'.GitUtil::getNoShowSignatureFlag($this->process), $output, $path)) {
$package['dist']['reference'] = trim($output);
}

Expand Down
46 changes: 46 additions & 0 deletions tests/Composer/Test/Repository/PathRepositoryTest.php
Expand Up @@ -154,4 +154,50 @@ public function testUrlRemainsRelative()
$relativeUrl = str_replace(DIRECTORY_SEPARATOR, '/', $relativeUrl);
$this->assertSame($relativeUrl, $package->getDistUrl());
}

public function testReferenceNone()
{
$ioInterface = $this->getMockBuilder('Composer\IO\IOInterface')
->getMock();

$config = new \Composer\Config();

$options = array(
'reference' => 'none',
);
$repositoryUrl = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'Fixtures', 'path', '*'));
$repository = new PathRepository(array('url' => $repositoryUrl, 'options' => $options), $ioInterface, $config);
$packages = $repository->getPackages();

$this->assertGreaterThanOrEqual(2, $repository->count());

foreach ($packages as $package) {
$this->assertEquals($package->getDistReference(), null);
}
}

public function testReferenceConfig()
{
$ioInterface = $this->getMockBuilder('Composer\IO\IOInterface')
->getMock();

$config = new \Composer\Config();

$options = array(
'reference' => 'config',
'relative' => true,
);
$repositoryUrl = implode(DIRECTORY_SEPARATOR, array(__DIR__, 'Fixtures', 'path', '*'));
$repository = new PathRepository(array('url' => $repositoryUrl, 'options' => $options), $ioInterface, $config);
$packages = $repository->getPackages();

$this->assertGreaterThanOrEqual(2, $repository->count());

foreach ($packages as $package) {
$this->assertEquals(
$package->getDistReference(),
sha1(file_get_contents($package->getDistUrl() . '/composer.json') . serialize($options))
);
}
}
}

0 comments on commit 321016c

Please sign in to comment.