Skip to content

Commit cc92345

Browse files
authored
Merge pull request composer#6475 from NamelessCoder/featurebranches
Add support for feature-branches setting
2 parents 1d9434a + 965f1f4 commit cc92345

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

doc/04-schema.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,4 +893,47 @@ Then "composer show -s" will give you `versions : * dev-latest-testing`.
893893

894894
Optional.
895895

896+
### feature-branches
897+
898+
If your feature branch names are other than the default names:
899+
900+
* master
901+
* trunk
902+
* default
903+
* develop
904+
905+
You can use this setting to declare the list of branch names which should be considered
906+
feature branches in your package. For example, if you do not use `develop` but rather use
907+
a name like `development` and have no `trunk` or `default` branches, you can change this
908+
setting to:
909+
910+
911+
```json
912+
{
913+
"feature-branches": ["master", "development"]
914+
}
915+
```
916+
917+
And Composer will consider those two branches (plus any numeric branches) as your only
918+
feature branches. This allows Composer to recognise when you create a new branch based
919+
on such a non-standard named feature branch, that your new branch is based on that "version".
920+
921+
In practice this allows you to have `my/package:dev-oddname` in your dependency
922+
section and still have `my/package:dev-branchofoddname` be recognised as compatible.
923+
Without overriding this configuration option, Composer would instead demand that you
924+
explicitly install `my/package:dev-oddname` or change the dependency to the explicit
925+
name `my/package:dev-branchofoddname`.
926+
927+
Particularly useful when combined with local repositories of type `path` where Composer
928+
will read the checked-out branch name to determine the version. Without this setting, if you
929+
used non-standard names, Composer would reject creating a symlink to the path and will
930+
instead force `git clone`. With the setting, symlink creation is allowed when your local
931+
branches are based on such non-standard named feature branches.
932+
933+
Note that this is only the case when your branch names are other than the above mentioned
934+
four default branch names. When you use default names you never need to declare this list -
935+
Composer will then detect your feature branches based on the default set of names.
936+
937+
Optional.
938+
896939
← [Command-line interface](03-cli.md) | [Repositories](05-repositories.md) →

res/composer-schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,13 @@
488488
}
489489
}
490490
},
491+
"feature-branches": {
492+
"type": ["array"],
493+
"description": "A set of string or regex patterns for non-numeric branch names that will be handled as feature branches. Overrides default feature branch names \"master\", \"trunk\", \"default\" and \"develop\" when declared.",
494+
"items": {
495+
"type": "string"
496+
}
497+
},
491498
"non-feature-branches": {
492499
"type": ["array"],
493500
"description": "A set of string or regex patterns for non-numeric branch names that will not be handled as feature branches.",

src/Composer/Package/Version/VersionGuesser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,19 @@ private function guessFeatureVersion(array $packageConfig, $version, array $bran
211211
$nonFeatureBranches = implode('|', $packageConfig['non-feature-branches']);
212212
}
213213

214+
$featureBranches = 'master|trunk|default|develop';
215+
if (!empty($packageConfig['feature-branches'])) {
216+
$featureBranches = implode('|', $packageConfig['feature-branches']);
217+
}
218+
214219
foreach ($branches as $candidate) {
215220
// return directly, if branch is configured to be non-feature branch
216221
if ($candidate === $branch && preg_match('{^(' . $nonFeatureBranches . ')$}', $candidate)) {
217222
break;
218223
}
219224

220225
// do not compare against other feature branches
221-
if ($candidate === $branch || !preg_match('{^(master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) {
226+
if ($candidate === $branch || !preg_match('{^(' . $featureBranches . '|\d+\..+)$}', $candidate, $match)) {
222227
continue;
223228
}
224229

tests/Composer/Test/Package/Version/VersionGuesserTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,51 @@ public function testGuessVersionReturnsData()
126126
$this->assertEquals($commitHash, $versionArray['commit']);
127127
}
128128

129+
public function testGuessVersionReadsAndRespectsFeatureBranchesConfigurationForArbitraryNaming()
130+
{
131+
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
132+
$anotherCommitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';
133+
134+
$executor = $this->getMockBuilder('\\Composer\\Util\\ProcessExecutor')
135+
->setMethods(array('execute'))
136+
->disableArgumentCloning()
137+
->disableOriginalConstructor()
138+
->getMock()
139+
;
140+
141+
$self = $this;
142+
143+
$executor
144+
->expects($this->at(0))
145+
->method('execute')
146+
->willReturnCallback(function ($command, &$output) use ($self, $commitHash, $anotherCommitHash) {
147+
$self->assertEquals('git branch --no-color --no-abbrev -v', $command);
148+
$output = " arbitrary $commitHash Commit message\n* current $anotherCommitHash Another message\n";
149+
150+
return 0;
151+
})
152+
;
153+
154+
$executor
155+
->expects($this->at(1))
156+
->method('execute')
157+
->willReturnCallback(function ($command, &$output, $path) use ($self, $anotherCommitHash) {
158+
$self->assertEquals('git rev-list arbitrary..current', $command);
159+
$output = "$anotherCommitHash\n";
160+
161+
return 0;
162+
})
163+
;
164+
165+
$config = new Config;
166+
$config->merge(array('repositories' => array('packagist' => false)));
167+
$guesser = new VersionGuesser($config, $executor, new VersionParser());
168+
$versionArray = $guesser->guessVersion(array('version' => 'self.version', 'feature-branches' => array('arbitrary')), 'dummy/path');
169+
170+
$this->assertEquals("dev-arbitrary", $versionArray['version']);
171+
$this->assertEquals($anotherCommitHash, $versionArray['commit']);
172+
}
173+
129174
public function testDetachedHeadBecomesDevHash()
130175
{
131176
$commitHash = '03a15d220da53c52eddd5f32ffca64a7b3801bea';

0 commit comments

Comments
 (0)