Skip to content

Commit cb77f97

Browse files
DanielRoseJakeGinnivan
authored andcommitted
Put the name of the branch configuration in BranchConfig.
1 parent b8c34c3 commit cb77f97

File tree

8 files changed

+70
-65
lines changed

8 files changed

+70
-65
lines changed

src/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using GitVersionCore.Tests;
55
using LibGit2Sharp;
66
using NUnit.Framework;
7-
using System.Collections.Generic;
87

98
[TestFixture]
109
public class FeatureBranchScenarios
@@ -296,7 +295,7 @@ public void ShouldPickUpVersionFromMasterAfterReleaseBranchCreated()
296295
{
297296
"master", new BranchConfig()
298297
{
299-
IsDevelop = true,
298+
TracksReleaseBranches = true,
300299
Regex = "master"
301300
}
302301
}
@@ -329,7 +328,7 @@ public void ShouldPickUpVersionFromMasterAfterReleaseBranchMergedBack()
329328
{
330329
"master", new BranchConfig()
331330
{
332-
IsDevelop = true,
331+
TracksReleaseBranches = true,
333332
Regex = "master"
334333
}
335334
}
@@ -414,7 +413,7 @@ public void ShouldPickUpVersionFromMasterAfterReleaseBranchCreated()
414413
{
415414
"master", new BranchConfig()
416415
{
417-
IsDevelop = true,
416+
TracksReleaseBranches = true,
418417
Regex = "master"
419418
}
420419
}
@@ -447,7 +446,7 @@ public void ShouldPickUpVersionFromMasterAfterReleaseBranchMergedBack()
447446
{
448447
"master", new BranchConfig()
449448
{
450-
IsDevelop = true,
449+
TracksReleaseBranches = true,
451450
Regex = "master"
452451
}
453452
}

src/GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,41 @@ namespace GitVersion
99

