Skip to content

Commit 0fe65f8

Browse files
committed
GitPackMemoryCache: Dispose of all cached streams on .Dispose()
- Implement GitPackCache.Dispose - Dispose GitPackCache in GitPack.Dispose() - Dispose GitRepository in ManagedGitContext.Dispose()
1 parent 90d7e39 commit 0fe65f8

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

src/NerdBank.GitVersioning/Managed/ManagedGitContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ public override string GetShortUniqueCommitId(int minLength)
119119
return this.Repository.ShortenObjectId(this.Commit.Value.Sha, minLength);
120120
}
121121

122+
/// <inheritdoc/>
123+
protected override void Dispose(bool disposing)
124+
{
125+
this.Repository.Dispose();
126+
}
127+
122128
/// <summary>
123129
/// Encodes a commit from history in a <see cref="Version"/>
124130
/// so that the original commit can be found later.

src/NerdBank.GitVersioning/ManagedGit/GitPack.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ public void Dispose()
259259

260260
this.accessor.Dispose();
261261
this.packFile.Dispose();
262+
this.cache.Dispose();
262263
}
263264

264265
private long? GetOffset(GitObjectId objectId)

src/NerdBank.GitVersioning/ManagedGit/GitPackCache.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#nullable enable
22

3+
using System;
34
using System.Diagnostics.CodeAnalysis;
45
using System.IO;
56
using System.Text;
@@ -12,7 +13,7 @@ namespace Nerdbank.GitVersioning.ManagedGit
1213
/// data from a <see cref="GitPack"/> can be potentially expensive: the data is
1314
/// compressed and can be deltified.
1415
/// </summary>
15-
public abstract class GitPackCache
16+
public abstract class GitPackCache : IDisposable
1617
{
1718
/// <summary>
1819
/// Attempts to retrieve a Git object from cache.
@@ -51,5 +52,10 @@ public abstract class GitPackCache
5152
/// A <see cref="Stream"/> which represents the cached entry.
5253
/// </returns>
5354
public abstract Stream Add(long offset, Stream stream);
55+
56+
/// <inheritdoc/>
57+
public virtual void Dispose()
58+
{
59+
}
5460
}
5561
}

src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCache.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Diagnostics.CodeAnalysis;
55
using System.IO;
6+
using System.Linq;
67
using System.Text;
78

89
namespace Nerdbank.GitVersioning.ManagedGit
@@ -55,5 +56,17 @@ public override void GetCacheStatistics(StringBuilder builder)
5556
{
5657
builder.AppendLine($"{this.cache.Count} items in cache");
5758
}
59+
60+
/// <inheritdoc/>
61+
public override void Dispose()
62+
{
63+
while (this.cache.Count > 0)
64+
{
65+
var key = this.cache.Keys.First();
66+
var stream = this.cache[key];
67+
stream.Dispose();
68+
this.cache.Remove(key);
69+
}
70+
}
5871
}
5972
}

src/NerdBank.GitVersioning/ManagedGit/GitPackMemoryCacheStream.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public override void Write(byte[] buffer, int offset, int count)
9999
throw new NotSupportedException();
100100
}
101101

102+
protected override void Dispose(bool disposing)
103+
{
104+
this.stream.Dispose();
105+
this.cacheStream.Dispose();
106+
}
107+
102108
private void DisposeStreamIfRead()
103109
{
104110
if (this.cacheStream.Length == this.stream.Length)

0 commit comments

Comments
 (0)