Skip to content

Commit

Permalink
Merge pull request #3037 from asbjornu/feature/gh-2590
Browse files Browse the repository at this point in the history
Fall back to `master` if `main` is missing
  • Loading branch information
asbjornu authored Apr 13, 2022
2 parents 4369845 + 719f269 commit 3892733
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 139 deletions.
33 changes: 28 additions & 5 deletions src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,36 @@ public TestEffectiveConfiguration(
bool isRelease = false,
string commitDateFormat = "yyyy-MM-dd",
bool updateBuildNumber = false) :
base(assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch,
branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag,
base(assemblyVersioningScheme,
assemblyFileVersioningScheme,
assemblyInformationalFormat,
assemblyVersioningFormat,
assemblyFileVersioningFormat,
versioningMode,
gitTagPrefix,
tag,
nextVersion,
IncrementStrategy.Patch,
branchPrefixToTrim,
preventIncrementForMergedBranchVersion,
tagNumberPattern,
continuousDeploymentFallbackTag,
trackMergeTarget,
majorMessage, minorMessage, patchMessage, noBumpMessage,
commitMessageMode, legacySemVerPadding, buildMetaDataPadding, commitsSinceVersionSourcePadding,
majorMessage,
minorMessage,
patchMessage,
noBumpMessage,
commitMessageMode,
legacySemVerPadding,
buildMetaDataPadding,
commitsSinceVersionSourcePadding,
versionFilters ?? Enumerable.Empty<IVersionFilter>(),
tracksReleaseBranches, isRelease, commitDateFormat, updateBuildNumber, 0, 0)
tracksReleaseBranches,
isRelease,
commitDateFormat,
updateBuildNumber,
0,
0)
{
}
}
28 changes: 14 additions & 14 deletions src/GitVersion.Core.Tests/Model/CommitDateTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Model.Configuration;
using GitVersion.VersionCalculation;
using NUnit.Framework;

namespace GitVersion.Core.Tests;
Expand All @@ -17,17 +14,20 @@ public class CommitDateTests : TestBase
public void CommitDateFormatTest(string format, string expectedOutcome)
{
var date = new DateTime(2017, 10, 6);

var formatValues = new SemanticVersionFormatValues(
new SemanticVersion
{
BuildMetaData = new SemanticVersionBuildMetaData("950d2f830f5a2af12a6779a48d20dcbb02351f25", 0, MainBranch, "3139d4eeb044f46057693473eacc2655b3b27e7d", "3139d4eeb", new DateTimeOffset(date, TimeSpan.Zero), 0) // assume time zone is UTC

},
new EffectiveConfiguration(
AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme.MajorMinorPatch, "", "", "", VersioningMode.ContinuousDelivery, "", "", "", IncrementStrategy.Inherit,
"", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty<IVersionFilter>(), false, true, format, false, 0, 0)
);
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData(
"950d2f830f5a2af12a6779a48d20dcbb02351f25",
0,
MainBranch,
"3139d4eeb044f46057693473eacc2655b3b27e7d",
"3139d4eeb",
new DateTimeOffset(date, TimeSpan.Zero),
0);
var semanticVersion = new SemanticVersion
{
BuildMetaData = semanticVersionBuildMetaData // assume time zone is UTC
};
var configuration = new TestEffectiveConfiguration(commitDateFormat: format);
var formatValues = new SemanticVersionFormatValues(semanticVersion, configuration);

Assert.That(formatValues.CommitDate, Is.EqualTo(expectedOutcome));
}
Expand Down
76 changes: 0 additions & 76 deletions src/GitVersion.Core/Configuration/ConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,82 +42,6 @@ public static class ConfigExtensions

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