1010
public class BranchConfigurationCalculator
1111
{
12-
public static KeyValuePair<string, BranchConfig> GetBranchConfiguration(Commit currentCommit, IRepository repository, bool onlyEvaluateTrackedBranches, Config config, Branch currentBranch, IList<Branch> excludedInheritBranches = null)
12+
/// <summary>
13+
/// Gets the <see cref="BranchConfig"/> for the current commit.
14+
/// </summary>
15+
public static BranchConfig GetBranchConfiguration(Commit currentCommit, IRepository repository, bool onlyEvaluateTrackedBranches, Config config, Branch currentBranch, IList<Branch> excludedInheritBranches = null)
1316
{
14-
var matchingBranches = LookupBranchConfiguration(config, currentBranch);
17+
var matchingBranches = LookupBranchConfiguration(config, currentBranch).ToArray();
1518

1619
if (matchingBranches.Length == 0)
1720
{
1821
Logger.WriteInfo(string.Format(
1922
"No branch configuration found for branch {0}, falling back to default configuration",
2023
currentBranch.FriendlyName));
2124

22-
var branchConfig = new BranchConfig();
25+
var branchConfig = new BranchConfig { Name = string.Empty };
2326
ConfigurationProvider.ApplyBranchDefaults(config, branchConfig, "");
24-
return new KeyValuePair<string, BranchConfig>(string.Empty, branchConfig);
27+
return branchConfig;
2528
}
29+
2630
if (matchingBranches.Length == 1)
2731
{
28-
var keyValuePair = matchingBranches[0];
29-
var branchConfiguration = keyValuePair.Value;
32+
var branchConfiguration = matchingBranches[0];
3033

3134
if (branchConfiguration.Increment == IncrementStrategy.Inherit)
3235
{
33-
return InheritBranchConfiguration(onlyEvaluateTrackedBranches, repository, currentCommit, currentBranch, keyValuePair, branchConfiguration, config, excludedInheritBranches);
36+
return InheritBranchConfiguration(onlyEvaluateTrackedBranches, repository, currentCommit, currentBranch, branchConfiguration, config, excludedInheritBranches);
3437
}
3538

36-
return keyValuePair;
39+
return branchConfiguration;
3740
}
3841

3942
const string format = "Multiple branch configurations match the current branch branchName of '{0}'. Matching configurations: '{1}'";
40-
throw new Exception(string.Format(format, currentBranch.FriendlyName, string.Join(", ", matchingBranches.Select(b => b.Key))));
43+
throw new Exception(string.Format(format, currentBranch.FriendlyName, string.Join(", ", matchingBranches.Select(b => b.Name))));
4144
}
4245

43-
static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration([NotNull] Config config, [NotNull] Branch currentBranch)
46+
static IEnumerable<BranchConfig> LookupBranchConfiguration([NotNull] Config config, [NotNull] Branch currentBranch)
4447
{
4548
if (config == null)
4649
{
@@ -52,11 +55,11 @@ static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration([NotNull]
5255
throw new ArgumentNullException("currentBranch");
5356
}
5457

55-
return config.Branches.Where(b => Regex.IsMatch(currentBranch.FriendlyName, "^" + b.Value.Regex, RegexOptions.IgnoreCase)).ToArray();
58+
return config.Branches.Where(b => Regex.IsMatch(currentBranch.FriendlyName, "^" + b.Value.Regex, RegexOptions.IgnoreCase)).Select(kvp => kvp.Value);
5659
}
5760

5861

59-
static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, Branch currentBranch, KeyValuePair<string, BranchConfig> keyValuePair, BranchConfig branchConfiguration, Config config, IList<Branch> excludedInheritBranches)
62+
static BranchConfig InheritBranchConfiguration(bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, Branch currentBranch, BranchConfig branchConfiguration, Config config, IList<Branch> excludedInheritBranches)
6063
{
6164
using (Logger.IndentLog("Attempting to inherit branch configuration from parent branch"))
6265
{
@@ -72,11 +75,11 @@ static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEv
7275
{
7376
excludedInheritBranches = repository.Branches.Where(b =>
7477
{
75-
var branchConfig = LookupBranchConfiguration(config, b);
78+
var branchConfig = LookupBranchConfiguration(config, b).ToArray();
7679

7780
// NOTE: if length is 0 we couldn't find the configuration for the branch e.g. "origin/master"
7881
// NOTE: if the length is greater than 1 we cannot decide which merge strategy to pick
79-
return (branchConfig.Length != 1) || (branchConfig.Length == 1 && branchConfig[0].Value.Increment == IncrementStrategy.Inherit);
82+
return (branchConfig.Length != 1) || (branchConfig.Length == 1 && branchConfig[0].Increment == IncrementStrategy.Inherit);
8083
}).ToList();
8184
}
8285
excludedBranches.ToList().ForEach(excludedInheritBranches.Add);
@@ -110,16 +113,14 @@ static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEv
110113

111114
if (possibleParents.Count == 1)
112115
{
113-
var branchConfig = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, possibleParents[0], excludedInheritBranches).Value;
114-
return new KeyValuePair<string, BranchConfig>(
115-
keyValuePair.Key,
116-
new BranchConfig(branchConfiguration)
117-
{
118-
Increment = branchConfig.Increment,
119-
PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion,
120-
// If we are inheriting from develop then we should behave like develop
121-
TracksReleaseBranches = branchConfig.TracksReleaseBranches
122-
});
116+
var branchConfig = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, possibleParents[0], excludedInheritBranches);
117+
return new BranchConfig(branchConfiguration)
118+
{
119+
Increment = branchConfig.Increment,
120+
PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion,
121+
// If we are inheriting from develop then we should behave like develop
122+
TracksReleaseBranches = branchConfig.TracksReleaseBranches
123+
};
123124
}
124125

125126
// If we fail to inherit it is probably because the branch has been merged and we can't do much. So we will fall back to develop's config
@@ -142,16 +143,14 @@ static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEv
142143
var branchName = chosenBranch.FriendlyName;
143144
Logger.WriteWarning(errorMessage + Environment.NewLine + Environment.NewLine + "Falling back to " + branchName + " branch config");
144145

145-
var inheritingBranchConfig = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, chosenBranch).Value;
146-
return new KeyValuePair<string, BranchConfig>(
147-
keyValuePair.Key,
148-
new BranchConfig(branchConfiguration)
149-
{
150-
Increment = inheritingBranchConfig.Increment,
151-
PreventIncrementOfMergedBranchVersion = inheritingBranchConfig.PreventIncrementOfMergedBranchVersion,
152-
// If we are inheriting from develop then we should behave like develop
153-
TracksReleaseBranches = inheritingBranchConfig.TracksReleaseBranches
154-
});
146+
var inheritingBranchConfig = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, chosenBranch);
147+
return new BranchConfig(branchConfiguration)
148+
{
149+
Increment = inheritingBranchConfig.Increment,
150+
PreventIncrementOfMergedBranchVersion = inheritingBranchConfig.PreventIncrementOfMergedBranchVersion,
151+
// If we are inheriting from develop then we should behave like develop
152+
TracksReleaseBranches = inheritingBranchConfig.TracksReleaseBranches
153+
};
155154
}
156155
}
157156

