Skip to content

Commit ac0e28b

Browse files
committed
cache git collections with lazy initialization
Replaces direct instantiations of Tags, Commits, Remotes, and References with lazy initialization. This avoids redundant object creation and improves performance by deferring instantiation until the collection is accessed.
1 parent f0fb376 commit ac0e28b

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -146,27 +146,25 @@ private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pu
146146
using var fixture = new EmptyRepositoryFixture();
147147
var remoteRepositoryPath = FileSystemHelper.Path.GetRepositoryTempPath();
148148
RepositoryFixtureBase.Init(remoteRepositoryPath);
149-
using (var remoteRepository = new Repository(remoteRepositoryPath))
150-
{
151-
remoteRepository.Config.Set("user.name", "Test");
152-
remoteRepository.Config.Set("user.email", "[email protected]");
153-
fixture.Repository.Network.Remotes.Add("origin", remoteRepositoryPath);
154-
Console.WriteLine("Created git repository at {0}", remoteRepositoryPath);
155-
remoteRepository.MakeATaggedCommit("1.0.3");
156-
157-
var branch = remoteRepository.CreateBranch("FeatureBranch");
158-
Commands.Checkout(remoteRepository, branch);
159-
remoteRepository.MakeCommits(2);
160-
Commands.Checkout(remoteRepository, remoteRepository.Head.Tip.Sha);
161-
//Emulate merge commit
162-
var mergeCommitSha = remoteRepository.MakeACommit().Sha;
163-
Commands.Checkout(remoteRepository, TestBase.MainBranch); // HEAD cannot be pointing at the merge commit
164-
remoteRepository.Refs.Add(pullRequestRef, new ObjectId(mergeCommitSha));
165-
166-
// Checkout PR commit
167-
Commands.Fetch(fixture.Repository, "origin", [], new FetchOptions(), null);
168-
Commands.Checkout(fixture.Repository, mergeCommitSha);
169-
}
149+
using var remoteRepository = new Repository(remoteRepositoryPath);
150+
remoteRepository.Config.Set("user.name", "Test");
151+
remoteRepository.Config.Set("user.email", "[email protected]");
152+
fixture.Repository.Network.Remotes.Add("origin", remoteRepositoryPath);
153+
Console.WriteLine("Created git repository at {0}", remoteRepositoryPath);
154+
remoteRepository.MakeATaggedCommit("1.0.3");
155+
156+
var branch = remoteRepository.CreateBranch("FeatureBranch");
157+
Commands.Checkout(remoteRepository, branch);
158+
remoteRepository.MakeCommits(2);
159+
Commands.Checkout(remoteRepository, remoteRepository.Head.Tip.Sha);
160+
//Emulate merge commit
161+
var mergeCommitSha = remoteRepository.MakeACommit().Sha;
162+
Commands.Checkout(remoteRepository, TestBase.MainBranch); // HEAD cannot be pointing at the merge commit
163+
remoteRepository.Refs.Add(pullRequestRef, new ObjectId(mergeCommitSha));
164+
165+
// Checkout PR commit
166+
Commands.Fetch(fixture.Repository, "origin", [], new FetchOptions(), null);
167+
Commands.Checkout(fixture.Repository, mergeCommitSha);
170168

171169
var programFixture = new ProgramFixture(fixture.RepositoryPath);
172170
programFixture.WithOverrides(services =>
@@ -199,12 +197,12 @@ private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pu
199197
public void VerifyPullRequestInput(string pullRequestRef, string friendly, bool isBranch, bool isPullRequest, bool isRemote)
200198
{
201199
var refName = new ReferenceName(pullRequestRef);
202-
Assert.Multiple(() =>
200+
using (Assert.EnterMultipleScope())
203201
{
204202
Assert.That(refName.Friendly, Is.EqualTo(friendly));
205203
Assert.That(refName.IsLocalBranch, Is.EqualTo(isBranch));
206204
Assert.That(refName.IsPullRequest, Is.EqualTo(isPullRequest));
207205
Assert.That(refName.IsRemoteBranch, Is.EqualTo(isRemote));
208-
});
206+
}
209207
}
210208
}

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ private IRepository RepositoryInstance
2323
public bool IsShallow => RepositoryInstance.Info.IsShallow;
2424
public IBranch Head => this.repositoryCache.GetOrWrap(RepositoryInstance.Head, RepositoryInstance.Diff);
2525

26-
public ITagCollection Tags => new TagCollection(RepositoryInstance.Tags, RepositoryInstance.Diff, this.repositoryCache);
26+
private ITagCollection? tags;
27+
public ITagCollection Tags => this.tags ??= new TagCollection(RepositoryInstance.Tags, RepositoryInstance.Diff, this.repositoryCache);
28+
2729
public IBranchCollection Branches => new BranchCollection(RepositoryInstance.Branches, RepositoryInstance.Diff, this.repositoryCache);
28-
public ICommitCollection Commits => new CommitCollection(RepositoryInstance.Commits, RepositoryInstance.Diff, this.repositoryCache);
29-
public IRemoteCollection Remotes => new RemoteCollection(RepositoryInstance.Network.Remotes, this.repositoryCache);
30-
public IReferenceCollection References => new ReferenceCollection(RepositoryInstance.Refs, this.repositoryCache);
30+
31+
private ICommitCollection? commits;
32+
public ICommitCollection Commits => this.commits ??= new CommitCollection(RepositoryInstance.Commits, RepositoryInstance.Diff, this.repositoryCache);
33+
34+
private IRemoteCollection? remotes;
35+
public IRemoteCollection Remotes => this.remotes ??= new RemoteCollection(RepositoryInstance.Network.Remotes, this.repositoryCache);
36+
37+
private IReferenceCollection? references;
38+
public IReferenceCollection References => this.references ??= new ReferenceCollection(RepositoryInstance.Refs, this.repositoryCache);
3139

3240
public void DiscoverRepository(string? gitDirectory)
3341
{

0 commit comments

Comments
 (0)