Skip to content

Commit

Permalink
Fix GitVersion calculates the wrong version after main is merged back…
Browse files Browse the repository at this point in the history
… to develop.
  • Loading branch information
HHobeck committed Feb 26, 2024
1 parent 912988a commit 0ce2b07
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 18 deletions.
7 changes: 0 additions & 7 deletions src/GitVersion.Configuration/BranchConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,4 @@ public virtual IBranchConfiguration Inherit(IBranchConfiguration configuration)
PreReleaseWeight = PreReleaseWeight ?? configuration.PreReleaseWeight
};
}

public IBranchConfiguration Empty() => new BranchConfiguration
{
RegularExpression = string.Empty,
Label = ConfigurationConstants.BranchNamePlaceholder,
Increment = IncrementStrategy.Inherit
};
}
7 changes: 7 additions & 0 deletions src/GitVersion.Configuration/GitVersionConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,11 @@ public string ToJsonString()
stream.Flush();
return stringBuilder.ToString();
}

public IBranchConfiguration GetEmptyBranchConfiguration() => new BranchConfiguration
{
RegularExpression = string.Empty,
Label = ConfigurationConstants.BranchNamePlaceholder,
Increment = IncrementStrategy.Inherit
};
}
53 changes: 53 additions & 0 deletions src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,4 +1218,57 @@ public void EnsurePreventIncrementWhenCurrentCommitTaggedOnReleaseBranchAndIncre

fixture.AssertFullSemver(semVersion, configuration);
}

[TestCase(false, "2.0.0-alpha.3")]
[TestCase(true, "2.0.0-alpha.2")]
public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrectForTrunkBased(bool applyTag, string semanticVersion)
{
var configuration = GitFlowConfigurationBuilder.New
.WithVersionStrategy(VersionStrategies.TrunkBased)
.Build();

using var fixture = new EmptyRepositoryFixture("main");

fixture.MakeACommit("A");
fixture.ApplyTag("1.0.0");
fixture.BranchTo("develop");
fixture.MakeACommit("B +semver: major");

// ✅ succeeds as expected
fixture.AssertFullSemver("2.0.0-alpha.1", configuration);

fixture.Checkout("main");
fixture.MakeACommit("C");
if (applyTag) fixture.ApplyTag("1.0.1");
fixture.Checkout("develop");
fixture.MergeNoFF("main");

// ✅ succeeds as expected
fixture.AssertFullSemver(semanticVersion, configuration);
}

[Test]
public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrectForGitFlow()
{
var configuration = GitFlowConfigurationBuilder.New.Build();

using var fixture = new EmptyRepositoryFixture("main");

fixture.MakeACommit("A");
fixture.ApplyTag("1.0.0");
fixture.BranchTo("develop");
fixture.MakeACommit("B +semver: major");

// ✅ succeeds as expected
fixture.AssertFullSemver("2.0.0-alpha.1", configuration);

fixture.Checkout("main");
fixture.MakeACommit("C");
fixture.ApplyTag("1.0.1");
fixture.Checkout("develop");
fixture.MergeNoFF("main");

// ✅ succeeds as expected
fixture.AssertFullSemver("2.0.0-alpha.3", configuration);
}
}
2 changes: 0 additions & 2 deletions src/GitVersion.Core/Configuration/IBranchConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,5 @@ public bool IsMatch(string branchName)

IReadOnlyCollection<string> IsSourceBranchFor { get; }

IBranchConfiguration Empty();

IBranchConfiguration Inherit(IBranchConfiguration configuration);
}
2 changes: 2 additions & 0 deletions src/GitVersion.Core/Configuration/IGitVersionConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ public interface IGitVersionConfiguration : IBranchConfiguration
IIgnoreConfiguration Ignore { get; }

string ToJsonString();

