Skip to content

Commit 0a93fe6

Browse files
committed
Introduced source-branches config option
This allows GitVersion to know what branches it should take into account for calculating merge base. This locks it down a little more, but is an easier to explain behavior than commit counting randomly resetting because GitVersion cannot effectively figure out which branch the current branch was branched off.
1 parent 891eaba commit 0a93fe6

12 files changed

+122
-30
lines changed

docs/configuration.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,41 @@ values, but here they are if you need to:
243243
### regex
244244
This is the regex which is used to match the current branch to the correct branch configuration.
245245

246+
### source-branches
247+
Because git is a directed graph GitVersion sometimes cannot tell which branch the current branch was branched from.
248+
249+
Take this graph
250+
251+
```
252+
* feature/foo
253+
* | release/1.0.0
254+
|/
255+
*
256+
*
257+
* (master)
258+
```
259+
260+
By looking at this graph, you cannot tell which of these scenarios happened
261+
262+
1. feature/foo branches off release/1.0.0
263+
- Branch release/1.0.0 from master
264+
- Branch feature/foo from release/1.0.0
265+
- Add a commit to both release/1.0.0 and feature/foo
266+
- release/1.0.0 is the base for feature/foo
267+
268+
2. release/1.0.0 branches off feature/foo
269+
- Branch feature/foo from master
270+
- Branch release/1.0.0 from feature/foo
271+
- Add a commit to both release/1.0.0 and feature/foo
272+
- feature/foo is the base for release/1.0.0
273+
274+
To resolve this issue we give GitVersion a hint to how we normally do our branching, feature branches have a value of:
275+
276+
`sourceBranches: ['master', 'develop', 'feature', 'hotfix', 'support']`
277+
278+
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.
279+
280+
246281
### branches
247282
The header for all the individual branch configuration.
248283

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

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ build-metadata-padding: 4
1212
commits-since-version-source-padding: 4
1313
commit-message-incrementing: Enabled
1414
branches:
15+
develop:
16+
mode: ContinuousDeployment
17+
tag: alpha
18+
increment: Minor
19+
prevent-increment-of-merged-branch-version: false
20+
track-merge-target: true
21+
regex: dev(elop)?(ment)?$
22+
source-branches: []
23+
tracks-release-branches: true
24+
is-release-branch: false
25+
is-mainline: false
1526
master:
1627
mode: ContinuousDelivery
1728
tag: ''
1829
increment: Patch
1930
prevent-increment-of-merged-branch-version: true
2031
track-merge-target: false
2132
regex: master
33+
source-branches:
34+
- develop
35+
- release
2236
tracks-release-branches: false
2337
is-release-branch: false
2438
is-mainline: true
@@ -29,6 +43,9 @@ branches:
2943
prevent-increment-of-merged-branch-version: true
3044
track-merge-target: false
3145
regex: releases?[/-]
46+
source-branches:
47+
- develop
48+
- master
3249
tracks-release-branches: false
3350
is-release-branch: true
3451
is-mainline: false
@@ -39,6 +56,13 @@ branches:
3956
prevent-increment-of-merged-branch-version: false
4057
track-merge-target: false
4158
regex: features?[/-]
59+
source-branches:
60+
- develop
61+
- master
62+
- release
63+
- feature
64+
- support
65+
- hotfix
4266
tracks-release-branches: false
4367
is-release-branch: false
4468
is-mainline: false
@@ -50,6 +74,13 @@ branches:
5074
tag-number-pattern: '[/-](?<number>\d+)'
5175
track-merge-target: false
5276
regex: (pull|pull\-requests|pr)[/-]
77+
source-branches:
78+
- develop
79+
- master
80+
- release
81+
- feature
82+
- support
83+
- hotfix
5384
tracks-release-branches: false
5485
is-release-branch: false
5586
is-mainline: false
@@ -60,6 +91,9 @@ branches:
6091
prevent-increment-of-merged-branch-version: false
6192
track-merge-target: false
6293
regex: hotfix(es)?[/-]
94+
source-branches:
95+
- develop
96+
- master
6397
tracks-release-branches: false
6498
is-release-branch: false
6599
is-mainline: false
@@ -70,18 +104,10 @@ branches:
70104
prevent-increment-of-merged-branch-version: true
71105
track-merge-target: false
72106
regex: support[/-]
107+
source-branches:
108+
- master
73109
tracks-release-branches: false
74110
is-release-branch: false
75111
is-mainline: true
76-
develop:
77-
mode: ContinuousDeployment
78-
tag: alpha
79-
increment: Minor
80-
prevent-increment-of-merged-branch-version: false
81-
track-merge-target: true
82-
regex: dev(elop)?(ment)?$
83-
tracks-release-branches: true
84-
is-release-branch: false
85-
is-mainline: false
86112
ignore:
87113
sha: []

