Skip to content

Add feature ignore regex for commit messages #1507

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
wants to merge 14 commits into from
5 changes: 5 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ commit-date-format: 'yyyy-MM-dd'
ignore:
sha: []
commits-before: yyyy-MM-ddTHH:mm:ss
regex: 'irgnoreMatch'
```

And the description of the available options are:
Expand Down Expand Up @@ -179,6 +180,10 @@ Date and time in the format `yyyy-MM-ddTHH:mm:ss` (eg `commits-before:
2015-10-23T12:23:15`) to setup an exclusion range. Effectively any commit before
`commits-before` will be ignored.

#### regex
The regex to match commit messages that will be ignored. Usefull to exlude for example
pull request from an automatic nuget updater.

## Branch configuration
Then we have branch specific configuration, which looks something like this:

Expand Down
8 changes: 6 additions & 2 deletions src/GitVersionCore/Configuration/IgnoreConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GitVersion
namespace GitVersion
{
using System;
using System.Collections.Generic;
Expand All @@ -19,10 +19,14 @@ public IgnoreConfig()
[YamlMember(Alias = "sha")]
public IEnumerable<string> SHAs { get; set; }

[YamlMember(Alias = "regex")]
public string Regex { get; set; }

public virtual IEnumerable<IVersionFilter> ToFilters()
{
if (SHAs.Any()) yield return new ShaVersionFilter(SHAs);
if (Before.HasValue) yield return new MinDateVersionFilter(Before.Value);
if (!string.IsNullOrWhiteSpace(Regex)) yield return new MessageFilter(Regex);
}
}
}
}
35 changes: 35 additions & 0 deletions src/GitVersionCore/VersionFilters/MessageFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using GitVersion.VersionCalculation.BaseVersionCalculators;

namespace GitVersion.VersionFilters
{
public class MessageFilter : IVersionFilter
{
private readonly Regex regex;

public MessageFilter(string regex)
{
if (regex == null) throw new ArgumentNullException(nameof(regex));
this.regex = new Regex(regex); ;
}

public bool Exclude(BaseVersion version, out string reason)
{
if (version == null) throw new ArgumentNullException(nameof(version));

reason = null;

if (version.BaseVersionSource != null &&
regex.Match(version.BaseVersionSource.Message).Success)
{
reason = $"Message {version.BaseVersionSource.Message} was ignored due to commit having been excluded by configuration";
return true;
}

return false;
}
}
}