IBranchConfiguration GetEmptyBranchConfiguration();
}
2 changes: 1 addition & 1 deletion src/GitVersion.Core/Extensions/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfig
public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfiguration configuration, ReferenceName branchName)
{
var branchConfiguration = GetBranchConfigurations(configuration, branchName.WithoutOrigin).FirstOrDefault();
branchConfiguration ??= configuration.Empty();
branchConfiguration ??= configuration.GetEmptyBranchConfiguration();
return branchConfiguration;
}

Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ GitVersion.Configuration.EffectiveConfiguration.VersionStrategy.get -> GitVersio
GitVersion.Configuration.IBranchConfiguration
GitVersion.Configuration.IBranchConfiguration.CommitMessageIncrementing.get -> GitVersion.VersionCalculation.CommitMessageIncrementMode?
GitVersion.Configuration.IBranchConfiguration.DeploymentMode.get -> GitVersion.VersionCalculation.DeploymentMode?
GitVersion.Configuration.IBranchConfiguration.Empty() -> GitVersion.Configuration.IBranchConfiguration!
GitVersion.Configuration.IBranchConfiguration.Increment.get -> GitVersion.IncrementStrategy
GitVersion.Configuration.IBranchConfiguration.Inherit(GitVersion.Configuration.IBranchConfiguration! configuration) -> GitVersion.Configuration.IBranchConfiguration!
GitVersion.Configuration.IBranchConfiguration.IsMainBranch.get -> bool?
Expand Down Expand Up @@ -178,6 +177,7 @@ GitVersion.Configuration.IGitVersionConfiguration.AssemblyVersioningFormat.get -
GitVersion.Configuration.IGitVersionConfiguration.AssemblyVersioningScheme.get -> GitVersion.Extensions.AssemblyVersioningScheme?
GitVersion.Configuration.IGitVersionConfiguration.Branches.get -> System.Collections.Generic.IReadOnlyDictionary<string!, GitVersion.Configuration.IBranchConfiguration!>!
GitVersion.Configuration.IGitVersionConfiguration.CommitDateFormat.get -> string?
GitVersion.Configuration.IGitVersionConfiguration.GetEmptyBranchConfiguration() -> GitVersion.Configuration.IBranchConfiguration!
GitVersion.Configuration.IGitVersionConfiguration.Ignore.get -> GitVersion.Configuration.IIgnoreConfiguration!
GitVersion.Configuration.IGitVersionConfiguration.TagPreReleaseWeight.get -> int?
GitVersion.Configuration.IGitVersionConfiguration.TagPrefix.get -> string?
Expand Down
21 changes: 14 additions & 7 deletions src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public VersionField DetermineIncrementedField(
configuration.NotNull();

var commitMessageIncrement = FindCommitMessageIncrement(
configuration, baseVersion.BaseVersionSource, currentCommit, label);
configuration, baseVersion, currentCommit, label);

var defaultIncrement = configuration.Increment.ToVersionField();

Expand Down Expand Up @@ -81,31 +81,38 @@ public VersionField DetermineIncrementedField(
}

private VersionField? FindCommitMessageIncrement(
EffectiveConfiguration configuration, ICommit? baseCommit, ICommit? currentCommit, string? label)
EffectiveConfiguration configuration, BaseVersion baseVersion, ICommit? currentCommit, string? label)
{
if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.Disabled)
{
return null;
}

var baseCommit = baseVersion.BaseVersionSource;

//get tags with valid version - depends on configuration (see #3757)
var targetShas = new Lazy<IReadOnlySet<string>>(() =>
this.taggedSemanticVersionRepository.GetTaggedSemanticVersions(configuration.TagPrefix, configuration.SemanticVersionFormat)
.SelectMany(_ => _).Where(_ => _.Value.IsMatchForBranchSpecificLabel(label)).Select(_ => _.Tag.TargetSha).ToHashSet()
.SelectMany(_ => _).Where(_ => _.Value.IsMatchForBranchSpecificLabel(label)).Select(_ => _.Tag.TargetSha).ToHashSet()
);

var commits = GetIntermediateCommits(baseCommit, currentCommit);
var targetTags = this.taggedSemanticVersionRepository
.GetTaggedSemanticVersions(configuration.TagPrefix, configuration.SemanticVersionFormat)
.SelectMany(_ => _).Where(element => element.Value.IsMatchForBranchSpecificLabel(label)
&& element.Value.CompareTo(baseVersion.GetSemanticVersion()) == 0).Select(_ => _.Tag.TargetSha).ToHashSet();

var intermediateCommits = GetIntermediateCommits(baseCommit, currentCommit);
// consider commit messages since latest tag only (see #3071)
commits = commits
var commits = intermediateCommits
.Reverse()
.TakeWhile(x => !targetShas.Value.Contains(x.Sha))
.TakeWhile(x => baseCommit != x)
.Where(element => !targetShas.Value.Contains(element.Sha))
.Reverse();

if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.MergeMessageOnly)
{
commits = commits.Where(c => c.Parents.Count() > 1);
}

return GetIncrementForCommits(
majorVersionBumpMessage: configuration.MajorVersionBumpMessage,
minorVersionBumpMessage: configuration.MinorVersionBumpMessage,
Expand Down

0 comments on commit 0ce2b07

Please sign in to comment.