Skip to content

Commit

Permalink
https://github.com/GitTools/GitVersion/issues/2917
Browse files Browse the repository at this point in the history
  • Loading branch information
HHobeck committed Mar 18, 2023
1 parent e2e31e3 commit 13ccc0e
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 41 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "7.0.103"
"version": "7.0.200"
}
}
75 changes: 75 additions & 0 deletions src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,87 @@
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.VersionCalculation;
using LibGit2Sharp;

namespace GitVersion.Core.Tests.IntegrationTests;

public class XenoLibPackages : TestBase
{
private GitVersionConfiguration GetConfiguration()
{
var configuration = GitFlowConfigurationBuilder.New
.WithBranch("feature", _ => _
.WithVersioningMode(VersioningMode.ContinuousDeployment)
.WithIncrement(IncrementStrategy.Minor)
).WithBranch("pull-request", _ => _
.WithIncrement(IncrementStrategy.None)
.WithRegularExpression(@"^(pull|pull\-requests|pr)[/-]")
.WithTrackMergeTarget(true)
).Build();
return configuration;
}

[Test, Ignore("")]
public void IncrementFeatureByMinor()
{
var configuration = GetConfiguration();

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

fixture.MakeATaggedCommit("0.1.0");
fixture.BranchTo("feature/foo", "foo");
fixture.MakeACommit();
fixture.AssertFullSemver("0.2.0-foo.1", configuration);
fixture.MakeACommit();
fixture.AssertFullSemver("0.2.0-foo.2", configuration);
fixture.Checkout("main");
fixture.MergeNoFF("feature/foo");
fixture.AssertFullSemver("0.2.0", configuration); // 0.1.1+3
}

[Test, Ignore("")]
public void CanCalculatePullRequestChanges()
{
var configuration = GetConfiguration();

using var fixture = new EmptyRepositoryFixture();

fixture.MakeATaggedCommit("0.1.0");
fixture.CreateBranch("feature/foo");
fixture.MakeACommit();
fixture.AssertFullSemver("0.2.0-foo.1", configuration);
fixture.Repository.CreatePullRequestRef("feature/foo", MainBranch, normalise: true);

fixture.AssertFullSemver("0.2.0-PullRequest0002.2"); // 0.1.1-PullRequest2.2
}
}

