-
Notifications
You must be signed in to change notification settings - Fork 652
/
BranchConfigurationCalculator.cs
245 lines (220 loc) · 12.3 KB
/
BranchConfigurationCalculator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using GitVersion.Common;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.Model.Configuration;
using LibGit2Sharp;
namespace GitVersion.Configuration
{
public class BranchConfigurationCalculator : IBranchConfigurationCalculator
{
private const string FallbackConfigName = "Fallback";
private readonly ILog log;
private readonly IRepositoryMetadataProvider repositoryMetadataProvider;
public BranchConfigurationCalculator(ILog log, IRepositoryMetadataProvider repositoryMetadataProvider)
{
this.log = log ?? throw new ArgumentNullException(nameof(log));
this.repositoryMetadataProvider = repositoryMetadataProvider ?? throw new ArgumentNullException(nameof(repositoryMetadataProvider));
}
/// <summary>
/// Gets the <see cref="BranchConfig"/> for the current commit.
/// </summary>
public BranchConfig GetBranchConfiguration(Branch targetBranch, Commit currentCommit, Config configuration, IList<Branch> excludedInheritBranches = null)
{
var matchingBranches = configuration.GetConfigForBranch(targetBranch.NameWithoutRemote());
if (matchingBranches == null)
{
log.Info($"No branch configuration found for branch {targetBranch.FriendlyName}, falling back to default configuration");
matchingBranches = BranchConfig.CreateDefaultBranchConfig(FallbackConfigName)
.Apply(new BranchConfig
{
Regex = "",
VersioningMode = configuration.VersioningMode,
Increment = configuration.Increment ?? IncrementStrategy.Inherit,
});
}
if (matchingBranches.Increment == IncrementStrategy.Inherit)
{
matchingBranches = InheritBranchConfiguration(targetBranch, matchingBranches, currentCommit, configuration, excludedInheritBranches);
if (matchingBranches.Name.IsEquivalentTo(FallbackConfigName) && matchingBranches.Increment == IncrementStrategy.Inherit)
{
// We tried, and failed to inherit, just fall back to patch
matchingBranches.Increment = IncrementStrategy.Patch;
}
}
return matchingBranches;
}
// TODO I think we need to take a fresh approach to this.. it's getting really complex with heaps of edge cases
private BranchConfig InheritBranchConfiguration(Branch targetBranch, BranchConfig branchConfiguration, Commit currentCommit, Config configuration, IList<Branch> excludedInheritBranches)
{
using (log.IndentLog("Attempting to inherit branch configuration from parent branch"))
{
var excludedBranches = new[] { targetBranch };
// Check if we are a merge commit. If so likely we are a pull request
var parentCount = currentCommit.Parents.Count();
if (parentCount == 2)
{
excludedBranches = CalculateWhenMultipleParents(currentCommit, ref targetBranch, excludedBranches);
}
excludedInheritBranches ??= repositoryMetadataProvider.GetExcludedInheritBranches(configuration);
// Add new excluded branches.
foreach (var excludedBranch in excludedBranches.ExcludingBranches(excludedInheritBranches))
{
excludedInheritBranches.Add(excludedBranch);
}
var branchesToEvaluate = repositoryMetadataProvider.ExcludingBranches(excludedInheritBranches).ToList();
var branchPoint = repositoryMetadataProvider
.FindCommitBranchWasBranchedFrom(targetBranch, configuration, excludedInheritBranches.ToArray());
List<Branch> possibleParents;
if (branchPoint == BranchCommit.Empty)
{
possibleParents = repositoryMetadataProvider.GetBranchesContainingCommit(targetBranch.Tip, branchesToEvaluate)
// It fails to inherit Increment branch configuration if more than 1 parent;
// therefore no point to get more than 2 parents
.Take(2)
.ToList();
}
else
{
var branches = repositoryMetadataProvider.GetBranchesContainingCommit(branchPoint.Commit, branchesToEvaluate).ToList();
if (branches.Count > 1)
{
var currentTipBranches = repositoryMetadataProvider.GetBranchesContainingCommit(currentCommit, branchesToEvaluate).ToList();
possibleParents = branches.Except(currentTipBranches).ToList();
}
else
{
possibleParents = branches;
}
}
log.Info("Found possible parent branches: " + string.Join(", ", possibleParents.Select(p => p.FriendlyName)));
if (possibleParents.Count == 1)
{
var branchConfig = GetBranchConfiguration(possibleParents[0], currentCommit, configuration, excludedInheritBranches);
// If we have resolved a fallback config we should not return that we have got config
if (branchConfig.Name != FallbackConfigName)
{
return new BranchConfig(branchConfiguration)
{
Increment = branchConfig.Increment,
PreventIncrementOfMergedBranchVersion = branchConfig.PreventIncrementOfMergedBranchVersion,
// If we are inheriting from develop then we should behave like develop
TracksReleaseBranches = branchConfig.TracksReleaseBranches
};
}
}
// 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
// if develop exists and master if not
var errorMessage = possibleParents.Count == 0
? "Failed to inherit Increment branch configuration, no branches found."
: "Failed to inherit Increment branch configuration, ended up with: " + string.Join(", ", possibleParents.Select(p => p.FriendlyName));
var chosenBranch = repositoryMetadataProvider.GetChosenBranch(configuration);
if (chosenBranch == null)
{
// TODO We should call the build server to generate this exception, each build server works differently
// for fetch issues and we could give better warnings.
throw new InvalidOperationException("Could not find a 'develop' or 'master' branch, neither locally nor remotely.");
}
var branchName = chosenBranch.FriendlyName;
log.Warning(errorMessage + System.Environment.NewLine + "Falling back to " + branchName + " branch config");
// To prevent infinite loops, make sure that a new branch was chosen.
if (targetBranch.IsSameBranch(chosenBranch))
{
var developOrMasterConfig =
ChooseMasterOrDevelopIncrementStrategyIfTheChosenBranchIsOneOfThem(
chosenBranch, branchConfiguration, configuration);
if (developOrMasterConfig != null)
{
return developOrMasterConfig;
}
log.Warning("Fallback branch wants to inherit Increment branch configuration from itself. Using patch increment instead.");
return new BranchConfig(branchConfiguration)
{
Increment = IncrementStrategy.Patch
};
}
var inheritingBranchConfig = GetBranchConfiguration(chosenBranch, currentCommit, configuration, excludedInheritBranches);
var configIncrement = inheritingBranchConfig.Increment;
if (inheritingBranchConfig.Name.IsEquivalentTo(FallbackConfigName) && configIncrement == IncrementStrategy.Inherit)
{
log.Warning("Fallback config inherits by default, dropping to patch increment");
configIncrement = IncrementStrategy.Patch;
}
return new BranchConfig(branchConfiguration)
{
Increment = configIncrement,
PreventIncrementOfMergedBranchVersion = inheritingBranchConfig.PreventIncrementOfMergedBranchVersion,
// If we are inheriting from develop then we should behave like develop
TracksReleaseBranches = inheritingBranchConfig.TracksReleaseBranches
};
}
}
private Branch[] CalculateWhenMultipleParents(Commit currentCommit, ref Branch currentBranch, Branch[] excludedBranches)
{
var parents = currentCommit.Parents.ToArray();
var branches = repositoryMetadataProvider.GetBranchesForCommit(parents[1]);
if (branches.Count == 1)
{
var branch = branches[0];
excludedBranches = new[]
{
currentBranch,
branch
};
currentBranch = branch;
}
else if (branches.Count > 1)
{
currentBranch = branches.FirstOrDefault(b => b.NameWithoutRemote() == "master") ?? branches.First();
}
else
{
var possibleTargetBranches = repositoryMetadataProvider.GetBranchesForCommit(parents[0]);
if (possibleTargetBranches.Count > 1)
{
currentBranch = possibleTargetBranches.FirstOrDefault(b => b.NameWithoutRemote() == "master") ?? possibleTargetBranches.First();
}
else
{
currentBranch = possibleTargetBranches.FirstOrDefault() ?? currentBranch;
}
}
log.Info("HEAD is merge commit, this is likely a pull request using " + currentBranch.FriendlyName + " as base");
return excludedBranches;
}
private static BranchConfig ChooseMasterOrDevelopIncrementStrategyIfTheChosenBranchIsOneOfThem(Branch chosenBranch, BranchConfig branchConfiguration, Config config)
{
BranchConfig masterOrDevelopConfig = null;
var developBranchRegex = config.Branches[Config.DevelopBranchKey].Regex;
var masterBranchRegex = config.Branches[Config.MasterBranchKey].Regex;
if (Regex.IsMatch(chosenBranch.FriendlyName, developBranchRegex, RegexOptions.IgnoreCase))
{
// Normally we would not expect this to happen but for safety we add a check
if (config.Branches[Config.DevelopBranchKey].Increment !=
IncrementStrategy.Inherit)
{
masterOrDevelopConfig = new BranchConfig(branchConfiguration)
{
Increment = config.Branches[Config.DevelopBranchKey].Increment
};
}
}
else if (Regex.IsMatch(chosenBranch.FriendlyName, masterBranchRegex, RegexOptions.IgnoreCase))
{
// Normally we would not expect this to happen but for safety we add a check
if (config.Branches[Config.MasterBranchKey].Increment !=
IncrementStrategy.Inherit)
{
masterOrDevelopConfig = new BranchConfig(branchConfiguration)
{
Increment = config.Branches[Config.DevelopBranchKey].Increment
};
}
}
return masterOrDevelopConfig;
}
}
}