Skip to content

Commit 3892733

Browse files
authored
Merge pull request #3037 from asbjornu/feature/gh-2590
Fall back to `master` if `main` is missing
2 parents 4369845 + 719f269 commit 3892733

File tree

8 files changed

+188
-139
lines changed

8 files changed

+188
-139
lines changed

src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs

+28-5
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,36 @@ public TestEffectiveConfiguration(
3434
bool isRelease = false,
3535
string commitDateFormat = "yyyy-MM-dd",
3636
bool updateBuildNumber = false) :
37-
base(assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch,
38-
branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag,
37+
base(assemblyVersioningScheme,
38+
assemblyFileVersioningScheme,
39+
assemblyInformationalFormat,
40+
assemblyVersioningFormat,
41+
assemblyFileVersioningFormat,
42+
versioningMode,
43+
gitTagPrefix,
44+
tag,
45+
nextVersion,
46+
IncrementStrategy.Patch,
47+
branchPrefixToTrim,
48+
preventIncrementForMergedBranchVersion,
49+
tagNumberPattern,
50+
continuousDeploymentFallbackTag,
3951
trackMergeTarget,
40-
majorMessage, minorMessage, patchMessage, noBumpMessage,
41-
commitMessageMode, legacySemVerPadding, buildMetaDataPadding, commitsSinceVersionSourcePadding,
52+
majorMessage,
53+
minorMessage,
54+
patchMessage,
55+
noBumpMessage,
56+
commitMessageMode,
57+
legacySemVerPadding,
58+
buildMetaDataPadding,
59+
commitsSinceVersionSourcePadding,
4260
versionFilters ?? Enumerable.Empty<IVersionFilter>(),
43-
tracksReleaseBranches, isRelease, commitDateFormat, updateBuildNumber, 0, 0)
61+
tracksReleaseBranches,
62+
isRelease,
63+
commitDateFormat,
64+
updateBuildNumber,
65+
0,
66+
0)
4467
{
4568
}
4669
}

src/GitVersion.Core.Tests/Model/CommitDateTests.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using GitVersion.Core.Tests.Helpers;
2-
using GitVersion.Extensions;
3-
using GitVersion.Model.Configuration;
4-
using GitVersion.VersionCalculation;
52
using NUnit.Framework;
63

74
namespace GitVersion.Core.Tests;
@@ -17,17 +14,20 @@ public class CommitDateTests : TestBase
1714
public void CommitDateFormatTest(string format, string expectedOutcome)
1815
{
1916
var date = new DateTime(2017, 10, 6);
20-
21-
var formatValues = new SemanticVersionFormatValues(
22-
new SemanticVersion
23-
{
24-
BuildMetaData = new SemanticVersionBuildMetaData("950d2f830f5a2af12a6779a48d20dcbb02351f25", 0, MainBranch, "3139d4eeb044f46057693473eacc2655b3b27e7d", "3139d4eeb", new DateTimeOffset(date, TimeSpan.Zero), 0) // assume time zone is UTC
25-
26-
},
27-
new EffectiveConfiguration(
28-
AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme.MajorMinorPatch, "", "", "", VersioningMode.ContinuousDelivery, "", "", "", IncrementStrategy.Inherit,
29-
"", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty<IVersionFilter>(), false, true, format, false, 0, 0)
30-
);
17+
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData(
18+
"950d2f830f5a2af12a6779a48d20dcbb02351f25",
19+
0,
20+
MainBranch,
21+
"3139d4eeb044f46057693473eacc2655b3b27e7d",
22+
"3139d4eeb",
23+
new DateTimeOffset(date, TimeSpan.Zero),
24+
0);
25+
var semanticVersion = new SemanticVersion
26+
{
27+
BuildMetaData = semanticVersionBuildMetaData // assume time zone is UTC
28+
};
29+
var configuration = new TestEffectiveConfiguration(commitDateFormat: format);
30+
var formatValues = new SemanticVersionFormatValues(semanticVersion, configuration);
3131

3232
Assert.That(formatValues.CommitDate, Is.EqualTo(expectedOutcome));
3333
}

src/GitVersion.Core/Configuration/ConfigExtensions.cs