src/GitVersionCore.Tests/ConfigProviderTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public void CanProvideConfigForNewBranch()
111111
branches:
112112
bug:
113113
regex: 'bug[/-]'
114-
tag: bugfix";
114+
tag: bugfix
115+
source-branches: []";
115116
SetupConfigFileContent(text);
116117
var config = ConfigurationProvider.Provide(repoPath, fileSystem);
117118

src/GitVersionCore.Tests/GitVersionContextTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public void UsesFirstBranchConfigWhenMultipleMatch()
9898
VersioningMode = VersioningMode.ContinuousDelivery,
9999
Branches =
100100
{
101-
{ "release/latest", new BranchConfig { Increment = IncrementStrategy.None, Regex = "release/latest" } },
102-
{ "release", new BranchConfig { Increment = IncrementStrategy.Patch, Regex = "releases?[/-]" } }
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] } }
103103
}
104104
}.ApplyDefaults();
105105

src/GitVersionCore.Tests/IntegrationTests/DevelopScenarios.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public void CanChangeDevelopTagViaConfig()
6464
{
6565
"develop", new BranchConfig
6666
{
67-
Tag = "alpha"
67+
Tag = "alpha",
68+
SourceBranches = new string[0]
6869
}
6970
}
7071
}

src/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public void BranchCreatedAfterFastForwardMergeShouldInheritCorrectly()
4242
{
4343
Branches =
4444
{
45-
{ "unstable", new BranchConfig { Increment = IncrementStrategy.Minor, Regex = "unstable"} }
45+
{ "unstable", new BranchConfig {
46+
Increment = IncrementStrategy.Minor, Regex = "unstable", SourceBranches = new string[0]
47+
} }
4648
}
4749
};
4850

src/GitVersionCore.Tests/IntegrationTests/OtherScenarios.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public void AllowHavingMainInsteadOfMaster()
5656
Tag = "useBranchName",
5757
Increment = IncrementStrategy.Patch,
5858
PreventIncrementOfMergedBranchVersion = true,
59-
TrackMergeTarget = false
59+
TrackMergeTarget = false,
60+
SourceBranches = new string[0]
6061
});
6162

6263
using (var fixture = new EmptyRepositoryFixture())

src/GitVersionCore.Tests/IntegrationTests/VersionBumpingScenarios.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ public void AppliedPrereleaseTagCausesBump()
1313
{
1414
Branches =
1515
{
16-
{ "master", new BranchConfig { Tag = "pre" } }
16+
{
17+
"master", new BranchConfig
18+
{
19+
Tag = "pre",
20+
SourceBranches = new string[0]
21+
}
22+
}
1723
}
1824
};
1925
using (var fixture = new EmptyRepositoryFixture())

src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public void PreReleaseTagCanUseBranchName()
6767
"custom", new BranchConfig
6868
{
6969
Regex = "custom/",
70-
Tag = "useBranchName"
70+
Tag = "useBranchName",
71+
SourceBranches = new string[0]
7172
}
7273
}
7374
}
@@ -97,7 +98,8 @@ public void PreReleaseTagCanUseBranchNameVariable()
9798
"custom", new BranchConfig
9899
{
99100
Regex = "custom/",
100-
Tag = "alpha.{BranchName}"
101+
Tag = "alpha.{BranchName}",
102+
SourceBranches = new string[0]
101103
}
102104
}
103105
}

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, "");
25+
ConfigurationProvider.ApplyBranchDefaults(context.FullConfiguration, matchingBranches, "", new string[0]);
2626
}
2727

2828
return matchingBranches.Increment == IncrementStrategy.Inherit ?

src/GitVersionCore/Configuration/BranchConfig.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public BranchConfig(BranchConfig branchConfiguration)
5454
[YamlMember(Alias = "regex")]
5555
public string Regex { get; set; }
5656

57+
[YamlMember(Alias = "source-branches")]
58+
public string[] SourceBranches { get; set; }
59+
5760
[YamlMember(Alias = "tracks-release-branches")]
5861
public bool? TracksReleaseBranches { get; set; }
5962

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,50 +101,57 @@ public static void ApplyDefaultsTo(Config config)
101101

102102
var configBranches = config.Branches.ToList();
103103

