Skip to content

Commit e84e12b

Browse files
author
roeil
committed
refactor: compile and cache regexes
1 parent a397bf5 commit e84e12b

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/GitVersion.Core/Configuration/IIgnoreConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ public interface IIgnoreConfiguration
88

99
IReadOnlyCollection<string> Paths { get; }
1010

11-
public bool IsEmpty => Before == null && Shas.Count == 0 && Paths.Count == 0;
11+
bool IsEmpty => Before == null && Shas.Count == 0 && Paths.Count == 0;
1212
}

src/GitVersion.Core/VersionCalculation/PathFilter.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ namespace GitVersion.VersionCalculation;
66

77
internal class PathFilter(IGitRepository repository, GitVersionContext context, IEnumerable<string> paths) : IVersionFilter
88
{
9-
public enum PathFilterMode { Inclusive = 0, Exclusive = 1 }
10-
11-
private readonly IEnumerable<string> paths = paths.NotNull();
129
private readonly GitVersionContext context = context;
10+
private readonly List<Regex> pathsRegexes = paths.Select(path => new Regex(path, RegexOptions.IgnoreCase | RegexOptions.Compiled)).ToList();
11+
private readonly Dictionary<string, bool> pathMatchCache = [];
1312

1413
public bool Exclude(IBaseVersion baseVersion, out string? reason)
1514
{
@@ -31,10 +30,19 @@ public bool Exclude(ICommit? commit, out string? reason)
3130

3231
if (patchPaths != null)
3332
{
34-
if (this.paths.Any(path => patchPaths.All(p => Regex.IsMatch(p, path, RegexOptions.IgnoreCase))))
33+
foreach (var path in patchPaths)
3534
{
36-
reason = "Source was ignored due to commit path matching ignore regex";
37-
return true;
35+
if (!pathMatchCache.TryGetValue(path, out var isMatch))
36+
{
37+
isMatch = this.pathsRegexes.Any(regex => regex.IsMatch(path));
38+
pathMatchCache[path] = isMatch;
39+
}
40+
41+
if (isMatch)
42+
{
43+
reason = "Source was ignored due to commit path matching ignore regex";
44+
return true;
45+
}
3846
}
3947
}
4048
}

0 commit comments

Comments
 (0)