Skip to content

Commit f7f14e7

Browse files
committed
Made sure it's still easy to add new branches to configuration.
1 parent 5e5e66e commit f7f14e7

16 files changed

+167
-42
lines changed

docs/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ To resolve this issue we give GitVersion a hint to how we normally do our branch
277277

278278
This means that we will never bother to evaluate pull requests as merge base options, being explicit like this helps GitVersion be much faster too.
279279

280+
### is-source-branch-for
281+
The reverse of the above setting. This property was introduced to keep it easy to extend GitVersion's config.
282+
283+
When you add a new branch type this allows you to specify both source-branches for the new branch, and also add the new branch to existing branch configurations. For example if you create a new branch called `unstable` you could set a value of `['release', 'master', 'feature']` etc.
280284

281285
### branches
282286
The header for all the individual branch configuration.

src/GitVersionCore.Tests/ConfigProviderTests.CanWriteOutEffectiveConfiguration.approved.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ branches:
4646
source-branches:
4747
- develop
4848
- master
49+
- support
50+
- release
4951
tracks-release-branches: false
5052
is-release-branch: true
5153
is-mainline: false
@@ -94,6 +96,7 @@ branches:
9496
source-branches:
9597
- develop
9698
- master
99+
- support
97100
tracks-release-branches: false
98101
is-release-branch: false
99102
is-mainline: false

src/GitVersionCore.Tests/GitRepoMetadataProviderTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public void FindsCorrectMergeBaseForForwardMerge()
5252

5353
var develop = fixture.Repository.FindBranch("develop");
5454
var release = fixture.Repository.FindBranch("release-2.0.0");
55-
var releaseBranchMergeBase = new GitRepoMetadataProvider(fixture.Repository)
55+
var releaseBranchMergeBase = new GitRepoMetadataProvider(fixture.Repository, new Config())
5656
.FindMergeBase(release, develop);
5757

58-
var developMergeBase = new GitRepoMetadataProvider(fixture.Repository)
58+
var developMergeBase = new GitRepoMetadataProvider(fixture.Repository, new Config())
5959
.FindMergeBase(develop, release);
6060

6161
fixture.Repository.DumpGraph(Console.WriteLine);
@@ -109,10 +109,10 @@ public void FindsCorrectMergeBaseForForwardMergeMovesOn()
109109

110110
var develop = fixture.Repository.FindBranch("develop");
111111
var release = fixture.Repository.FindBranch("release-2.0.0");
112-
var releaseBranchMergeBase = new GitRepoMetadataProvider(fixture.Repository)
112+
var releaseBranchMergeBase = new GitRepoMetadataProvider(fixture.Repository, new Config())
113113
.FindMergeBase(release, develop);
114114

115-
var developMergeBase = new GitRepoMetadataProvider(fixture.Repository)
115+
var developMergeBase = new GitRepoMetadataProvider(fixture.Repository, new Config())
116116
.FindMergeBase(develop, release);
117117

118118
fixture.Repository.DumpGraph(Console.WriteLine);
@@ -184,10 +184,10 @@ public void FindsCorrectMergeBaseForMultipleForwardMerges()
184184

185185
var develop = fixture.Repository.FindBranch("develop");
186186
var release = fixture.Repository.FindBranch("release-2.0.0");
187-
var releaseBranchMergeBase = new GitRepoMetadataProvider(fixture.Repository)
187+
var releaseBranchMergeBase = new GitRepoMetadataProvider(fixture.Repository, new Config())
188188
.FindMergeBase(release, develop);
189189

190-
var developMergeBase = new GitRepoMetadataProvider(fixture.Repository)
190+
var developMergeBase = new GitRepoMetadataProvider(fixture.Repository, new Config())
191191
.FindMergeBase(develop, release);
192192

193193
fixture.Repository.DumpGraph(Console.WriteLine);

src/GitVersionCore.Tests/GitVersionContextTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using LibGit2Sharp;
66
using NUnit.Framework;
77
using Shouldly;
8+
using System.Collections.Generic;
89

