Skip to content

Commit d2f4d34

Browse files
DanielRoseJakeGinnivan
authored andcommitted
Use Inherit strategy by default.
1 parent 5f15d60 commit d2f4d34

File tree

4 files changed

+80
-29
lines changed

4 files changed

+80
-29
lines changed

src/GitVersionCore.Tests/GitVersionContextTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,34 @@ public void CanInheritVersioningMode(VersioningMode mode)
3131
context.Configuration.VersioningMode.ShouldBe(mode);
3232
}
3333

34+
[TestCase(IncrementStrategy.Inherit, IncrementStrategy.Patch)] // Since it inherits, the increment strategy of master is used => Patch
35+
[TestCase(IncrementStrategy.Patch, null)]
36+
[TestCase(IncrementStrategy.Major, null)]
37+
[TestCase(IncrementStrategy.Minor, null)]
38+
[TestCase(IncrementStrategy.None, null)]
39+
public void CanInheritIncrement(IncrementStrategy increment, IncrementStrategy? alternateExpected)
40+
{
41+
// Dummy branch name to make sure that no default config exists.
42+
const string dummyBranchName = "dummy";
43+
44+
var config = new Config
45+
{
46+
Increment = increment
47+
};
48+
ConfigurationProvider.ApplyDefaultsTo(config);
49+
50+
using (var fixture = new EmptyRepositoryFixture())
51+
{
52+
fixture.MakeACommit();
53+
fixture.BranchTo(dummyBranchName);
54+
fixture.MakeACommit();
55+
56+
var context = new GitVersionContext(fixture.Repository, fixture.Repository.Branches[dummyBranchName], config);
57+
context.Configuration.Increment.ShouldBe(alternateExpected ?? increment);
58+
}
59+
60+
}
61+
3462
[Test]
3563
public void UsesBranchSpecificConfigOverTopLevelDefaults()
3664
{

src/GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,30 @@ public static BranchConfig GetBranchConfiguration(Commit currentCommit, IReposit
1616
{
1717
var matchingBranches = LookupBranchConfiguration(config, currentBranch).ToArray();
1818

19-
if (matchingBranches.Length == 0)
19+
if (matchingBranches.Length > 1)
2020
{
21-
Logger.WriteInfo(string.Format(
22-
"No branch configuration found for branch {0}, falling back to default configuration",
23-
currentBranch.FriendlyName));
24-
25-
var branchConfig = new BranchConfig { Name = string.Empty };
26-
ConfigurationProvider.ApplyBranchDefaults(config, branchConfig, "");
27-
return branchConfig;
21+
const string format = "Multiple branch configurations match the current branch branchName of '{0}'. Matching configurations: '{1}'";
22+
throw new Exception(string.Format(format, currentBranch.FriendlyName, string.Join(", ", matchingBranches.Select(b => b.Name))));
2823
}
2924

25+
BranchConfig branchConfiguration;
3026
if (matchingBranches.Length == 1)
3127
{
32-
var branchConfiguration = matchingBranches[0];
33-
34-
if (branchConfiguration.Increment == IncrementStrategy.Inherit)
35-
{
36-
return InheritBranchConfiguration(onlyEvaluateTrackedBranches, repository, currentCommit, currentBranch, branchConfiguration, config, excludedInheritBranches);
37-
}
28+
branchConfiguration = matchingBranches[0];
29+
}
30+
else
31+
{
32+
Logger.WriteInfo(string.Format(
33+
"No branch configuration found for branch {0}, falling back to default configuration",
34+
currentBranch.FriendlyName));
3835

39-
return branchConfiguration;
36+
branchConfiguration = new BranchConfig { Name = string.Empty };
37+
ConfigurationProvider.ApplyBranchDefaults(config, branchConfiguration, "");
4038
}
4139

42-
const string format = "Multiple branch configurations match the current branch branchName of '{0}'. Matching configurations: '{1}'";
43-
throw new Exception(string.Format(format, currentBranch.FriendlyName, string.Join(", ", matchingBranches.Select(b => b.Name))));
40+
return branchConfiguration.Increment == IncrementStrategy.Inherit ?
41+
InheritBranchConfiguration(onlyEvaluateTrackedBranches, repository, currentCommit, currentBranch, branchConfiguration, config, excludedInheritBranches) :
42+
branchConfiguration;
4443
}
4544

4645
static IEnumerable<BranchConfig> LookupBranchConfiguration([NotNull] Config config, [NotNull] Branch currentBranch)
@@ -58,7 +57,6 @@ static IEnumerable<BranchConfig> LookupBranchConfiguration([NotNull] Config conf
5857
return config.Branches.Where(b => Regex.IsMatch(currentBranch.FriendlyName, "^" + b.Value.Regex, RegexOptions.IgnoreCase)).Select(kvp => kvp.Value);
5958
}
6059

61-
6260
static BranchConfig InheritBranchConfiguration(bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, Branch currentBranch, BranchConfig branchConfiguration, Config config, IList<Branch> excludedInheritBranches)
6361
{
6462
using (Logger.IndentLog("Attempting to inherit branch configuration from parent branch"))

src/GitVersionCore/Configuration/Config.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,8 @@ T MergeObjects<T>(T target, T source)
9999

100100
[YamlMember(Alias = "ignore")]
101101
public IgnoreConfig Ignore { get; set; }
102+
103+
[YamlMember(Alias = "increment")]
104+
public IncrementStrategy? Increment { get; set; }
102105
}
103106
}

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class ConfigurationProvider
3030
public const string SupportBranchKey = "support";
3131
public const string DevelopBranchKey = "develop";
3232

33+
private const IncrementStrategy DefaultIncrementStrategy = IncrementStrategy.Inherit;
34+
3335
public static Config Provide(GitPreparer gitPreparer, IFileSystem fileSystem, bool applyDefaults = true, Config overrideConfig = null)
3436
{
3537
var workingDirectory = gitPreparer.WorkingDirectory;
@@ -99,33 +101,53 @@ public static void ApplyDefaultsTo(Config config)
99101

100102
var configBranches = config.Branches.ToList();
101103

102-
ApplyBranchDefaults(config, GetOrCreateBranchDefaults(config, MasterBranchKey),
104+
ApplyBranchDefaults(config,
105+
GetOrCreateBranchDefaults(config, MasterBranchKey),
103106
MasterBranchRegex,
104107
defaultTag: string.Empty,
105108
defaultPreventIncrement: true,
109+
defaultIncrementStrategy: IncrementStrategy.Patch,
106110
isMainline: true);
107-
ApplyBranchDefaults(config, GetOrCreateBranchDefaults(config, ReleaseBranchKey), ReleaseBranchRegex, defaultTag: "beta", defaultPreventIncrement: true, isReleaseBranch: true);
108-
ApplyBranchDefaults(config, GetOrCreateBranchDefaults(config, FeatureBranchKey), FeatureBranchRegex, defaultIncrementStrategy: IncrementStrategy.Inherit);
109-
ApplyBranchDefaults(config, GetOrCreateBranchDefaults(config, PullRequestBranchKey), PullRequestRegex,
111+
ApplyBranchDefaults(config,
112+
GetOrCreateBranchDefaults(config, ReleaseBranchKey),
113+
ReleaseBranchRegex,
114+
defaultTag: "beta",
115+
defaultPreventIncrement: true,
116+
defaultIncrementStrategy: IncrementStrategy.Patch,
117+
isReleaseBranch: true);
118+
ApplyBranchDefaults(config,
119+
GetOrCreateBranchDefaults(config, FeatureBranchKey),
120+
FeatureBranchRegex,
121+
defaultIncrementStrategy: IncrementStrategy.Inherit);
122+
ApplyBranchDefaults(config,
123+
GetOrCreateBranchDefaults(config, PullRequestBranchKey),
124+
PullRequestRegex,
110125
defaultTag: "PullRequest",
111126
defaultTagNumberPattern: @"[/-](?<number>\d+)[-/]",
112127
defaultIncrementStrategy: IncrementStrategy.Inherit);
113-
ApplyBranchDefaults(config, GetOrCreateBranchDefaults(config, HotfixBranchKey), HotfixBranchRegex, defaultTag: "beta");
114-
ApplyBranchDefaults(config, GetOrCreateBranchDefaults(config, SupportBranchKey),
128+
ApplyBranchDefaults(config,
129+
GetOrCreateBranchDefaults(config, HotfixBranchKey),
130+
HotfixBranchRegex,
131+
defaultTag: "beta",
132+
defaultIncrementStrategy: IncrementStrategy.Patch);
133+
ApplyBranchDefaults(config,
134+
GetOrCreateBranchDefaults(config, SupportBranchKey),
115135
SupportBranchRegex,
116136
defaultTag: string.Empty,
117137
defaultPreventIncrement: true,
138+
defaultIncrementStrategy: IncrementStrategy.Patch,
118139
isMainline: true);
119-
ApplyBranchDefaults(config, GetOrCreateBranchDefaults(config, DevelopBranchKey),
140+
ApplyBranchDefaults(config,
141+
GetOrCreateBranchDefaults(config, DevelopBranchKey),
120142
DevelopBranchRegex,
121143
defaultTag: "alpha",
122144
defaultIncrementStrategy: IncrementStrategy.Minor,
123145
defaultVersioningMode: VersioningMode.ContinuousDeployment,
124146
defaultTrackMergeTarget: true,
125147
tracksReleaseBranches: true);
126148

127-
// Any user defined branches should have other values defaulted after known branches filled in
128-
// This allows users to override one value of
149+
// Any user defined branches should have other values defaulted after known branches filled in.
150+
// This allows users to override any of the value.
129151
foreach (var branchConfig in configBranches)
130152
{
131153
var regex = branchConfig.Value.Regex;
@@ -159,7 +181,7 @@ public static void ApplyBranchDefaults(Config config,
159181
BranchConfig branchConfig,
160182
string branchRegex,
161183
string defaultTag = "useBranchName",
162-
IncrementStrategy defaultIncrementStrategy = IncrementStrategy.Patch,
184+
IncrementStrategy? defaultIncrementStrategy = null, // Looked up from main config
163185
bool defaultPreventIncrement = false,
164186
VersioningMode? defaultVersioningMode = null, // Looked up from main config
165187
bool defaultTrackMergeTarget = false,
@@ -171,7 +193,7 @@ public static void ApplyBranchDefaults(Config config,
171193
branchConfig.Regex = string.IsNullOrEmpty(branchConfig.Regex) ? branchRegex : branchConfig.Regex;
172194
branchConfig.Tag = branchConfig.Tag ?? defaultTag;
173195
branchConfig.TagNumberPattern = branchConfig.TagNumberPattern ?? defaultTagNumberPattern;
174-
branchConfig.Increment = branchConfig.Increment ?? defaultIncrementStrategy;
196+
branchConfig.Increment = branchConfig.Increment ?? defaultIncrementStrategy ?? config.Increment ?? DefaultIncrementStrategy;
175197
branchConfig.PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion ?? defaultPreventIncrement;
176198
branchConfig.TrackMergeTarget = branchConfig.TrackMergeTarget ?? defaultTrackMergeTarget;
177199
branchConfig.VersioningMode = branchConfig.VersioningMode ?? defaultVersioningMode ?? config.VersioningMode;

0 commit comments

Comments
 (0)