Skip to content

GitVersion calculates the wrong version after main is merged back to develop #3935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 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,87 @@ public void EnsurePreventIncrementWhenCurrentCommitTaggedOnReleaseBranchAndIncre

fixture.AssertFullSemver(semVersion, configuration);
}

[Test]
public void EnsureVersionAfterMainIsMergedBackToDevelopIsCorrect()
{
using EmptyRepositoryFixture fixture = new("main");

fixture.MakeATaggedCommit("1.0.0");
fixture.BranchTo("develop");
fixture.MakeACommit("A");

// ✅ succeeds as expected
fixture.AssertFullSemver("1.1.0-alpha.1");

fixture.Checkout("main");
fixture.MakeACommit("B");
fixture.BranchTo("hotfix/just-a-hotfix");
fixture.MakeACommit("C +semver: major");
fixture.MergeTo("main", removeBranchAfterMerging: true);
fixture.Checkout("develop");
fixture.MakeACommit("D");
fixture.Checkout("main");
fixture.MakeACommit("E");
fixture.ApplyTag("1.0.1");
fixture.Checkout("develop");
fixture.MergeNoFF("main");

// ✅ succeeds as expected
fixture.AssertFullSemver("1.1.0-alpha.7");
}

[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 EmptyRepositoryFixture fixture = new("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);
}

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

using EmptyRepositoryFixture fixture = new("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);
}
}
66 changes: 46 additions & 20 deletions src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,21 @@ public VersionField DetermineIncrementedField(
}

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

//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()
IEnumerable<ICommit> commits = GetCommitHistory(
tagPrefix: configuration.TagPrefix,
semanticVersionFormat: configuration.SemanticVersionFormat,
baseVersionSource: baseVersionSource,
currentCommit: currentCommit,
label: label
);

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

if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.MergeMessageOnly)
{
commits = commits.Where(c => c.Parents.Count() > 1);
Expand All @@ -114,6 +109,41 @@ private static Regex TryGetRegexOrDefault(string? messageRegex, Regex defaultReg
? defaultRegex
: CompiledRegexCache.GetOrAdd(messageRegex, pattern => new(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase));

private IReadOnlyCollection<ICommit> GetCommitHistory(
string? tagPrefix, SemanticVersionFormat semanticVersionFormat, ICommit? baseVersionSource, ICommit currentCommit, string? label)
{
var targetShas = new Lazy<HashSet<string>>(() =>
this.taggedSemanticVersionRepository.GetTaggedSemanticVersions(tagPrefix, semanticVersionFormat)
.SelectMany(_ => _).Where(_ => _.Value.IsMatchForBranchSpecificLabel(label)).Select(_ => _.Tag.TargetSha).ToHashSet()
);

var intermediateCommits = GetIntermediateCommits(baseVersionSource, currentCommit).ToArray();

var commitLog = intermediateCommits.ToDictionary(element => element.Id.Sha);

foreach (var intermediateCommit in intermediateCommits.Reverse())
{
if (targetShas.Value.Contains(intermediateCommit.Sha) && commitLog.Remove(intermediateCommit.Sha))
{
var parentCommits = intermediateCommit.Parents.ToList();
while (parentCommits.Count != 0)
{
List<ICommit> temporaryList = new();
foreach (var parentCommit in parentCommits)
{
if (commitLog.Remove(parentCommit.Sha))
{
temporaryList.AddRange(parentCommit.Parents);
}
}
parentCommits = temporaryList;
}
}
}

return commitLog.Values;
}

/// <summary>
/// Get the sequence of commits in a repository between a <paramref name="baseCommit"/> (exclusive)
/// and a particular <paramref name="headCommit"/> (inclusive)
Expand Down Expand Up @@ -149,7 +179,7 @@ private Dictionary<string, int> GetHeadCommitsMap(ICommit? headCommit) =>
/// </summary>
private ICommit[] GetHeadCommits(ICommit? headCommit) =>
this.headCommitsCache.GetOrAdd(headCommit?.Sha ?? "NULL", () =>
GetCommitsReacheableFromHead(repository, headCommit).ToArray());
GetCommitsReacheableFromHead(headCommit).ToArray());

private VersionField? GetIncrementFromCommit(ICommit commit, Regex majorRegex, Regex minorRegex, Regex patchRegex, Regex none) =>
this.commitIncrementCache.GetOrAdd(commit.Sha, () =>
Expand All @@ -164,19 +194,15 @@ private ICommit[] GetHeadCommits(ICommit? headCommit) =>
return null;
}

/// <summary>
/// Query a <paramref name="repo"/> for the sequence of commits from the beginning to a particular
/// <paramref name="headCommit"/> (inclusive)
/// </summary>
private static IEnumerable<ICommit> GetCommitsReacheableFromHead(IGitRepository repo, ICommit? headCommit)
private IEnumerable<ICommit> GetCommitsReacheableFromHead(ICommit? headCommit)
{
var filter = new CommitFilter
{
IncludeReachableFrom = headCommit,
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Reverse
};

return repo.Commits.QueryBy(filter);
return repository.Commits.QueryBy(filter);
}

public IEnumerable<ICommit> GetMergedCommits(ICommit mergeCommit, int index)
Expand All @@ -202,7 +228,7 @@ private static ICommit GetMergedHead(ICommit mergeCommit)
{
var parents = mergeCommit.Parents.Skip(1).ToList();
if (parents.Count > 1)
throw new NotSupportedException("Mainline development does not support more than one merge source in a single commit yet");
throw new NotSupportedException("GitVersion does not support more than one merge source in a single commit yet");
return parents.Single();
}

Expand Down