910
public class GitVersionContextTests
1011
{
@@ -98,8 +99,8 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
9899
VersioningMode = VersioningMode.ContinuousDelivery,
99100
Branches =
100101
{
101-
{ "release/latest", new BranchConfig { Increment = IncrementStrategy.None, Regex = "release/latest", SourceBranches = new string[0] } },
102-
{ "release", new BranchConfig { Increment = IncrementStrategy.Patch, Regex = "releases?[/-]", SourceBranches = new string[0] } }
102+
{ "release/latest", new BranchConfig { Increment = IncrementStrategy.None, Regex = "release/latest", SourceBranches = new List<string>() } },
103+
{ "release", new BranchConfig { Increment = IncrementStrategy.Patch, Regex = "releases?[/-]", SourceBranches = new List<string>() } }
103104
}
104105
}.ApplyDefaults();
105106

src/GitVersionCore.Tests/IntegrationTests/DevelopScenarios.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using GitVersionCore.Tests;
44
using LibGit2Sharp;
55
using NUnit.Framework;
6+
using System.Collections.Generic;
67

78
[TestFixture]
89
public class DevelopScenarios
@@ -65,7 +66,7 @@ public void CanChangeDevelopTagViaConfig()
6566
"develop", new BranchConfig
6667
{
6768
Tag = "alpha",
68-
SourceBranches = new string[0]
69+
SourceBranches = new List<string>()
6970
}
7071
}
7172
}

src/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ public void BranchCreatedAfterFastForwardMergeShouldInheritCorrectly()
4242
{
4343
Branches =
4444
{
45-
{ "unstable", new BranchConfig {
46-
Increment = IncrementStrategy.Minor, Regex = "unstable", SourceBranches = new string[0]
47-
} }
45+
{
46+
"unstable",
47+
new BranchConfig
48+
{
49+
Increment = IncrementStrategy.Minor,
50+
Regex = "unstable",
51+
SourceBranches = new List<string>(),
52+
IsSourceBranchFor = new [] { "feature" }
53+
}
54+
}
4855
}
4956
};
5057

src/GitVersionCore.Tests/IntegrationTests/OtherScenarios.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GitVersion;
66
using LibGit2Sharp;
77
using NUnit.Framework;
8+
using System.Collections.Generic;
89

910
[TestFixture]
1011
public class OtherScenarios
@@ -57,7 +58,7 @@ public void AllowHavingMainInsteadOfMaster()
5758
Increment = IncrementStrategy.Patch,
5859
PreventIncrementOfMergedBranchVersion = true,
5960
TrackMergeTarget = false,
60-
SourceBranches = new string[0]
61+
SourceBranches = new List<string>()
6162
});
6263

6364
using (var fixture = new EmptyRepositoryFixture())

src/GitVersionCore.Tests/IntegrationTests/ReleaseBranchScenarios.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public void CommitOnDevelop_AfterReleaseBranchMergeToDevelop_ShouldNotResetCount
418418
fixture.AssertFullSemver(config, "2.0.0-beta.5");
419419
}
420420
}
421-
421+
422422
public void ReleaseBranchShouldUseBranchNameVersionDespiteBumpInPreviousCommit()
423423
{
424424
using (var fixture = new EmptyRepositoryFixture())
@@ -463,4 +463,53 @@ public void ReleaseBranchedAtCommitWithSemverMessageShouldUseBranchNameVersion()
463463
fixture.AssertFullSemver("2.0.0-beta.1+1");
464464
}
465465
}
466+
467+
[Test]
468+
public void FeatureFromReleaseBranch_ShouldNotResetCount()
469+
{
470+
var config = new Config
471+
{
472+
VersioningMode = VersioningMode.ContinuousDeployment
473+
};
474+
475+
using (var fixture = new EmptyRepositoryFixture())
476+
{
477+
fixture.Repository.MakeACommit("initial");
478+
fixture.Repository.CreateBranch("develop");
479+
Commands.Checkout(fixture.Repository, "develop");
480+
fixture.Repository.CreateBranch("release-2.0.0");
481+
Commands.Checkout(fixture.Repository, "release-2.0.0");
482+
fixture.AssertFullSemver(config, "2.0.0-beta.0");
483+
484+
// Make some commits on release
485+
fixture.Repository.MakeCommits(10);
486+
fixture.AssertFullSemver(config, "2.0.0-beta.10");
487+
488+
// Create feature from release
489+
fixture.BranchTo("feature/xxx");
490+
fixture.Repository.MakeACommit("feature 1");
491+
fixture.Repository.MakeACommit("feature 2");
492+
493+
// Check version on release
494+
Commands.Checkout(fixture.Repository, "release-2.0.0");
495+
fixture.AssertFullSemver(config, "2.0.0-beta.10");
496+
fixture.Repository.MakeACommit("release 11");
497+
fixture.AssertFullSemver(config, "2.0.0-beta.11");
498+
499+
// Make new commit on feature
500+
Commands.Checkout(fixture.Repository, "feature/xxx");
501+
fixture.Repository.MakeACommit("feature 3");
502+
503+
// Checkout to release (no new commits)
504+
Commands.Checkout(fixture.Repository, "release-2.0.0");
505+
fixture.AssertFullSemver(config, "2.0.0-beta.11");
506+
507+
// Merge feature to release
508+
fixture.Repository.MergeNoFF("feature/xxx", Generate.SignatureNow());
509+
fixture.AssertFullSemver(config, "2.0.0-beta.15");
510+
511+
fixture.Repository.MakeACommit("release 13 - after feature merge");
512+
fixture.AssertFullSemver(config, "2.0.0-beta.16");
513+
}
514+
}
466515
}