src/GitVersionCore/Configuration/BranchConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public BranchConfig(BranchConfig branchConfiguration)
2424
Regex = branchConfiguration.Regex;
2525
IsReleaseBranch = branchConfiguration.IsReleaseBranch;
2626
IsMainline = branchConfiguration.IsMainline;
27+
Name = branchConfiguration.Name;
2728
}
2829

2930
[YamlMember(Alias = "mode")]
@@ -61,5 +62,11 @@ public BranchConfig(BranchConfig branchConfiguration)
6162

6263
[YamlMember(Alias = "is-mainline")]
6364
public bool? IsMainline { get; set; }
65+
66+
/// <summary>
67+
/// The name given to this configuration in the config file.
68+
/// </summary>
69+
[YamlIgnore]
70+
public string Name { get; set; }
6471
}
6572
}

src/GitVersionCore/Configuration/Config.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public Dictionary<string, BranchConfig> Branches
7979
value.ToList().ForEach(_ =>
8080
{
8181
if (!branches.ContainsKey(_.Key))
82-
branches.Add(_.Key, new BranchConfig());
82+
branches.Add(_.Key, new BranchConfig {Name = _.Key});
8383

8484
branches[_.Key] = MergeObjects(branches[_.Key], _.Value);
8585
});

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static BranchConfig GetOrCreateBranchDefaults(Config config, string branchKey)
147147
{
148148
if (!config.Branches.ContainsKey(branchKey))
149149
{
150-
var branchConfig = new BranchConfig();
150+
var branchConfig = new BranchConfig {Name = branchKey};
151151
config.Branches.Add(branchKey, branchConfig);
152152
return branchConfig;
153153
}

src/GitVersionCore/Configuration/IncrementStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public enum IncrementStrategy
99
Minor,
1010
Patch,
1111
/// <summary>
12-
/// Uses the <see cref="BranchConfig.Increment"/>, <see cref="BranchConfig.PreventIncrementOfMergedBranchVersion"/> and <see cref="BranchConfig.IsDevelop"/>
12+
/// Uses the <see cref="BranchConfig.Increment"/>, <see cref="BranchConfig.PreventIncrementOfMergedBranchVersion"/> and <see cref="BranchConfig.TracksReleaseBranches"/>
1313
/// of the "parent" branch (i.e. the branch where the current branch was branched from).
1414
/// </summary>
1515
Inherit

src/GitVersionCore/Configuration/Init/SetConfig/ConfigureBranches.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected override StepResult HandleResult(string result, Queue<ConfigInitWizard
2929
var branchConfig = foundBranch.Value;
3030
if (branchConfig == null)
3131
{
32-
branchConfig = new BranchConfig();
32+
branchConfig = new BranchConfig {Name = foundBranch.Key};
3333
config.Branches.Add(foundBranch.Key, branchConfig);
3434
}
3535
steps.Enqueue(new ConfigureBranch(foundBranch.Key, branchConfig, Console, FileSystem));

src/GitVersionCore/GitVersionContext.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,18 @@ void CalculateEffectiveConfiguration()
8383
{
8484
var currentBranchConfig = BranchConfigurationCalculator.GetBranchConfiguration(CurrentCommit, Repository, OnlyEvaluateTrackedBranches, FullConfiguration, CurrentBranch);
8585

86-
if (!currentBranchConfig.Value.VersioningMode.HasValue)
87-
throw new Exception(string.Format("Configuration value for 'Versioning mode' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Key));
88-
if (!currentBranchConfig.Value.Increment.HasValue)
89-
throw new Exception(string.Format("Configuration value for 'Increment' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Key));
90-
if (!currentBranchConfig.Value.PreventIncrementOfMergedBranchVersion.HasValue)
91-
throw new Exception(string.Format("Configuration value for 'PreventIncrementOfMergedBranchVersion' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Key));
92-
if (!currentBranchConfig.Value.TrackMergeTarget.HasValue)
93-
throw new Exception(string.Format("Configuration value for 'TrackMergeTarget' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Key));
94-
if (!currentBranchConfig.Value.TracksReleaseBranches.HasValue)
95-
throw new Exception(string.Format("Configuration value for 'TracksReleaseBranches' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Key));
96-
if (!currentBranchConfig.Value.IsReleaseBranch.HasValue)
97-
throw new Exception(string.Format("Configuration value for 'IsReleaseBranch' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Key));
86+
if (!currentBranchConfig.VersioningMode.HasValue)
87+
throw new Exception(string.Format("Configuration value for 'Versioning mode' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Name));
88+
if (!currentBranchConfig.Increment.HasValue)
89+
throw new Exception(string.Format("Configuration value for 'Increment' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Name));
90+
if (!currentBranchConfig.PreventIncrementOfMergedBranchVersion.HasValue)
91+
throw new Exception(string.Format("Configuration value for 'PreventIncrementOfMergedBranchVersion' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Name));
92+
if (!currentBranchConfig.TrackMergeTarget.HasValue)
93+
throw new Exception(string.Format("Configuration value for 'TrackMergeTarget' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Name));
94+
if (!currentBranchConfig.TracksReleaseBranches.HasValue)
95+
throw new Exception(string.Format("Configuration value for 'TracksReleaseBranches' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Name));
96+
if (!currentBranchConfig.IsReleaseBranch.HasValue)
97+
throw new Exception(string.Format("Configuration value for 'IsReleaseBranch' for branch {0} has no value. (this should not happen, please report an issue)", currentBranchConfig.Name));
9898

9999
if (!FullConfiguration.AssemblyVersioningScheme.HasValue)
100100
throw new Exception("Configuration value for 'AssemblyVersioningScheme' has no value. (this should not happen, please report an issue)");
@@ -107,12 +107,12 @@ void CalculateEffectiveConfiguration()
107107
if (!FullConfiguration.CommitsSinceVersionSourcePadding.HasValue)
108108
throw new Exception("Configuration value for 'CommitsSinceVersionSourcePadding' has no value. (this should not happen, please report an issue)");
109109

110-
var versioningMode = currentBranchConfig.Value.VersioningMode.Value;
111-
var tag = currentBranchConfig.Value.Tag;
112-
var tagNumberPattern = currentBranchConfig.Value.TagNumberPattern;
113-
var incrementStrategy = currentBranchConfig.Value.Increment.Value;
114-
var preventIncrementForMergedBranchVersion = currentBranchConfig.Value.PreventIncrementOfMergedBranchVersion.Value;
115-
var trackMergeTarget = currentBranchConfig.Value.TrackMergeTarget.Value;
110+
var versioningMode = currentBranchConfig.VersioningMode.Value;
111+
var tag = currentBranchConfig.Tag;
112+
var tagNumberPattern = currentBranchConfig.TagNumberPattern;
113+
var incrementStrategy = currentBranchConfig.Increment.Value;
114+
var preventIncrementForMergedBranchVersion = currentBranchConfig.PreventIncrementOfMergedBranchVersion.Value;
115+
var trackMergeTarget = currentBranchConfig.TrackMergeTarget.Value;
116116

117117
var nextVersion = FullConfiguration.NextVersion;
118118
var assemblyVersioningScheme = FullConfiguration.AssemblyVersioningScheme.Value;
@@ -123,12 +123,12 @@ void CalculateEffectiveConfiguration()
123123
var patchMessage = FullConfiguration.PatchVersionBumpMessage;
124124
var noBumpMessage = FullConfiguration.NoBumpMessage;
125125

126-
var commitMessageVersionBump = currentBranchConfig.Value.CommitMessageIncrementing ?? FullConfiguration.CommitMessageIncrementing.Value;
126+
var commitMessageVersionBump = currentBranchConfig.CommitMessageIncrementing ?? FullConfiguration.CommitMessageIncrementing.Value;
127127

128128
Configuration = new EffectiveConfiguration(
129129
assemblyVersioningScheme, assemblyInformationalFormat, versioningMode, gitTagPrefix,
130130
tag, nextVersion, incrementStrategy,
131-
currentBranchConfig.Value.Regex,
131+
currentBranchConfig.Regex,
132132
preventIncrementForMergedBranchVersion,
133133
tagNumberPattern, FullConfiguration.ContinuousDeploymentFallbackTag,
134134
trackMergeTarget,
@@ -138,8 +138,8 @@ void CalculateEffectiveConfiguration()
138138
FullConfiguration.BuildMetaDataPadding.Value,
139139
FullConfiguration.CommitsSinceVersionSourcePadding.Value,
140140
FullConfiguration.Ignore.ToFilters(),
141-
currentBranchConfig.Value.TracksReleaseBranches.Value,
142-
currentBranchConfig.Value.IsReleaseBranch.Value);
141+
currentBranchConfig.TracksReleaseBranches.Value,
142+
currentBranchConfig.IsReleaseBranch.Value);
143143
}
144144
}
145145
}

0 commit comments

Comments
 (0)