Skip to content

Commit 0051e12

Browse files
author
roeil
committed
refactor: paths filter
1 parent 87b1ac0 commit 0051e12

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

src/GitVersion.Core.Tests/VersionCalculation/PathFilterTests.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ public void WhenPathMismatchShouldNotExclude()
3636
reason.ShouldBeNull();
3737
}
3838

39-
[Test]
40-
public void WhenCommitSourceStartsWithCommitTagShouldNotBeExcluded()
41-
{
42-
var commit = GitRepositoryTestingExtensions.CreateMockCommit(["/path"]);
43-
BaseVersion version = new("Git tag: v1.0.0", new SemanticVersion(1), commit);
44-
var sut = new PathFilter(commit.DiffPaths);
45-
46-
var result = sut.Exclude(version, out var reason);
47-
48-
result.ShouldBeFalse();
49-
reason.ShouldBeNull();
50-
}
51-
5239
[Test]
5340
public void ExcludeShouldAcceptVersionWithNullCommit()
5441
{

src/GitVersion.Core/VersionCalculation/PathFilter.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55

66
namespace GitVersion.VersionCalculation;
77

8-
internal class PathFilter(IReadOnlyList<string> paths) : IVersionFilter
8+
internal enum PathFilterMode
9+
{
10+
Inclusive, // All commit paths must match for commit to be excluded
11+
//Exclusive // Any commit path must match for commit to be excluded
12+
}
13+
14+
internal class PathFilter(IReadOnlyList<string> paths, PathFilterMode mode = PathFilterMode.Inclusive) : IVersionFilter
915
{
1016
private readonly IReadOnlyList<Regex> pathsRegexes = [.. paths.Select(path => new Regex(path, RegexOptions.Compiled))];
1117
private readonly ConcurrentDictionary<string, bool> pathMatchCache = [];
@@ -16,25 +22,33 @@ public bool Exclude(IBaseVersion baseVersion, [NotNullWhen(true)] out string? re
1622
return Exclude(baseVersion.BaseVersionSource, out reason);
1723
}
1824

25+
private bool IsMatch(string path)
26+
{
27+
if (!pathMatchCache.TryGetValue(path, out var isMatch))
28+
{
29+
isMatch = this.pathsRegexes.Any(regex => regex.IsMatch(path));
30+
pathMatchCache[path] = isMatch;
31+
}
32+
return isMatch;
33+
}
34+
1935
public bool Exclude(ICommit? commit, [NotNullWhen(true)] out string? reason)
2036
{
2137
reason = null;
2238

2339
if (commit != null)
2440
{
25-
foreach (var path in commit.DiffPaths)
41+
switch (mode)
2642
{
27-
if (!pathMatchCache.TryGetValue(path, out var isMatch))
28-
{
29-
isMatch = this.pathsRegexes.Any(regex => regex.IsMatch(path));
30-
pathMatchCache[path] = isMatch;
31-
}
32-
33-
if (isMatch)
34-
{
35-
reason = "Source was ignored due to commit path matching ignore regex";
36-
return true;
37-
}
43+
case PathFilterMode.Inclusive:
44+
{
45+
if (commit.DiffPaths.All(this.IsMatch))
46+
{
47+
reason = "Source was ignored due to all commit paths matching ignore regex";
48+
return true;
49+
}
50+
break;
51+
}
3852
}
3953
}
4054

0 commit comments

Comments
 (0)