Skip to content

Commit 66ac29e

Browse files
author
roeil
committed
refactor: use PathFilterMode to indicate explicitly the mode of path-based filtering
1 parent f924b85 commit 66ac29e

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

new-cli/GitVersion.Core.Libgit2Sharp/GitVersion.Core.Libgit2Sharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
</Compile>
5757
<Compile Include="..\..\src\GitVersion.LibGit2Sharp\Git\TagCollection.cs">
5858
<Link>Git\TagCollection.cs</Link>
59+
</Compile>
60+
<Compile Include="..\..\src\GitVersion.LibGit2Sharp\Git\TreeChanges.cs">
61+
<Link>Git\TreeChanges.cs</Link>
5962
</Compile>
6063
</ItemGroup>
6164

src/GitVersion.Core/Git/ICommit.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public interface ICommit : IEquatable<ICommit?>, IComparable<ICommit>, IGitObjec
77
DateTimeOffset When { get; }
88

99
string Message { get; }
10+
1011
IReadOnlyList<string> DiffPaths { get; }
1112
}

src/GitVersion.Core/VersionCalculation/PathFilter.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
namespace GitVersion.VersionCalculation;
77

8-
internal class PathFilter(IReadOnlyList<string> paths) : IVersionFilter
8+
internal enum PathFilterMode
99
{
10-
private readonly List<Regex> pathsRegexes = [.. paths.Select(path => new Regex(path, RegexOptions.Compiled))];
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
15+
{
16+
private readonly IReadOnlyList<Regex> pathsRegexes = [.. paths.Select(path => new Regex(path, RegexOptions.Compiled))];
1117
private readonly ConcurrentDictionary<string, bool> pathMatchCache = [];
1218

1319
public bool Exclude(IBaseVersion baseVersion, [NotNullWhen(true)] out string? reason)
@@ -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

src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal sealed class BranchCollection : IBranchCollection
1212
internal BranchCollection(LibGit2Sharp.BranchCollection collection, LibGit2Sharp.Diff diff)
1313
{
1414
this.innerCollection = collection.NotNull();
15-
this.branches = new Lazy<IReadOnlyCollection<IBranch>>(() => [.. this.innerCollection.Select(branch => new Branch(branch, diff))]);
15+
this.branches = new Lazy<IReadOnlyCollection<IBranch>>(() => [.. this.innerCollection.Select(branch => new Branch(branch, diff))]);
1616
this.diff = diff.NotNull();
1717
}
1818

src/GitVersion.LibGit2Sharp/Git/Commit.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ internal Commit(LibGit2Sharp.Commit innerCommit, LibGit2Sharp.Diff repoDiff) : b
2727
public IReadOnlyList<ICommit> Parents => this.parentsLazy.Value;
2828
public DateTimeOffset When { get; }
2929
public string Message => this.innerCommit.Message;
30-
// TODO implement tag prefix filtering before returning the paths.
3130
public IReadOnlyList<string> DiffPaths
3231
{
3332
get

0 commit comments

Comments
 (0)