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
arturcic committed Feb 26, 2024
1 parent 45a307e commit a0c170b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
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);
}
}
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 a0c170b

Please sign in to comment.