Skip to content

Commit b462ee7

Browse files
committed
caches git objects
Uses lazy initialization to cache references and remotes.
1 parent 314d06c commit b462ee7

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,17 @@ public int UncommittedChangesCount()
6565

6666
public Branch GetOrWrap(LibGit2Sharp.Branch innerBranch, Diff repoDiff)
6767
{
68-
if (innerBranch.Tip is null)
69-
{
70-
return new Branch(innerBranch, repoDiff, this);
71-
}
72-
73-
var cacheKey = $"{innerBranch.RemoteName}/{innerBranch.CanonicalName}@{innerBranch.Tip.Sha}";
68+
var cacheKey = innerBranch.Tip is null
69+
? $"{innerBranch.RemoteName}/{innerBranch.CanonicalName}"
70+
: $"{innerBranch.RemoteName}/{innerBranch.CanonicalName}@{innerBranch.Tip.Sha}";
7471
return cachedBranches.GetOrAdd(cacheKey, _ => new Branch(innerBranch, repoDiff, this));
7572
}
7673

77-
public Commit GetOrWrap(LibGit2Sharp.Commit innerCommit, Diff repoDiff) =>
78-
cachedCommits.GetOrAdd(innerCommit.Sha, _ => new Commit(innerCommit, repoDiff, this));
74+
public Commit GetOrWrap(LibGit2Sharp.Commit innerCommit, Diff repoDiff)
75+
=> cachedCommits.GetOrAdd(innerCommit.Sha, _ => new Commit(innerCommit, repoDiff, this));
7976

8077
public Tag GetOrWrap(LibGit2Sharp.Tag innerTag, Diff repoDiff)
81-
{
82-
var cacheKey = $"{innerTag.CanonicalName}@{innerTag.Target.Sha}";
83-
return cachedTags.GetOrAdd(cacheKey, _ => new Tag(innerTag, repoDiff, this));
84-
}
78+
=> cachedTags.GetOrAdd(innerTag.CanonicalName, _ => new Tag(innerTag, repoDiff, this));
8579

8680
public void Dispose()
8781
{

src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@ namespace GitVersion.Git;
55
internal sealed class ReferenceCollection : IReferenceCollection
66
{
77
private readonly LibGit2Sharp.ReferenceCollection innerCollection;
8-
private IReadOnlyCollection<IReference>? references;
8+
private readonly Lazy<IReadOnlyCollection<IReference>> references;
99

10-
internal ReferenceCollection(LibGit2Sharp.ReferenceCollection collection) => this.innerCollection = collection.NotNull();
11-
12-
public IEnumerator<IReference> GetEnumerator()
10+
internal ReferenceCollection(LibGit2Sharp.ReferenceCollection collection)
1311
{
14-
this.references ??= [.. this.innerCollection.Select(reference => new Reference(reference))];
15-
return this.references.GetEnumerator();
12+
this.innerCollection = collection.NotNull();
13+
this.references = new Lazy<IReadOnlyCollection<IReference>>(() => [.. this.innerCollection.Select(reference => new Reference(reference))]);
1614
}
1715

16+
public IEnumerator<IReference> GetEnumerator() => this.references.Value.GetEnumerator();
17+
1818
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1919

2020
public void Add(string name, string canonicalRefNameOrObject, bool allowOverwrite = false) => this.innerCollection.Add(name, canonicalRefNameOrObject, allowOverwrite);
2121

2222
public void UpdateTarget(IReference directRef, IObjectId targetId)
23-
{
24-
RepositoryExtensions.RunSafe(() => this.innerCollection.UpdateTarget((Reference)directRef, (ObjectId)targetId));
25-
this.references = null;
26-
}
23+
=> RepositoryExtensions.RunSafe(() => this.innerCollection.UpdateTarget((Reference)directRef, (ObjectId)targetId));
2724

2825
public IReference? this[string name]
2926
{

src/GitVersion.LibGit2Sharp/Git/RemoteCollection.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ namespace GitVersion.Git;
55
internal sealed class RemoteCollection : IRemoteCollection
66
{
77
private readonly LibGit2Sharp.RemoteCollection innerCollection;
8-
private IReadOnlyCollection<IRemote>? remotes;
8+
private readonly Lazy<IReadOnlyCollection<IRemote>> remotes;
99

10-
internal RemoteCollection(LibGit2Sharp.RemoteCollection collection) => this.innerCollection = collection.NotNull();
11-
12-
public IEnumerator<IRemote> GetEnumerator()
10+
internal RemoteCollection(LibGit2Sharp.RemoteCollection collection)
1311
{
14-
this.remotes ??= [.. this.innerCollection.Select(reference => new Remote(reference))];
15-
return this.remotes.GetEnumerator();
12+
this.innerCollection = collection.NotNull();
13+
this.remotes = new Lazy<IReadOnlyCollection<IRemote>>(() => [.. this.innerCollection.Select(reference => new Remote(reference))]);
1614
}
1715

16+
public IEnumerator<IRemote> GetEnumerator() => this.remotes.Value.GetEnumerator();
17+
1818
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1919

2020
public IRemote? this[string name]
@@ -27,14 +27,8 @@ public IRemote? this[string name]
2727
}
2828

2929
public void Remove(string remoteName)
30-
{
31-
this.innerCollection.Remove(remoteName);
32-
this.remotes = null;
33-
}
30+
=> this.innerCollection.Remove(remoteName);
3431

3532
public void Update(string remoteName, string refSpec)
36-
{
37-
this.innerCollection.Update(remoteName, r => r.FetchRefSpecs.Add(refSpec));
38-
this.remotes = null;
39-
}
33+
=> this.innerCollection.Update(remoteName, r => r.FetchRefSpecs.Add(refSpec));
4034
}

0 commit comments

Comments
 (0)