Skip to content

Enable config based ignoring of commits/messages that incorrectly inc… #470

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

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
6 changes: 4 additions & 2 deletions GitVersionCore.Tests/TestEffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ public TestEffectiveConfiguration(
bool preventIncrementForMergedBranchVersion = false,
string tagNumberPattern = null,
string continuousDeploymentFallbackTag = "ci",
bool trackMergeTarget = false) :
bool trackMergeTarget = false,
string[] commitsToIgnore = null,
string[] mergeMessagesToIgnore = null) :
base(assemblyVersioningScheme, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch,
branchPrefixToTrim, preventIncrementForMergedBranchVersion, tagNumberPattern, continuousDeploymentFallbackTag,
trackMergeTarget)
trackMergeTarget, commitsToIgnore, mergeMessagesToIgnore)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace GitVersionCore.Tests.VersionCalculation.Strategies
{
using System.Collections.Generic;
using GitVersion;
using GitVersion.VersionCalculation.BaseVersionCalculators;
using LibGit2Sharp;
using NUnit.Framework;
Expand Down Expand Up @@ -87,11 +88,50 @@ public void MergeMessagesThatsNotRelatedToGitVersion(string commitMessage)
var parents = GetParents(true);

AssertMergeMessage(commitMessage, null, parents);
}
}

[TestCase(@"Merge branch 'release/14.08-some-historical-commit'")]
public void MergeMessageIgnoresConfiguredMessages(string commitMessage)
{
var parents = GetParents(true);

var config = new Config
{
MergeMessagesToIgnore = new[]
{
"release/14.08"
}
};

AssertMergeMessage(commitMessage, null, parents, config);
}

static void AssertMergeMessage(string message, string expectedVersion, List<Commit> parents)
[TestCase(@"Merge branch 'Release-v0.2.0'", "9541b36186140ad5201c6e27e1abdc0b02a899e4")]
public void MergeMessageIgnoresConfiguredCommits(string commitMessage, string sha)
{
var commit = new MockCommit
var parents = GetParents(true);

var config = new Config
{
CommitsToIgnore = new[]
{
sha
}
};

AssertMergeMessage(commitMessage, null, parents, config, sha);
}

static void AssertMergeMessage(string message, string expectedVersion, List<Commit> parents, Config config = null, string commitSha = null)
{
ObjectId objectId = null;

if (commitSha != null)
{
ObjectId.TryParse(commitSha, out objectId);
}

var commit = new MockCommit(objectId)
{
MessageEx = message,
ParentsEx = parents
Expand All @@ -106,6 +146,7 @@ static void AssertMergeMessage(string message, string expectedVersion, List<Comm
new MockCommit()
}
})
.WithConfig(config)
.Build();
var sut = new MergeMessageBaseVersionStrategy();

Expand Down
6 changes: 6 additions & 0 deletions GitVersionCore/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public Config()
[YamlMember(Alias = "next-version")]
public string NextVersion { get; set; }

[YamlMember(Alias="commits-to-ignore")]
public string[] CommitsToIgnore { get; set; }

[YamlMember(Alias = "merge-messages-to-ignore")]
public string[] MergeMessagesToIgnore { get; set; }

[YamlMember(Alias = "branches")]
public Dictionary<string, BranchConfig> Branches
{
Expand Down
9 changes: 8 additions & 1 deletion GitVersionCore/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public EffectiveConfiguration(
bool preventIncrementForMergedBranchVersion,
string tagNumberPattern,
string continuousDeploymentFallbackTag,
bool trackMergeTarget)
bool trackMergeTarget,
string[] commitsToIgnore,
string[] mergeMessagesToIgnore)
{
AssemblyVersioningScheme = assemblyVersioningScheme;
VersioningMode = versioningMode;
Expand All @@ -26,6 +28,8 @@ public EffectiveConfiguration(
TagNumberPattern = tagNumberPattern;
ContinuousDeploymentFallbackTag = continuousDeploymentFallbackTag;
TrackMergeTarget = trackMergeTarget;
CommitsToIgnore = commitsToIgnore;
MergeMessagesToIgnore = mergeMessagesToIgnore;
}

public VersioningMode VersioningMode { get; private set; }
Expand Down Expand Up @@ -54,5 +58,8 @@ public EffectiveConfiguration(

public string ContinuousDeploymentFallbackTag { get; private set; }
public bool TrackMergeTarget { get; private set; }

public string[] CommitsToIgnore { get; private set; }
public string[] MergeMessagesToIgnore { get; private set; }
}
}
4 changes: 3 additions & 1 deletion GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ void CalculateEffectiveConfiguration()
tag, nextVersion, incrementStrategy, currentBranchConfig.Key,
preventIncrementForMergedBranchVersion,
tagNumberPattern, configuration.ContinuousDeploymentFallbackTag,
currentBranchConfig.Value.TrackMergeTarget);
currentBranchConfig.Value.TrackMergeTarget,
configuration.CommitsToIgnore,
configuration.MergeMessagesToIgnore);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ private static SemanticVersion Inner(Commit mergeCommit, EffectiveConfiguration
return null;
}

if (configuration.CommitsToIgnore != null && configuration.CommitsToIgnore.Contains(mergeCommit.Sha))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we should do something a little more generic, what about bad tags or other things? Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought on tags specifically is that it's pretty easy to delete/fix a bad tag, but fixing a bad commit/message from way back in the repo is nigh on impossible.

As to the larger point, how do you think that would be accomplished? Just have a ThingsToIgnore collection and just do

if(mergeCommit.Sha.In(ThingsToIgnore) || mergeCommit.Tag.In(ThingsToIgnore) || ... etc. etc.)

?

I'm not super familiar w/ how the Commit object works, so there might be a better way than that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. Leave me with this, want to think it through before I put it in the config (cause then we are stuck with it).

I will merge this before the next release.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

{
return null;
}

if (configuration.MergeMessagesToIgnore != null && configuration.MergeMessagesToIgnore.Any(x => mergeCommit.Message.Contains(x)))
{
return null;
}

//TODO: Make the version prefixes customizable
var possibleVersions = Regex.Matches(mergeCommit.Message, @"^.*?(([rR]elease|[hH]otfix|[aA]lpha)-|-v|/|/v|'|Finish )(?<PossibleVersions>\d+\.\d+(\.*\d+)*)")
.Cast<Match>()
Expand Down