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,21 @@ 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  =>  GetOrCreate ( RepositoryInstance . Head ,  RepositoryInstance . Diff ) ; 
24+     public  IBranch  Head  =>  this . repositoryCache . GetOrWrap ( RepositoryInstance . Head ,  RepositoryInstance . Diff ) ; 
25+ 
26+     private  ITagCollection ?  tags ; 
27+     public  ITagCollection  Tags  =>  this . tags  ??=  new  TagCollection ( RepositoryInstance . Tags ,  RepositoryInstance . Diff ,  this . repositoryCache ) ; 
28+ 
29+     public  IBranchCollection  Branches  =>  new  BranchCollection ( RepositoryInstance . Branches ,  RepositoryInstance . Diff ,  this . repositoryCache ) ; 
30+ 
31+     private  ICommitCollection ?  commits ; 
32+     public  ICommitCollection  Commits  =>  this . commits  ??=  new  CommitCollection ( RepositoryInstance . Commits ,  RepositoryInstance . Diff ,  this . repositoryCache ) ; 
3233
33-     public   ITagCollection   Tags   =>   new   TagCollection ( RepositoryInstance . Tags ,   RepositoryInstance . Diff ,   this ) ; 
34-     public  IBranchCollection   Branches  =>  new  BranchCollection ( RepositoryInstance . Branches ,   RepositoryInstance . Diff ,  this ) ; 
35-      public   ICommitCollection   Commits   =>   new   CommitCollection ( RepositoryInstance . Commits ,   RepositoryInstance . Diff ,   this ) ; 
36-     public   IRemoteCollection   Remotes   =>   new   RemoteCollection ( RepositoryInstance . Network . Remotes ,   this ) ; 
37-     public  IReferenceCollection  References  =>  new  ReferenceCollection ( RepositoryInstance . Refs ,  this ) ; 
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 ) ; 
3839
3940    public  void  DiscoverRepository ( string ?  gitDirectory ) 
4041    { 
@@ -56,7 +57,7 @@ public void DiscoverRepository(string? gitDirectory)
5657            var  first  =  ( Commit ) commit ; 
5758            var  second  =  ( Commit ) otherCommit ; 
5859            var  mergeBase  =  RepositoryInstance . ObjectDatabase . FindMergeBase ( first ,  second ) ; 
59-             return  mergeBase  ==  null  ?  null  :  GetOrCreate ( mergeBase ,  RepositoryInstance . Diff ) ; 
60+             return  mergeBase  ==  null  ?  null  :  this . repositoryCache . GetOrWrap ( mergeBase ,  RepositoryInstance . Diff ) ; 
6061        } ) ; 
6162    } 
6263
@@ -66,29 +67,6 @@ public int UncommittedChangesCount()
6667        return  retryAction . Execute ( GetUncommittedChangesCountInternal ) ; 
6768    } 
6869
69-     public  Branch  GetOrCreate ( 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  GetOrCreate ( LibGit2Sharp . Commit  innerCommit ,  Diff  repoDiff ) 
78-         =>  cachedCommits . GetOrAdd ( innerCommit . Sha ,  _ =>  new  Commit ( innerCommit ,  repoDiff ,  this ) ) ; 
79- 
80-     public  Tag  GetOrCreate ( LibGit2Sharp . Tag  innerTag ,  Diff  repoDiff ) 
81-         =>  cachedTags . GetOrAdd ( innerTag . CanonicalName ,  _ =>  new  Tag ( innerTag ,  repoDiff ,  this ) ) ; 
82- 
83-     public  Remote  GetOrCreate ( LibGit2Sharp . Remote  innerRemote ) 
84-         =>  cachedRemotes . GetOrAdd ( innerRemote . Name ,  _ =>  new  Remote ( innerRemote ,  this ) ) ; 
85- 
86-     public  Reference  GetOrCreate ( LibGit2Sharp . Reference  innerReference ) 
87-         =>  cachedReferences . GetOrAdd ( innerReference . CanonicalName ,  _ =>  new  Reference ( innerReference ) ) ; 
88- 
89-     public  RefSpec  GetOrCreate ( LibGit2Sharp . RefSpec  innerRefSpec ) 
90-         =>  cachedRefSpecs . GetOrAdd ( innerRefSpec . Specification ,  _ =>  new  RefSpec ( innerRefSpec ) ) ; 
91- 
9270    public  void  Dispose ( ) 
9371    { 
9472        if  ( this . repositoryLazy  is  {  IsValueCreated :  true  } )  RepositoryInstance . Dispose ( ) ; 
0 commit comments