src/GitVersionCore.Tests/IntegrationTests/VersionBumpingScenarios.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using GitVersion;
33
using GitVersionCore.Tests;
44
using NUnit.Framework;
5+
using System.Collections.Generic;
56

67
[TestFixture]
78
public class VersionBumpingScenarios
@@ -17,7 +18,7 @@ public void AppliedPrereleaseTagCausesBump()
1718
"master", new BranchConfig
1819
{
1920
Tag = "pre",
20-
SourceBranches = new string[0]
21+
SourceBranches = new List<string>()
2122
}
2223
}
2324
}

src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void PreReleaseTagCanUseBranchName()
6868
{
6969
Regex = "custom/",
7070
Tag = "useBranchName",
71-
SourceBranches = new string[0]
71+
SourceBranches = new List<string>()
7272
}
7373
}
7474
}
@@ -99,7 +99,7 @@ public void PreReleaseTagCanUseBranchNameVariable()
9999
{
100100
Regex = "custom/",
101101
Tag = "alpha.{BranchName}",
102-
SourceBranches = new string[0]
102+
SourceBranches = new List<string>()
103103
}
104104
}
105105
}

src/GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static BranchConfig GetBranchConfiguration(GitVersionContext context, Bra
2222
targetBranch.FriendlyName));
2323

2424
matchingBranches = new BranchConfig { Name = string.Empty };
25-
ConfigurationProvider.ApplyBranchDefaults(context.FullConfiguration, matchingBranches, "", new string[0]);
25+
ConfigurationProvider.ApplyBranchDefaults(context.FullConfiguration, matchingBranches, "", new List<string>());
2626
}
2727

2828
return matchingBranches.Increment == IncrementStrategy.Inherit ?

src/GitVersionCore/Configuration/BranchConfig.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace GitVersion
22
{
3+
using System.Collections.Generic;
34
using YamlDotNet.Serialization;
45

56
public class BranchConfig
@@ -25,6 +26,8 @@ public BranchConfig(BranchConfig branchConfiguration)
2526
IsReleaseBranch = branchConfiguration.IsReleaseBranch;
2627
IsMainline = branchConfiguration.IsMainline;
2728
Name = branchConfiguration.Name;
29+
SourceBranches = branchConfiguration.SourceBranches;
30+
IsSourceBranchFor = branchConfiguration.IsSourceBranchFor;
2831
}
2932

3033
[YamlMember(Alias = "mode")]
@@ -55,7 +58,10 @@ public BranchConfig(BranchConfig branchConfiguration)
5558
public string Regex { get; set; }
5659

5760
[YamlMember(Alias = "source-branches")]
58-
public string[] SourceBranches { get; set; }
61+
public List<string> SourceBranches { get; set; }
62+
63+
[YamlMember(Alias = "is-source-branch-for")]
64+
public string[] IsSourceBranchFor { get; set; }
5965

