Skip to content

Commit 047de5c

Browse files
committed
Simplified getting branch configuration
1 parent 286ec69 commit 047de5c

File tree

3 files changed

+40
-42
lines changed

3 files changed

+40
-42
lines changed

src/GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,21 @@ public class BranchConfigurationCalculator
1313
/// </summary>
1414
public static BranchConfig GetBranchConfiguration(GitVersionContext context, Branch targetBranch, IList<Branch> excludedInheritBranches = null)
1515
{
16-
var matchingBranches = LookupBranchConfiguration(context.FullConfiguration, targetBranch).ToArray();
17-
18-
BranchConfig branchConfiguration;
19-
if (matchingBranches.Length > 0)
20-
{
21-
branchConfiguration = matchingBranches[0];
22-
23-
if (matchingBranches.Length > 1)
24-
{
25-
Logger.WriteWarning(string.Format(
26-
"Multiple branch configurations match the current branch branchName of '{0}'. Using the first matching configuration, '{1}'. Matching configurations include: '{2}'",
27-
targetBranch.FriendlyName,
28-
branchConfiguration.Name,
29-
string.Join("', '", matchingBranches.Select(b => b.Name))));
30-
}
31-
}
32-
else
16+
var matchingBranches = context.FullConfiguration.GetConfigForBranch(targetBranch.FriendlyName);
17+
18+
if (matchingBranches == null)
3319
{
3420
Logger.WriteInfo(string.Format(
3521
"No branch configuration found for branch {0}, falling back to default configuration",
3622
targetBranch.FriendlyName));
3723

38-
branchConfiguration = new BranchConfig { Name = string.Empty };
39-
ConfigurationProvider.ApplyBranchDefaults(context.FullConfiguration, branchConfiguration, "");
40-
}
41-
42-
return branchConfiguration.Increment == IncrementStrategy.Inherit ?
43-
InheritBranchConfiguration(context, targetBranch, branchConfiguration, excludedInheritBranches) :
44-
branchConfiguration;
45-
}
46-
47-
static IEnumerable<BranchConfig> LookupBranchConfiguration(Config config, Branch currentBranch)
48-
{
49-
if (config == null)
50-
{
51-
throw new ArgumentNullException(nameof(config));
52-
}
53-
54-
if (currentBranch == null)
55-
{
56-
throw new ArgumentNullException(nameof(currentBranch));
24+
matchingBranches = new BranchConfig { Name = string.Empty };
25+
ConfigurationProvider.ApplyBranchDefaults(context.FullConfiguration, matchingBranches, "");
5726
}
5827

59-
return config.Branches.Where(b => Regex.IsMatch(currentBranch.FriendlyName, "^" + b.Value.Regex, RegexOptions.IgnoreCase)).Select(kvp => kvp.Value);
28+
return matchingBranches.Increment == IncrementStrategy.Inherit ?
29+
InheritBranchConfiguration(context, targetBranch, matchingBranches, excludedInheritBranches) :
30+
matchingBranches;
6031
}
6132

6233
static BranchConfig InheritBranchConfiguration(GitVersionContext context, Branch targetBranch, BranchConfig branchConfiguration, IList<Branch> excludedInheritBranches)
@@ -77,11 +48,9 @@ static BranchConfig InheritBranchConfiguration(GitVersionContext context, Branch
7748
{
7849
excludedInheritBranches = repository.Branches.Where(b =>
7950
{
80-
var branchConfig = LookupBranchConfiguration(config, b).ToArray();
51+
var branchConfig = config.GetConfigForBranch(b.FriendlyName);
8152

82-
// NOTE: if length is 0 we couldn't find the configuration for the branch e.g. "origin/master"
83-
// NOTE: if the length is greater than 1 we cannot decide which merge strategy to pick
84-
return (branchConfig.Length != 1) || (branchConfig.Length == 1 && branchConfig[0].Increment == IncrementStrategy.Inherit);
53+
return branchConfig != null && branchConfig.Increment == IncrementStrategy.Inherit;
8554
}).ToList();
8655
}
8756
// Add new excluded branches.

src/GitVersionCore/Configuration/Config.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
namespace GitVersion
22
{
3+
using System;
34
using System.Collections.Generic;
45
using System.Globalization;
56
using System.Linq;
7+
using System.Text.RegularExpressions;
68
using YamlDotNet.Serialization;
79

810
public class Config
@@ -89,6 +91,33 @@ public Dictionary<string, BranchConfig> Branches
8991
}
9092
}
9193

94+
public BranchConfig GetConfigForBranch(string branchName)
95+
{
96+
if (branchName == null) throw new ArgumentNullException(nameof(branchName));
97+
var matches = Branches
98+
.Where(b => Regex.IsMatch(branchName, "^" + b.Value.Regex, RegexOptions.IgnoreCase));
99+
100+
try
101+
{
102+
return matches
103+
.Select(kvp => kvp.Value)
104+
.SingleOrDefault();
105+
}
106+
catch (InvalidOperationException)
107+
{
108+
var matchingConfigs = string.Join("\n - ", matches.Select(m => m.Key));
109+
var picked = matches
110+
.Select(kvp => kvp.Value)
111+
.First();
112+
113+
Logger.WriteWarning(
114+
$"Multiple branch configurations match the current branch branchName of '{branchName}'. " +
115+
$"Using the first matching configuration, '{picked}'. Matching configurations include: '{matchingConfigs}'");
116+
117+
return picked;
118+
}
119+
}
120+
92121
T MergeObjects<T>(T target, T source)
93122
{
94123
typeof(T).GetProperties()

src/GitVersionCore/MergeMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MergeMessage(string mergeMessage, Config config)
3131
}
3232

3333
MergedBranch = ParseBranch();
34-
34+
3535
// Remove remotes and branch prefixes like release/ feature/ hotfix/ etc
3636
var toMatch = Regex.Replace(MergedBranch, @"^(\w+[-/])*", "", RegexOptions.IgnoreCase);
3737
toMatch = Regex.Replace(toMatch, $"^{config.TagPrefix}", "");

0 commit comments

Comments
 (0)