[TestFixture]
public class OtherBranchScenarios : TestBase
{
[Test]
public void VerifyManuallyIncrementingVersion()
{
var configuration = GitFlowConfigurationBuilder.New
.WithBranch("develop", _ => _
.WithCommitMessageIncrementing(CommitMessageIncrementMode.Enabled)
.WithVersioningMode(VersioningMode.ContinuousDelivery)
).Build();

using var fixture = new EmptyRepositoryFixture();

fixture.MakeACommit("1");

fixture.BranchTo("develop");
fixture.MakeACommit("+semver: fix");

fixture.AssertFullSemver("0.1.1-alpha.1+2", configuration);

fixture.MakeACommit("+semver: fix");

fixture.AssertFullSemver("0.1.2-alpha.1+3", configuration);
}

[Test]
public void CanTakeVersionFromReleaseBranch()
{
Expand Down
7 changes: 4 additions & 3 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,8 @@ GitVersion.SemanticVersion.BuildMetaData -> GitVersion.SemanticVersionBuildMetaD
GitVersion.SemanticVersion.CompareTo(GitVersion.SemanticVersion? value) -> int
GitVersion.SemanticVersion.CompareTo(GitVersion.SemanticVersion? value, bool includePrerelease) -> int
GitVersion.SemanticVersion.Equals(GitVersion.SemanticVersion? obj) -> bool
GitVersion.SemanticVersion.IncrementVersion(GitVersion.VersionField incrementStrategy, string? label) -> GitVersion.SemanticVersion!
GitVersion.SemanticVersion.IncrementVersion(bool isContinuousDeployment, string? label, params GitVersion.VersionField[]! incrementStrategy) -> GitVersion.SemanticVersion!
GitVersion.SemanticVersion.IncrementVersion(string? label, params GitVersion.VersionField[]! incrementStrategy) -> GitVersion.SemanticVersion!
GitVersion.SemanticVersion.IsEmpty() -> bool
GitVersion.SemanticVersion.IsLabeledWith(string! value) -> bool
GitVersion.SemanticVersion.IsMatchForBranchSpecificLabel(string? value) -> bool
Expand Down Expand Up @@ -890,12 +891,12 @@ GitVersion.VersionCalculation.EffectiveBranchConfigurationFinder.EffectiveBranch
GitVersion.VersionCalculation.IEffectiveBranchConfigurationFinder
GitVersion.VersionCalculation.IEffectiveBranchConfigurationFinder.GetConfigurations(GitVersion.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.Configuration.EffectiveBranchConfiguration!>!
GitVersion.VersionCalculation.IIncrementStrategyFinder
GitVersion.VersionCalculation.IIncrementStrategyFinder.DetermineIncrementedField(GitVersion.ICommit? currentCommit, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.Configuration.EffectiveConfiguration! configuration) -> GitVersion.VersionField
GitVersion.VersionCalculation.IIncrementStrategyFinder.DetermineIncrementedFields(GitVersion.ICommit? currentCommit, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.Configuration.EffectiveConfiguration! configuration) -> GitVersion.VersionField[]!
GitVersion.VersionCalculation.IIncrementStrategyFinder.GetIncrementForCommits(string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, System.Collections.Generic.IEnumerable<GitVersion.ICommit!>! commits) -> GitVersion.VersionField?
GitVersion.VersionCalculation.IMainlineVersionCalculator
GitVersion.VersionCalculation.IMainlineVersionCalculator.CreateVersionBuildMetaData(GitVersion.ICommit? baseVersionSource) -> GitVersion.SemanticVersionBuildMetaData!
GitVersion.VersionCalculation.IMainlineVersionCalculator.FindMainlineModeVersion(GitVersion.VersionCalculation.NextVersion! nextVersion) -> GitVersion.SemanticVersion!
GitVersion.VersionCalculation.IncrementStrategyFinder.DetermineIncrementedField(GitVersion.ICommit? currentCommit, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.Configuration.EffectiveConfiguration! configuration) -> GitVersion.VersionField
GitVersion.VersionCalculation.IncrementStrategyFinder.DetermineIncrementedFields(GitVersion.ICommit? currentCommit, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.Configuration.EffectiveConfiguration! configuration) -> GitVersion.VersionField[]!
GitVersion.VersionCalculation.IncrementStrategyFinder.GetIncrementForCommits(string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, System.Collections.Generic.IEnumerable<GitVersion.ICommit!>! commits) -> GitVersion.VersionField?
GitVersion.VersionCalculation.INextVersionCalculator
GitVersion.VersionCalculation.INextVersionCalculator.FindVersion() -> GitVersion.VersionCalculation.NextVersion!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ namespace GitVersion.VersionCalculation;

public interface IIncrementStrategyFinder
{
VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration);
VersionField[] DetermineIncrementedFields(
ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration
);

VersionField? GetIncrementForCommits(
string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage,
Expand Down
58 changes: 46 additions & 12 deletions src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,62 @@ public class IncrementStrategyFinder : IIncrementStrategyFinder

public IncrementStrategyFinder(IGitRepository repository) => this.repository = repository.NotNull();

public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration)
public VersionField[] DetermineIncrementedFields(
ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration)
{
baseVersion.NotNull();
configuration.NotNull();

var commitMessageIncrement = FindCommitMessageIncrement(configuration, baseVersion.BaseVersionSource, currentCommit);
return DetermineIncrementedFieldsInternal(currentCommit, baseVersion, configuration).ToArray();
}

private IEnumerable<VersionField> DetermineIncrementedFieldsInternal(
ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration)
{
var commitMessageIncrements = FindCommitMessageIncrements(
configuration, baseVersion.BaseVersionSource, currentCommit
).ToArray();

var defaultIncrement = configuration.Increment.ToVersionField();

// use the default branch configuration increment strategy if there are no commit message overrides
if (commitMessageIncrement == null)
if (commitMessageIncrements.Length == 0)
{
return baseVersion.ShouldIncrement ? defaultIncrement : VersionField.None;
yield return baseVersion.ShouldIncrement ? defaultIncrement : VersionField.None;
yield break;
}

// don't increment for less than the branch configuration increment, if the absence of commit messages would have
// still resulted in an increment of configuration.Increment
if (baseVersion.ShouldIncrement && commitMessageIncrement < defaultIncrement)
if (baseVersion.ShouldIncrement && defaultIncrement != VersionField.None)
{
return defaultIncrement;
yield return defaultIncrement;
}

return commitMessageIncrement.Value;
// var maximalIncrement = baseVersion.ShouldIncrement ? defaultIncrement : VersionField.Major;
foreach (var commitMessageIncrement in commitMessageIncrements)
{
// don't increment for less than the branch configuration increment, if the absence of commit messages would have
// still resulted in an increment of configuration.Increment
//if (commitMessageIncrement <= maximalIncrement)
{
yield return commitMessageIncrement;
}
}
}

private IEnumerable<VersionField> GetIncrementForCommitsInternal(
string? majorVersionBumpMessage, string? minorVersionBumpMessage,
string? patchVersionBumpMessage, string? noBumpMessage, IEnumerable<ICommit> commits)
{
commits.NotNull();

var majorRegex = TryGetRegexOrDefault(majorVersionBumpMessage, DefaultMajorPatternRegex);
var minorRegex = TryGetRegexOrDefault(minorVersionBumpMessage, DefaultMinorPatternRegex);
var patchRegex = TryGetRegexOrDefault(patchVersionBumpMessage, DefaultPatchPatternRegex);
var none = TryGetRegexOrDefault(noBumpMessage, DefaultNoBumpPatternRegex);

return commits
.Select(c => GetIncrementFromCommit(c, majorRegex, minorRegex, patchRegex, none))
.Where(v => v != null).Select(el => el!.Value);
}

public VersionField? GetIncrementForCommits(string? majorVersionBumpMessage, string? minorVersionBumpMessage,
Expand All @@ -71,11 +104,12 @@ public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersio
: null;
}

private VersionField? FindCommitMessageIncrement(EffectiveConfiguration configuration, ICommit? baseCommit, ICommit? currentCommit)
private IEnumerable<VersionField> FindCommitMessageIncrements(
EffectiveConfiguration configuration, ICommit? baseCommit, ICommit? currentCommit)
{
if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.Disabled)
{
return null;
return Enumerable.Empty<VersionField>();
}

var commits = GetIntermediateCommits(baseCommit, currentCommit);
Expand All @@ -92,7 +126,7 @@ public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersio
commits = commits.Where(c => c.Parents.Count() > 1);
}

return GetIncrementForCommits(
return GetIncrementForCommitsInternal(
majorVersionBumpMessage: configuration.MajorVersionBumpMessage,
minorVersionBumpMessage: configuration.MinorVersionBumpMessage,
patchVersionBumpMessage: configuration.PatchVersionBumpMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public SemanticVersion FindMainlineModeVersion(NextVersion nextVersion)
var branchIncrement = FindMessageIncrement(null, Context.CurrentCommit, mergeBase, mainlineCommitLog);
this.log.Info($"Performing {branchIncrement} increment for current branch ");

mainlineVersion = mainlineVersion.IncrementVersion(branchIncrement, null);
mainlineVersion = mainlineVersion.IncrementVersion(null, branchIncrement);
}

return new SemanticVersion(mainlineVersion)
Expand Down Expand Up @@ -133,7 +133,7 @@ private SemanticVersion AggregateMergeCommitIncrement(ICommit commit, List<IComm
directCommits.Clear();

// Finally increment for the branch
mainlineVersion = mainlineVersion.IncrementVersion(findMessageIncrement, null);
mainlineVersion = mainlineVersion.IncrementVersion(null, findMessageIncrement);
this.log.Info($"Merge commit {mergeCommit} incremented base versions {findMessageIncrement}, now {mainlineVersion}");
return mainlineVersion;
}
Expand Down Expand Up @@ -288,7 +288,7 @@ private SemanticVersion IncrementForEachCommit(IEnumerable<ICommit> directCommit
noBumpMessage: Context.Configuration.NoBumpMessage,
commits: new[] { directCommit }
) ?? FindDefaultIncrementForBranch(Context, mainline);
mainlineVersion = mainlineVersion.IncrementVersion(directCommitIncrement, null);
mainlineVersion = mainlineVersion.IncrementVersion(null, directCommitIncrement);
this.log.Info($"Direct commit on main {directCommit} incremented base versions {directCommitIncrement}, now {mainlineVersion}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,14 @@ private bool TryGetNextVersion([NotNullWhen(true)] out NextVersion? result,

private SemanticVersion GetIncrementedVersion(EffectiveBranchConfiguration configuration, BaseVersion baseVersion, string? label)
{
var incrementStrategy = incrementStrategyFinder.DetermineIncrementedField(
var incrementStrategy = incrementStrategyFinder.DetermineIncrementedFields(
currentCommit: Context.CurrentCommit,
baseVersion: baseVersion,
configuration: configuration.Value
);
return baseVersion.SemanticVersion.IncrementVersion(incrementStrategy, label);

bool isContinuousDeployment = configuration.Value.VersioningMode == VersioningMode.ContinuousDeployment;
return baseVersion.SemanticVersion.IncrementVersion(isContinuousDeployment, label, incrementStrategy);
}

private bool IncludeVersion(BaseVersion baseVersion, IIgnoreConfiguration ignoreConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,30 +303,33 @@ public string ToString(string? format, IFormatProvider? formatProvider)
}
}

public SemanticVersion IncrementVersion(VersionField incrementStrategy, string? label)
public SemanticVersion IncrementVersion(string? label, params VersionField[] incrementStrategy)
{
var incremented = new SemanticVersion(this);

if (!incremented.PreReleaseTag.HasTag())
foreach (var item in incrementStrategy)
{
switch (incrementStrategy)
if (!incremented.PreReleaseTag.HasTag())
{
case VersionField.None:
break;
case VersionField.Major:
incremented.Major++;
incremented.Minor = 0;
incremented.Patch = 0;
break;
case VersionField.Minor:
incremented.Minor++;
incremented.Patch = 0;
break;
case VersionField.Patch:
incremented.Patch++;
break;
default:
throw new ArgumentOutOfRangeException(nameof(incrementStrategy));
switch (item)
{
case VersionField.None:
break;
case VersionField.Major:
incremented.Major++;
incremented.Minor = 0;
incremented.Patch = 0;
break;
case VersionField.Minor:
incremented.Minor++;
incremented.Patch = 0;
break;
case VersionField.Patch:
incremented.Patch++;
break;
default:
throw new ArgumentOutOfRangeException(nameof(incrementStrategy));
}
}
}

Expand All @@ -342,4 +345,47 @@ public SemanticVersion IncrementVersion(VersionField incrementStrategy, string?

return incremented;
}

public SemanticVersion IncrementVersion(bool isContinuousDeployment, string? label, params VersionField[] incrementStrategy)
{
var incremented = new SemanticVersion(this);

foreach (var item in incrementStrategy)
{
if (!incremented.PreReleaseTag.HasTag())
{
switch (item)
{
case VersionField.None:
break;
case VersionField.Major:
incremented.Major++;
incremented.Minor = 0;
incremented.Patch = 0;
break;
case VersionField.Minor:
incremented.Minor++;
incremented.Patch = 0;
break;
case VersionField.Patch:
incremented.Patch++;
break;
default:
throw new ArgumentOutOfRangeException(nameof(incrementStrategy));
}
}
}

if (incremented.PreReleaseTag.HasTag() && incremented.PreReleaseTag.Number != null)
{
incremented.PreReleaseTag.Number++;
}

if (!PreReleaseTag.HasTag() && !label.IsNullOrEmpty())
{
incremented.PreReleaseTag = new(label, isContinuousDeployment ? 1 : 1);
}

return incremented;
}
}

0 comments on commit 13ccc0e

Please sign in to comment.