Skip to content
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

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

Closed
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);
}
}
69 changes: 47 additions & 22 deletions src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace GitVersion.VersionCalculation;

internal class IncrementStrategyFinder : IIncrementStrategyFinder
internal sealed class IncrementStrategyFinder : IIncrementStrategyFinder
{
public const string DefaultMajorPattern = @"\+semver:\s?(breaking|major)";
public const string DefaultMinorPattern = @"\+semver:\s?(feature|minor)";
Expand All @@ -28,7 +28,6 @@ internal class IncrementStrategyFinder : IIncrementStrategyFinder

public IncrementStrategyFinder(IGitRepository repository, ITaggedSemanticVersionRepository taggedSemanticVersionRepository)
{
this.repository = repository.NotNull();
this.repository = repository.NotNull();
this.taggedSemanticVersionRepository = taggedSemanticVersionRepository;
}
Expand Down Expand Up @@ -81,26 +80,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 @@ -120,6 +114,41 @@ private static Regex TryGetRegexOrDefault(string? messageRegex, Regex defaultReg
? defaultRegex
: CompiledRegexCache.GetOrAdd(messageRegex, pattern => new(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase));

public IReadOnlyCollection<ICommit> GetCommitHistory(
string? tagPrefix, SemanticVersionFormat semanticVersionFormat, ICommit? baseVersionSource, ICommit currentCommit, string? label)
{
var targetShas = new Lazy<IReadOnlySet<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 item in intermediateCommits.Reverse())
{
if (!commitLog.ContainsKey(item.Sha)) continue;

if (targetShas.Value.Contains(item.Sha))
{
void RemoveCommitFromHistory(ICommit commit)
{
if (!commitLog.ContainsKey(commit.Sha)) return;

commitLog.Remove(commit.Sha);
foreach (var item in commit.Parents)
{
RemoveCommitFromHistory(item);
}
}
RemoveCommitFromHistory(item);
}
}

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 @@ -155,7 +184,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 @@ -170,19 +199,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 @@ -208,7 +233,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
Loading