-76
Original file line numberDiff line numberDiff line change
@@ -42,82 +42,6 @@ public static class ConfigExtensions
4242

4343
public static bool IsReleaseBranch(this Config config, string branchName) => config.GetConfigForBranch(branchName)?.IsReleaseBranch ?? false;
4444

45-
public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config configuration, BranchConfig currentBranchConfig)
46-
{
47-
var name = currentBranchConfig.Name;
48-
if (!currentBranchConfig.VersioningMode.HasValue)
49-
throw new Exception($"Configuration value for 'Versioning mode' for branch {name} has no value. (this should not happen, please report an issue)");
50-
if (!currentBranchConfig.Increment.HasValue)
51-
throw new Exception($"Configuration value for 'Increment' for branch {name} has no value. (this should not happen, please report an issue)");
52-
if (!currentBranchConfig.PreventIncrementOfMergedBranchVersion.HasValue)
53-
throw new Exception($"Configuration value for 'PreventIncrementOfMergedBranchVersion' for branch {name} has no value. (this should not happen, please report an issue)");
54-
if (!currentBranchConfig.TrackMergeTarget.HasValue)
55-
throw new Exception($"Configuration value for 'TrackMergeTarget' for branch {name} has no value. (this should not happen, please report an issue)");
56-
if (!currentBranchConfig.TracksReleaseBranches.HasValue)
57-
throw new Exception($"Configuration value for 'TracksReleaseBranches' for branch {name} has no value. (this should not happen, please report an issue)");
58-
if (!currentBranchConfig.IsReleaseBranch.HasValue)
59-
throw new Exception($"Configuration value for 'IsReleaseBranch' for branch {name} has no value. (this should not happen, please report an issue)");
60-
61-
if (!configuration.AssemblyVersioningScheme.HasValue)
62-
throw new Exception("Configuration value for 'AssemblyVersioningScheme' has no value. (this should not happen, please report an issue)");
63-
if (!configuration.AssemblyFileVersioningScheme.HasValue)
64-
throw new Exception("Configuration value for 'AssemblyFileVersioningScheme' has no value. (this should not happen, please report an issue)");
65-
if (!configuration.CommitMessageIncrementing.HasValue)
66-
throw new Exception("Configuration value for 'CommitMessageIncrementing' has no value. (this should not happen, please report an issue)");
67-
if (!configuration.LegacySemVerPadding.HasValue)
68-
throw new Exception("Configuration value for 'LegacySemVerPadding' has no value. (this should not happen, please report an issue)");
69-
if (!configuration.BuildMetaDataPadding.HasValue)
70-
throw new Exception("Configuration value for 'BuildMetaDataPadding' has no value. (this should not happen, please report an issue)");
71-
if (!configuration.CommitsSinceVersionSourcePadding.HasValue)
72-
throw new Exception("Configuration value for 'CommitsSinceVersionSourcePadding' has no value. (this should not happen, please report an issue)");
73-
if (!configuration.TagPreReleaseWeight.HasValue)
74-
throw new Exception("Configuration value for 'TagPreReleaseWeight' has no value. (this should not happen, please report an issue)");
75-
76-
var versioningMode = currentBranchConfig.VersioningMode.Value;
77-
var tag = currentBranchConfig.Tag;
78-
var tagNumberPattern = currentBranchConfig.TagNumberPattern;
79-
var incrementStrategy = currentBranchConfig.Increment.Value;
80-
var preventIncrementForMergedBranchVersion = currentBranchConfig.PreventIncrementOfMergedBranchVersion.Value;
81-
var trackMergeTarget = currentBranchConfig.TrackMergeTarget.Value;
82-
var preReleaseWeight = currentBranchConfig.PreReleaseWeight ?? 0;
83-
84-
var nextVersion = configuration.NextVersion;
85-
var assemblyVersioningScheme = configuration.AssemblyVersioningScheme.Value;
86-
var assemblyFileVersioningScheme = configuration.AssemblyFileVersioningScheme.Value;
87-
var assemblyInformationalFormat = configuration.AssemblyInformationalFormat;
88-
var assemblyVersioningFormat = configuration.AssemblyVersioningFormat;
89-
var assemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
90-
var gitTagPrefix = configuration.TagPrefix;
91-
var majorMessage = configuration.MajorVersionBumpMessage;
92-
var minorMessage = configuration.MinorVersionBumpMessage;
93-
var patchMessage = configuration.PatchVersionBumpMessage;
94-
var noBumpMessage = configuration.NoBumpMessage;
95-
var commitDateFormat = configuration.CommitDateFormat;
96-
var updateBuildNumber = configuration.UpdateBuildNumber ?? true;
97-
var tagPreReleaseWeight = configuration.TagPreReleaseWeight.Value;
98-
99-
var commitMessageVersionBump = currentBranchConfig.CommitMessageIncrementing ?? configuration.CommitMessageIncrementing.Value;
100-
return new EffectiveConfiguration(
101-
assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix,
102-
tag, nextVersion, incrementStrategy,
103-
currentBranchConfig.Regex,
104-
preventIncrementForMergedBranchVersion,
105-
tagNumberPattern, configuration.ContinuousDeploymentFallbackTag,
106-
trackMergeTarget,
107-
majorMessage, minorMessage, patchMessage, noBumpMessage,
108-
commitMessageVersionBump,
109-
configuration.LegacySemVerPadding.Value,
110-
configuration.BuildMetaDataPadding.Value,
111-
configuration.CommitsSinceVersionSourcePadding.Value,
112-
configuration.Ignore.ToFilters(),
113-
currentBranchConfig.TracksReleaseBranches.Value,
114-
currentBranchConfig.IsReleaseBranch.Value,
115-
commitDateFormat,
116-
updateBuildNumber,
117-
preReleaseWeight,
118-
tagPreReleaseWeight);
119-
}
120-
12145
public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string? branchFriendlyName, string? branchNameOverride)
12246
{
12347
var tagToUse = configuration.Tag ?? "{BranchName}";

src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface IRepositoryStore
1818

1919
IBranch GetTargetBranch(string? targetBranchName);
2020
IBranch? FindBranch(string? branchName);
21+
IBranch? FindMainBranch(Config configuration);
2122
IBranch? GetChosenBranch(Config configuration);
2223
IEnumerable<IBranch> GetBranchesForCommit(ICommit commit);
2324
IEnumerable<IBranch> GetExcludedInheritBranches(Config configuration);

src/GitVersion.Core/Core/GitVersionContextFactory.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using GitVersion.Common;
22
using GitVersion.Configuration;
33
using GitVersion.Extensions;
4+
using GitVersion.Model.Configuration;
45
using Microsoft.Extensions.Options;
56

67
namespace GitVersion;
@@ -36,7 +37,7 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
3637
}
3738

