Skip to content

Commit e78fba2

Browse files
committed
cache git collections with lazy initialization
Replaces direct instantiations of Tags, Branches, 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 ef67797 commit e78fba2

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ 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);
27-
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);
26+
private ITagCollection? tags;
27+
public ITagCollection Tags => this.tags ??= new TagCollection(RepositoryInstance.Tags, RepositoryInstance.Diff, this.repositoryCache);
28+
29+
private IBranchCollection? branches;
30+
public IBranchCollection Branches => this.branches ??= new BranchCollection(RepositoryInstance.Branches, RepositoryInstance.Diff, this.repositoryCache);
31+
32+
private ICommitCollection? commits;
33+
public ICommitCollection Commits => this.commits ??= new CommitCollection(RepositoryInstance.Commits, RepositoryInstance.Diff, this.repositoryCache);
34+
35+
private IRemoteCollection? remotes;
36+
public IRemoteCollection Remotes => this.remotes ??= new RemoteCollection(RepositoryInstance.Network.Remotes, this.repositoryCache);
37+
38+
private IReferenceCollection? references;
39+
public IReferenceCollection References => this.references ??= new ReferenceCollection(RepositoryInstance.Refs, this.repositoryCache);
3140

3241
public void DiscoverRepository(string? gitDirectory)
3342
{

0 commit comments

Comments
 (0)