Skip to content

Commit ec630ba

Browse files
author
roeil
committed
refactor: patch finding
1 parent 001b673 commit ec630ba

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/GitVersion.Core/Git/IGitRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface IGitRepository : IDisposable
1616
IRemoteCollection Remotes { get; }
1717

1818
ICommit? FindMergeBase(ICommit commit, ICommit otherCommit);
19-
IEnumerable<string>? FindPatchPaths(ICommit commit, string? tagPrefix);
19+
IReadOnlyList<string>? FindPatchPaths(ICommit commit, string? tagPrefix);
2020
int UncommittedChangesCount();
2121
void DiscoverRepository(string? gitDirectory);
2222
}

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Text.RegularExpressions;
1+
using System.Collections.Concurrent;
22
using GitVersion.Extensions;
33
using GitVersion.Helpers;
44
using LibGit2Sharp;
@@ -8,7 +8,7 @@ namespace GitVersion.Git;
88
internal sealed partial class GitRepository
99
{
1010
private Lazy<IRepository>? repositoryLazy;
11-
private readonly Dictionary<string, Patch> patchCache = [];
11+
private readonly ConcurrentDictionary<string, IReadOnlyList<string>?> patchPathsCache = new();
1212

1313
private IRepository RepositoryInstance
1414
{
@@ -55,28 +55,37 @@ public void DiscoverRepository(string? gitDirectory)
5555
});
5656
}
5757

58-
public IEnumerable<string>? FindPatchPaths(ICommit commit, string? tagPrefix)
58+
public IReadOnlyList<string>? FindPatchPaths(ICommit commit, string? tagPrefix)
5959
{
60-
Patch? patch = null;
61-
var innerCommit = this.RepositoryInstance.Commits.First(c => c.Sha == commit.Sha);
62-
var match = new Regex($"^({tagPrefix ?? ""}).*$", RegexOptions.Compiled);
60+
ArgumentNullException.ThrowIfNull(commit);
6361

64-
if (!this.patchCache.ContainsKey(commit.Sha))
62+
return patchPathsCache.GetOrAdd(commit.Sha, commitSha =>
6563
{
66-
if (!this.RepositoryInstance.Tags.Any(t => t.Target.Sha == commit.Sha && match.IsMatch(t.FriendlyName)))
64+
if (PatchPathsNeedsToBeDetermined(commitSha, tagPrefix))
6765
{
66+
var innerCommit = this.RepositoryInstance.Commits.First(c => c.Sha == commitSha);
6867
Tree commitTree = innerCommit.Tree; // Main Tree
6968
Tree? parentCommitTree = innerCommit.Parents.FirstOrDefault()?.Tree; // Secondary Tree
70-
patch = this.RepositoryInstance.Diff.Compare<Patch>(parentCommitTree, commitTree); // Difference
71-
this.patchCache[commit.Sha] = patch;
69+
Patch patch = this.RepositoryInstance.Diff.Compare<Patch>(parentCommitTree, commitTree); // Difference
70+
return patch.Select(element => element.Path).ToList();
7271
}
73-
}
74-
else
72+
return null;
73+
});
74+
}
75+
76+
private bool PatchPathsNeedsToBeDetermined(string commitSha, string? tagPrefix)
77+
{
78+
if (!string.IsNullOrEmpty(tagPrefix))
7579
{
76-
patch = this.patchCache[commit.Sha];
80+
foreach (var tag in this.RepositoryInstance.Tags.Where(element => element.Target.Sha == commitSha))
81+
{
82+
if (tag.FriendlyName.StartsWith(tagPrefix, StringComparison.InvariantCulture))
83+
{
84+
return false;
85+
}
86+
}
7787
}
78-
79-
return patch?.Select(p => p.Path);
88+
return true;
8089
}
8190

8291
public int UncommittedChangesCount()

0 commit comments

Comments
 (0)