public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config configuration, BranchConfig currentBranchConfig)
{
var name = currentBranchConfig.Name;
if (!currentBranchConfig.VersioningMode.HasValue)
throw new Exception($"Configuration value for 'Versioning mode' for branch {name} has no value. (this should not happen, please report an issue)");
if (!currentBranchConfig.Increment.HasValue)
throw new Exception($"Configuration value for 'Increment' for branch {name} has no value. (this should not happen, please report an issue)");
if (!currentBranchConfig.PreventIncrementOfMergedBranchVersion.HasValue)
throw new Exception($"Configuration value for 'PreventIncrementOfMergedBranchVersion' for branch {name} has no value. (this should not happen, please report an issue)");
if (!currentBranchConfig.TrackMergeTarget.HasValue)
throw new Exception($"Configuration value for 'TrackMergeTarget' for branch {name} has no value. (this should not happen, please report an issue)");
if (!currentBranchConfig.TracksReleaseBranches.HasValue)
throw new Exception($"Configuration value for 'TracksReleaseBranches' for branch {name} has no value. (this should not happen, please report an issue)");
if (!currentBranchConfig.IsReleaseBranch.HasValue)
throw new Exception($"Configuration value for 'IsReleaseBranch' for branch {name} has no value. (this should not happen, please report an issue)");

if (!configuration.AssemblyVersioningScheme.HasValue)
throw new Exception("Configuration value for 'AssemblyVersioningScheme' has no value. (this should not happen, please report an issue)");
if (!configuration.AssemblyFileVersioningScheme.HasValue)
throw new Exception("Configuration value for 'AssemblyFileVersioningScheme' has no value. (this should not happen, please report an issue)");
if (!configuration.CommitMessageIncrementing.HasValue)
throw new Exception("Configuration value for 'CommitMessageIncrementing' has no value. (this should not happen, please report an issue)");
if (!configuration.LegacySemVerPadding.HasValue)
throw new Exception("Configuration value for 'LegacySemVerPadding' has no value. (this should not happen, please report an issue)");
if (!configuration.BuildMetaDataPadding.HasValue)
throw new Exception("Configuration value for 'BuildMetaDataPadding' has no value. (this should not happen, please report an issue)");
if (!configuration.CommitsSinceVersionSourcePadding.HasValue)
throw new Exception("Configuration value for 'CommitsSinceVersionSourcePadding' has no value. (this should not happen, please report an issue)");
if (!configuration.TagPreReleaseWeight.HasValue)
throw new Exception("Configuration value for 'TagPreReleaseWeight' has no value. (this should not happen, please report an issue)");

var versioningMode = currentBranchConfig.VersioningMode.Value;
var tag = currentBranchConfig.Tag;
var tagNumberPattern = currentBranchConfig.TagNumberPattern;
var incrementStrategy = currentBranchConfig.Increment.Value;
var preventIncrementForMergedBranchVersion = currentBranchConfig.PreventIncrementOfMergedBranchVersion.Value;
var trackMergeTarget = currentBranchConfig.TrackMergeTarget.Value;
var preReleaseWeight = currentBranchConfig.PreReleaseWeight ?? 0;

var nextVersion = configuration.NextVersion;
var assemblyVersioningScheme = configuration.AssemblyVersioningScheme.Value;
var assemblyFileVersioningScheme = configuration.AssemblyFileVersioningScheme.Value;
var assemblyInformationalFormat = configuration.AssemblyInformationalFormat;
var assemblyVersioningFormat = configuration.AssemblyVersioningFormat;
var assemblyFileVersioningFormat = configuration.AssemblyFileVersioningFormat;
var gitTagPrefix = configuration.TagPrefix;
var majorMessage = configuration.MajorVersionBumpMessage;
var minorMessage = configuration.MinorVersionBumpMessage;
var patchMessage = configuration.PatchVersionBumpMessage;
var noBumpMessage = configuration.NoBumpMessage;
var commitDateFormat = configuration.CommitDateFormat;
var updateBuildNumber = configuration.UpdateBuildNumber ?? true;
var tagPreReleaseWeight = configuration.TagPreReleaseWeight.Value;

var commitMessageVersionBump = currentBranchConfig.CommitMessageIncrementing ?? configuration.CommitMessageIncrementing.Value;
return new EffectiveConfiguration(
assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix,
tag, nextVersion, incrementStrategy,
currentBranchConfig.Regex,
preventIncrementForMergedBranchVersion,
tagNumberPattern, configuration.ContinuousDeploymentFallbackTag,
trackMergeTarget,
majorMessage, minorMessage, patchMessage, noBumpMessage,
commitMessageVersionBump,
configuration.LegacySemVerPadding.Value,
configuration.BuildMetaDataPadding.Value,
configuration.CommitsSinceVersionSourcePadding.Value,
configuration.Ignore.ToFilters(),
currentBranchConfig.TracksReleaseBranches.Value,
currentBranchConfig.IsReleaseBranch.Value,
commitDateFormat,
updateBuildNumber,
preReleaseWeight,
tagPreReleaseWeight);
}

public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string? branchFriendlyName, string? branchNameOverride)
{
var tagToUse = configuration.Tag ?? "{BranchName}";
Expand Down
1 change: 1 addition & 0 deletions src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface IRepositoryStore

IBranch GetTargetBranch(string? targetBranchName);
IBranch? FindBranch(string? branchName);
IBranch? FindMainBranch(Config configuration);
IBranch? GetChosenBranch(Config configuration);
IEnumerable<IBranch> GetBranchesForCommit(ICommit commit);
IEnumerable<IBranch> GetExcludedInheritBranches(Config configuration);
Expand Down
3 changes: 2 additions & 1 deletion src/GitVersion.Core/Core/GitVersionContextFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GitVersion.Common;
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Model.Configuration;
using Microsoft.Extensions.Options;

namespace GitVersion;
Expand Down Expand Up @@ -36,7 +37,7 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
}

var currentBranchConfig = this.branchConfigurationCalculator.GetBranchConfiguration(currentBranch, currentCommit, configuration);
var effectiveConfiguration = configuration.CalculateEffectiveConfiguration(currentBranchConfig);
var effectiveConfiguration = new EffectiveConfiguration(configuration, currentBranchConfig);
var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, effectiveConfiguration);
var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges();

Expand Down
14 changes: 14 additions & 0 deletions src/GitVersion.Core/Core/RepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ public IBranch GetTargetBranch(string? targetBranchName)

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

public IBranch? FindMainBranch(Config configuration)
{
var mainBranchRegex = configuration.Branches[Config.MainBranchKey]?.Regex
?? configuration.Branches[Config.MasterBranchKey]?.Regex;

if (mainBranchRegex == null)
{
return FindBranch(Config.MainBranchKey) ?? FindBranch(Config.MasterBranchKey);
}

return this.repository.Branches.FirstOrDefault(b =>
Regex.IsMatch(b.Name.Friendly, mainBranchRegex, RegexOptions.IgnoreCase));
}

public IBranch? GetChosenBranch(Config configuration)
{
var developBranchRegex = configuration.Branches[Config.DevelopBranchKey]?.Regex;
Expand Down
Loading

0 comments on commit 3892733

Please sign in to comment.