Skip to content

Commit

Permalink
Make sure Streams are disposed of.
Browse files Browse the repository at this point in the history
  • Loading branch information
qmfrederik committed Jul 27, 2021
1 parent edc1621 commit c955e1f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/NerdBank.GitVersioning.Tests/ManagedGit/GitPackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,24 @@ public void TryGetObjectTest()
using (SHA1 sha = SHA1.Create())
{
Assert.True(gitPack.TryGetObject(GitObjectId.Parse("f5b401f40ad83f13030e946c9ea22cb54cb853cd"), "commit", out Stream commitStream));

// This commit is not deltafied. It is stored as a .gz-compressed stream in the pack file.
var zlibStream = Assert.IsType<ZLibStream>(commitStream);
var deflateStream = Assert.IsType<DeflateStream>(zlibStream.BaseStream);
var pooledStream = Assert.IsType<MemoryMappedStream>(deflateStream.BaseStream);

Assert.Equal(222, commitStream.Length);
Assert.Equal("/zgldANj+jvgOwlecnOKylZDVQg=", Convert.ToBase64String(sha.ComputeHash(commitStream)));
using (commitStream)
{
// This commit is not deltafied. It is stored as a .gz-compressed stream in the pack file.
var zlibStream = Assert.IsType<ZLibStream>(commitStream);
var deflateStream = Assert.IsType<DeflateStream>(zlibStream.BaseStream);

if (IntPtr.Size > 4)
{
var pooledStream = Assert.IsType<MemoryMappedStream>(deflateStream.BaseStream);
}
else
{
var directAccessStream = Assert.IsType<FileStream>(deflateStream.BaseStream);
}

Assert.Equal(222, commitStream.Length);
Assert.Equal("/zgldANj+jvgOwlecnOKylZDVQg=", Convert.ToBase64String(sha.ComputeHash(commitStream)));
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/NerdBank.GitVersioning/ManagedGit/GitPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,17 @@ public Stream GetObject(long offset, string objectType)
}

var packStream = this.GetPackStream();
Stream objectStream = GitPackReader.GetObject(this, packStream, offset, objectType, packObjectType);
Stream objectStream;

try
{
objectStream = GitPackReader.GetObject(this, packStream, offset, objectType, packObjectType);
}
catch
{
packStream.Dispose();
throw;
}

return this.cache.Add(offset, objectStream);
}
Expand Down

0 comments on commit c955e1f

Please sign in to comment.