104+
ApplyBranchDefaults(config,
105+
GetOrCreateBranchDefaults(config, DevelopBranchKey),
106+
DevelopBranchRegex,
107+
new string[0],
108+
defaultTag: "alpha",
109+
defaultIncrementStrategy: IncrementStrategy.Minor,
110+
defaultVersioningMode: VersioningMode.ContinuousDeployment,
111+
defaultTrackMergeTarget: true,
112+
tracksReleaseBranches: true);
104113
ApplyBranchDefaults(config,
105114
GetOrCreateBranchDefaults(config, MasterBranchKey),
106115
MasterBranchRegex,
116+
new string[] { "develop", "release" },
107117
defaultTag: string.Empty,
108118
defaultPreventIncrement: true,
109119
defaultIncrementStrategy: IncrementStrategy.Patch,
110120
isMainline: true);
111121
ApplyBranchDefaults(config,
112122
GetOrCreateBranchDefaults(config, ReleaseBranchKey),
113123
ReleaseBranchRegex,
124+
new string[] { "develop", "master" },
114125
defaultTag: "beta",
115126
defaultPreventIncrement: true,
116127
defaultIncrementStrategy: IncrementStrategy.Patch,
117128
isReleaseBranch: true);
118129
ApplyBranchDefaults(config,
119130
GetOrCreateBranchDefaults(config, FeatureBranchKey),
120131
FeatureBranchRegex,
132+
new string[] { "develop", "master", "release", "feature", "support", "hotfix" },
121133
defaultIncrementStrategy: IncrementStrategy.Inherit);
122134
ApplyBranchDefaults(config,
123135
GetOrCreateBranchDefaults(config, PullRequestBranchKey),
124136
PullRequestRegex,
137+
new string[] { "develop", "master", "release", "feature", "support", "hotfix" },
125138
defaultTag: "PullRequest",
126139
defaultTagNumberPattern: @"[/-](?<number>\d+)",
127140
defaultIncrementStrategy: IncrementStrategy.Inherit);
128141
ApplyBranchDefaults(config,
129142
GetOrCreateBranchDefaults(config, HotfixBranchKey),
130143
HotfixBranchRegex,
144+
new string[] { "develop", "master" },
131145
defaultTag: "beta",
132146
defaultIncrementStrategy: IncrementStrategy.Patch);
133147
ApplyBranchDefaults(config,
134148
GetOrCreateBranchDefaults(config, SupportBranchKey),
135149
SupportBranchRegex,
150+
new string[] { "master" },
136151
defaultTag: string.Empty,
137152
defaultPreventIncrement: true,
138153
defaultIncrementStrategy: IncrementStrategy.Patch,
139154
isMainline: true);
140-
ApplyBranchDefaults(config,
141-
GetOrCreateBranchDefaults(config, DevelopBranchKey),
142-
DevelopBranchRegex,
143-
defaultTag: "alpha",
144-
defaultIncrementStrategy: IncrementStrategy.Minor,
145-
defaultVersioningMode: VersioningMode.ContinuousDeployment,
146-
defaultTrackMergeTarget: true,
147-
tracksReleaseBranches: true);
148155

149156
// Any user defined branches should have other values defaulted after known branches filled in.
150157
// This allows users to override any of the value.
@@ -153,10 +160,16 @@ public static void ApplyDefaultsTo(Config config)
153160
var regex = branchConfig.Value.Regex;
154161
if (regex == null)
155162
{
156-
throw new GitVersionConfigurationException(string.Format("Branch configuration '{0}' is missing required configuration 'regex'", branchConfig.Key));
163+
throw new GitVersionConfigurationException($"Branch configuration '{branchConfig.Key}' is missing required configuration 'regex'");
164+
}
165+
166+
var sourceBranches = branchConfig.Value.SourceBranches;
167+
if (sourceBranches == null)
168+
{
169+
throw new GitVersionConfigurationException($"Branch configuration '{branchConfig.Key}' is missing required configuration 'source-branches'");
157170
}
158171

159-
ApplyBranchDefaults(config, branchConfig.Value, regex);
172+
ApplyBranchDefaults(config, branchConfig.Value, regex, sourceBranches);
160173
}
161174
}
162175

@@ -180,6 +193,7 @@ static BranchConfig GetOrCreateBranchDefaults(Config config, string branchKey)
180193
public static void ApplyBranchDefaults(Config config,
181194
BranchConfig branchConfig,
182195
string branchRegex,
196+
string[] sourceBranches,
183197
string defaultTag = "useBranchName",
184198
IncrementStrategy? defaultIncrementStrategy = null, // Looked up from main config
185199
bool defaultPreventIncrement = false,
@@ -191,6 +205,7 @@ public static void ApplyBranchDefaults(Config config,
191205
bool isMainline = false)
192206
{
193207
branchConfig.Regex = string.IsNullOrEmpty(branchConfig.Regex) ? branchRegex : branchConfig.Regex;
208+
branchConfig.SourceBranches = sourceBranches;
194209
branchConfig.Tag = branchConfig.Tag ?? defaultTag;
195210
branchConfig.TagNumberPattern = branchConfig.TagNumberPattern ?? defaultTagNumberPattern;
196211
branchConfig.Increment = branchConfig.Increment ?? defaultIncrementStrategy ?? config.Increment ?? DefaultIncrementStrategy;

0 commit comments

Comments
 (0)