Skip to content

Commit d2db6ee

Browse files
author
roeil
committed
refactor(paths-filter): implement path find logic in gitrepository class
1 parent e242079 commit d2db6ee

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

src/GitVersion.Core/Git/IGitRepository.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using LibGit2Sharp;
2-
31
namespace GitVersion.Git;
42

53
public interface IGitRepository : IDisposable
@@ -16,11 +14,9 @@ public interface IGitRepository : IDisposable
1614
IBranchCollection Branches { get; }
1715
ICommitCollection Commits { get; }
1816
IRemoteCollection Remotes { get; }
19-
IQueryableCommitLog InnerCommits { get; }
20-
IEnumerable<Tag> InnerTags { get; }
21-
Diff InnerDiff { get; }
2217

2318
ICommit? FindMergeBase(ICommit commit, ICommit otherCommit);
19+
IEnumerable<string>? FindPatchPaths(ICommit commit, string? tagPrefix);
2420
int UncommittedChangesCount();
2521
void DiscoverRepository(string? gitDirectory);
2622
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
#nullable enable
22
GitVersion.Configuration.IIgnoreConfiguration.Paths.get -> System.Collections.Generic.IReadOnlyCollection<string!>!
3-
GitVersion.Git.IGitRepository.InnerCommits.get -> LibGit2Sharp.IQueryableCommitLog!
4-
GitVersion.Git.IGitRepository.InnerDiff.get -> LibGit2Sharp.Diff!
5-
GitVersion.Git.IGitRepository.InnerTags.get -> System.Collections.Generic.IEnumerable<LibGit2Sharp.Tag!>!
3+
GitVersion.Git.IGitRepository.FindPatchPaths(GitVersion.Git.ICommit! commit, string? tagPrefix) -> System.Collections.Generic.IEnumerable<string!>?

src/GitVersion.Core/VersionCalculation/PathFilter.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using GitVersion.Extensions;
22
using GitVersion.Git;
3-
using LibGit2Sharp;
43

54
namespace GitVersion.VersionCalculation;
65

76
internal class PathFilter(IGitRepository repository, GitVersionContext context, IEnumerable<string> paths, PathFilter.PathFilterMode mode = PathFilter.PathFilterMode.Inclusive) : IVersionFilter
87
{
9-
private readonly static Dictionary<string, Patch> patchsCache = [];
10-
118
public enum PathFilterMode { Inclusive = 0, Exclusive = 1 }
129

1310
private readonly IEnumerable<string> paths = paths.NotNull();
@@ -24,44 +21,28 @@ public bool Exclude(IBaseVersion baseVersion, out string? reason)
2421
return Exclude(baseVersion.BaseVersionSource, out reason);
2522
}
2623

27-
public bool Exclude(ICommit? localCommit, out string? reason)
24+
public bool Exclude(ICommit? commit, out string? reason)
2825
{
29-
localCommit.NotNull();
30-
var commit = repository.InnerCommits.First(c => c.Sha == localCommit.Sha);
31-
26+
commit.NotNull();
3227
reason = null;
3328

34-
var match = new System.Text.RegularExpressions.Regex($"^({context.Configuration.TagPrefixPattern}).*$",
35-
System.Text.RegularExpressions.RegexOptions.Compiled);
36-
3729
if (commit != null)
3830
{
39-
Patch? patch = null;
40-
if (!patchsCache.ContainsKey(commit.Sha))
41-
{
42-
if (!repository.InnerTags.Any(t => t.Target.Sha == commit.Sha && match.IsMatch(t.FriendlyName)))
43-
{
44-
Tree commitTree = commit.Tree; // Main Tree
45-
Tree? parentCommitTree = commit.Parents.FirstOrDefault()?.Tree; // Secondary Tree
46-
patch = repository.InnerDiff.Compare<Patch>(parentCommitTree, commitTree); // Difference
47-
}
48-
patchsCache[commit.Sha] = patch;
49-
}
31+
var patchPaths = repository.FindPatchPaths(commit, context.Configuration.TagPrefixPattern);
5032

51-
patch = patchsCache[commit.Sha];
52-
if (patch != null)
33+
if (patchPaths != null)
5334
{
5435
switch (mode)
5536
{
5637
case PathFilterMode.Inclusive:
57-
if (!paths.Any(path => patch.Any(p => p.Path.StartsWith(path, StringComparison.OrdinalIgnoreCase))))
38+
if (!paths.Any(path => patchPaths.Any(p => p.StartsWith(path, StringComparison.OrdinalIgnoreCase))))
5839
{
5940
reason = "Source was ignored due to commit path is not present";
6041
return true;
6142
}
6243
break;
6344
case PathFilterMode.Exclusive:
64-
if (paths.Any(path => patch.All(p => p.Path.StartsWith(path, StringComparison.OrdinalIgnoreCase))))
45+
if (paths.Any(path => patchPaths.All(p => p.StartsWith(path, StringComparison.OrdinalIgnoreCase))))
6546
{
6647
reason = "Source was ignored due to commit path excluded";
6748
return true;

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.RegularExpressions;
12
using GitVersion.Extensions;
23
using GitVersion.Helpers;
34
using LibGit2Sharp;
@@ -7,6 +8,7 @@ namespace GitVersion.Git;
78
internal sealed partial class GitRepository
89
{
910
private Lazy<IRepository>? repositoryLazy;
11+
private readonly static Dictionary<string, Patch> patchsCache = [];
1012

1113
private IRepository RepositoryInstance
1214
{
@@ -28,9 +30,6 @@ private IRepository RepositoryInstance
2830
public IBranchCollection Branches => new BranchCollection(RepositoryInstance.Branches);
2931
public ICommitCollection Commits => new CommitCollection(RepositoryInstance.Commits);
3032
public IRemoteCollection Remotes => new RemoteCollection(RepositoryInstance.Network.Remotes);
31-
public IQueryableCommitLog InnerCommits => RepositoryInstance.Commits;
32-
public IEnumerable<LibGit2Sharp.Tag> InnerTags => RepositoryInstance.Tags;
33-
public Diff InnerDiff => RepositoryInstance.Diff;
3433

3534
public void DiscoverRepository(string? gitDirectory)
3635
{
@@ -56,6 +55,26 @@ public void DiscoverRepository(string? gitDirectory)
5655
});
5756
}
5857

58+
public IEnumerable<string>? FindPatchPaths(ICommit commit, string? tagPrefix)
59+
{
60+
Patch? patch = null;
61+
var innerCommit = RepositoryInstance.Commits.First(c => c.Sha == commit.Sha);
62+
var match = new Regex($"^({tagPrefix ?? ""}).*$", RegexOptions.Compiled);
63+
64+
if (!patchsCache.ContainsKey(commit.Sha))
65+
{
66+
if (!RepositoryInstance.Tags.Any(t => t.Target.Sha == commit.Sha && match.IsMatch(t.FriendlyName)))
67+
{
68+
Tree commitTree = innerCommit.Tree; // Main Tree
69+
Tree? parentCommitTree = innerCommit.Parents.FirstOrDefault()?.Tree; // Secondary Tree
70+
patch = RepositoryInstance.Diff.Compare<Patch>(parentCommitTree, commitTree); // Difference
71+
}
72+
patchsCache[commit.Sha] = patch;
73+
}
74+
75+
return patch?.Select(p => p.Path);
76+
}
77+
5978
public int UncommittedChangesCount()
6079
{
6180
var retryAction = new RetryAction<LibGit2Sharp.LockedFileException, int>();

0 commit comments

Comments
 (0)