1- using System . Collections . Concurrent ;
21using GitVersion . Extensions ;
32using GitVersion . Helpers ;
43using LibGit2Sharp ;
@@ -8,13 +7,7 @@ namespace GitVersion.Git;
87internal sealed partial class GitRepository
98{
109 private Lazy < IRepository > ? repositoryLazy ;
11-
12- private readonly ConcurrentDictionary < string , Branch > cachedBranches = new ( ) ;
13- private readonly ConcurrentDictionary < string , Commit > cachedCommits = new ( ) ;
14- private readonly ConcurrentDictionary < string , Tag > cachedTags = new ( ) ;
15- private readonly ConcurrentDictionary < string , Remote > cachedRemotes = new ( ) ;
16- private readonly ConcurrentDictionary < string , Reference > cachedReferences = new ( ) ;
17- private readonly ConcurrentDictionary < string , RefSpec > cachedRefSpecs = new ( ) ;
10+ private readonly GitRepositoryCache repositoryCache = new ( ) ;
1811
1912 private IRepository RepositoryInstance
2013 {
@@ -28,13 +21,13 @@ private IRepository RepositoryInstance
2821 public string WorkingDirectory => RepositoryInstance . Info . WorkingDirectory ;
2922 public bool IsHeadDetached => RepositoryInstance . Info . IsHeadDetached ;
3023 public bool IsShallow => RepositoryInstance . Info . IsShallow ;
31- public IBranch Head => GetOrWrap ( RepositoryInstance . Head , RepositoryInstance . Diff ) ;
24+ public IBranch Head => this . repositoryCache . GetOrWrap ( RepositoryInstance . Head , RepositoryInstance . Diff ) ;
3225
33- public ITagCollection Tags => new TagCollection ( RepositoryInstance . Tags , RepositoryInstance . Diff , this ) ;
34- public IReferenceCollection Refs => new ReferenceCollection ( RepositoryInstance . Refs , this ) ;
35- public IBranchCollection Branches => new BranchCollection ( RepositoryInstance . Branches , RepositoryInstance . Diff , this ) ;
36- public ICommitCollection Commits => new CommitCollection ( RepositoryInstance . Commits , RepositoryInstance . Diff , this ) ;
37- public IRemoteCollection Remotes => new RemoteCollection ( RepositoryInstance . Network . Remotes , this ) ;
26+ public ITagCollection Tags => new TagCollection ( RepositoryInstance . Tags , RepositoryInstance . Diff , this . repositoryCache ) ;
27+ public IReferenceCollection Refs => new ReferenceCollection ( RepositoryInstance . Refs , this . repositoryCache ) ;
28+ public IBranchCollection Branches => new BranchCollection ( RepositoryInstance . Branches , RepositoryInstance . Diff , this . repositoryCache ) ;
29+ public ICommitCollection Commits => new CommitCollection ( RepositoryInstance . Commits , RepositoryInstance . Diff , this . repositoryCache ) ;
30+ public IRemoteCollection Remotes => new RemoteCollection ( RepositoryInstance . Network . Remotes , this . repositoryCache ) ;
3831
3932 public void DiscoverRepository ( string ? gitDirectory )
4033 {
@@ -56,7 +49,7 @@ public void DiscoverRepository(string? gitDirectory)
5649 var first = ( Commit ) commit ;
5750 var second = ( Commit ) otherCommit ;
5851 var mergeBase = RepositoryInstance . ObjectDatabase . FindMergeBase ( first , second ) ;
59- return mergeBase == null ? null : GetOrWrap ( mergeBase , RepositoryInstance . Diff ) ;
52+ return mergeBase == null ? null : this . repositoryCache . GetOrWrap ( mergeBase , RepositoryInstance . Diff ) ;
6053 } ) ;
6154 }
6255
@@ -66,29 +59,6 @@ public int UncommittedChangesCount()
6659 return retryAction . Execute ( GetUncommittedChangesCountInternal ) ;
6760 }
6861
69- public Branch GetOrWrap ( LibGit2Sharp . Branch innerBranch , Diff repoDiff )
70- {
71- var cacheKey = innerBranch . Tip is null
72- ? $ "{ innerBranch . RemoteName } /{ innerBranch . CanonicalName } "
73- : $ "{ innerBranch . RemoteName } /{ innerBranch . CanonicalName } @{ innerBranch . Tip . Sha } ";
74- return cachedBranches . GetOrAdd ( cacheKey , _ => new Branch ( innerBranch , repoDiff , this ) ) ;
75- }
76-
77- public Commit GetOrWrap ( LibGit2Sharp . Commit innerCommit , Diff repoDiff )
78- => cachedCommits . GetOrAdd ( innerCommit . Sha , _ => new Commit ( innerCommit , repoDiff , this ) ) ;
79-
80- public Tag GetOrWrap ( LibGit2Sharp . Tag innerTag , Diff repoDiff )
81- => cachedTags . GetOrAdd ( innerTag . CanonicalName , _ => new Tag ( innerTag , repoDiff , this ) ) ;
82-
83- public Remote GetOrWrap ( LibGit2Sharp . Remote innerRemote )
84- => cachedRemotes . GetOrAdd ( innerRemote . Name , _ => new Remote ( innerRemote , this ) ) ;
85-
86- public Reference GetOrWrap ( LibGit2Sharp . Reference innerReference )
87- => cachedReferences . GetOrAdd ( innerReference . CanonicalName , _ => new Reference ( innerReference ) ) ;
88-
89- public RefSpec GetOrWrap ( LibGit2Sharp . RefSpec innerRefSpec )
90- => cachedRefSpecs . GetOrAdd ( innerRefSpec . Specification , _ => new RefSpec ( innerRefSpec ) ) ;
91-
9262 public void Dispose ( )
9363 {
9464 if ( this . repositoryLazy is { IsValueCreated : true } ) RepositoryInstance . Dispose ( ) ;
0 commit comments