6066
[YamlMember(Alias = "tracks-release-branches")]
6167
public bool? TracksReleaseBranches { get; set; }

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace GitVersion
22
{
33
using Configuration.Init.Wizard;
44
using GitVersion.Helpers;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using System.Text;
@@ -104,7 +105,7 @@ public static void ApplyDefaultsTo(Config config)
104105
ApplyBranchDefaults(config,
105106
GetOrCreateBranchDefaults(config, DevelopBranchKey),
106107
DevelopBranchRegex,
107-
new string[0],
108+
new List<string>(),
108109
defaultTag: "alpha",
109110
defaultIncrementStrategy: IncrementStrategy.Minor,
110111
defaultVersioningMode: VersioningMode.ContinuousDeployment,
@@ -113,41 +114,41 @@ public static void ApplyDefaultsTo(Config config)
113114
ApplyBranchDefaults(config,
114115
GetOrCreateBranchDefaults(config, MasterBranchKey),
115116
MasterBranchRegex,
116-
new string[] { "develop", "release" },
117+
new List<string> { "develop", "release" },
117118
defaultTag: string.Empty,
118119
defaultPreventIncrement: true,
119120
defaultIncrementStrategy: IncrementStrategy.Patch,
120121
isMainline: true);
121122
ApplyBranchDefaults(config,
122123
GetOrCreateBranchDefaults(config, ReleaseBranchKey),
123124
ReleaseBranchRegex,
124-
new string[] { "develop", "master" },
125+
new List<string> { "develop", "master", "support", "release" },
125126
defaultTag: "beta",
126127
defaultPreventIncrement: true,
127128
defaultIncrementStrategy: IncrementStrategy.Patch,
128129
isReleaseBranch: true);
129130
ApplyBranchDefaults(config,
130131
GetOrCreateBranchDefaults(config, FeatureBranchKey),
131132
FeatureBranchRegex,
132-
new string[] { "develop", "master", "release", "feature", "support", "hotfix" },
133+
new List<string> { "develop", "master", "release", "feature", "support", "hotfix" },
133134
defaultIncrementStrategy: IncrementStrategy.Inherit);
134135
ApplyBranchDefaults(config,
135136
GetOrCreateBranchDefaults(config, PullRequestBranchKey),
136137
PullRequestRegex,
137-
new string[] { "develop", "master", "release", "feature", "support", "hotfix" },
138+
new List<string> { "develop", "master", "release", "feature", "support", "hotfix" },
138139
defaultTag: "PullRequest",
139140
defaultTagNumberPattern: @"[/-](?<number>\d+)",
140141
defaultIncrementStrategy: IncrementStrategy.Inherit);
141142
ApplyBranchDefaults(config,
142143
GetOrCreateBranchDefaults(config, HotfixBranchKey),
143144
HotfixBranchRegex,
144-
new string[] { "develop", "master" },
145+
new List<string> { "develop", "master", "support" },
145146
defaultTag: "beta",
146147
defaultIncrementStrategy: IncrementStrategy.Patch);
147148
ApplyBranchDefaults(config,
148149
GetOrCreateBranchDefaults(config, SupportBranchKey),
149150
SupportBranchRegex,
150-
new string[] { "master" },
151+
new List<string> { "master" },
151152
defaultTag: string.Empty,
152153
defaultPreventIncrement: true,
153154
defaultIncrementStrategy: IncrementStrategy.Patch,
@@ -171,6 +172,16 @@ public static void ApplyDefaultsTo(Config config)
171172

172173
ApplyBranchDefaults(config, branchConfig.Value, regex, sourceBranches);
173174
}
175+
176+
// This is a second pass to add additional sources, it has to be another pass to prevent ordering issues
177+
foreach (var branchConfig in configBranches)
178+
{
179+
if (branchConfig.Value.IsSourceBranchFor == null) continue;
180+
foreach (var isSourceBranch in branchConfig.Value.IsSourceBranchFor)
181+
{
182+
config.Branches[isSourceBranch].SourceBranches.Add(branchConfig.Key);
183+
}
184+
}
174185
}
175186

176187
static void ApplyOverridesTo(Config config, Config overrideConfig)
@@ -193,7 +204,7 @@ static BranchConfig GetOrCreateBranchDefaults(Config config, string branchKey)
193204
public static void ApplyBranchDefaults(Config config,
194205
BranchConfig branchConfig,
195206
string branchRegex,
196-
string[] sourceBranches,
207+
List<string> sourceBranches,
197208
string defaultTag = "useBranchName",
198209
IncrementStrategy? defaultIncrementStrategy = null, // Looked up from main config
199210
bool defaultPreventIncrement = false,

0 commit comments

Comments
 (0)