3839
var currentBranchConfig = this.branchConfigurationCalculator.GetBranchConfiguration(currentBranch, currentCommit, configuration);
39-
var effectiveConfiguration = configuration.CalculateEffectiveConfiguration(currentBranchConfig);
40+
var effectiveConfiguration = new EffectiveConfiguration(configuration, currentBranchConfig);
4041
var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, effectiveConfiguration);
4142
var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges();
4243

src/GitVersion.Core/Core/RepositoryStore.cs

+14
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ public IBranch GetTargetBranch(string? targetBranchName)
126126

127127
public IBranch? FindBranch(string? branchName) => this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(branchName));
128128

129+
public IBranch? FindMainBranch(Config configuration)
130+
{
131+
var mainBranchRegex = configuration.Branches[Config.MainBranchKey]?.Regex
132+
?? configuration.Branches[Config.MasterBranchKey]?.Regex;
133+
134+
if (mainBranchRegex == null)
135+
{
136+
return FindBranch(Config.MainBranchKey) ?? FindBranch(Config.MasterBranchKey);
137+
}
138+
139+
return this.repository.Branches.FirstOrDefault(b =>
140+
Regex.IsMatch(b.Name.Friendly, mainBranchRegex, RegexOptions.IgnoreCase));
141+
}
142+
129143
public IBranch? GetChosenBranch(Config configuration)
130144
{
131145
var developBranchRegex = configuration.Branches[Config.DevelopBranchKey]?.Regex;

0 commit comments

Comments
 (0)