Skip to content

Introduce ObjectDatabase.Write(Stream...) #1518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions LibGit2Sharp.Tests/ObjectDatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ public void CanCreateABlobFromAStream(string expectedSha, string hintPath)
}
}

[Fact]
public void CanWriteABlobFromAByteArray()
{
var ba = Encoding.ASCII.GetBytes("libgit2\r\n");

using (var repo = new Repository(InitNewRepository()))
{
var id = repo.ObjectDatabase.Write<Blob>(ba);
Assert.Equal(new ObjectId("99115ea359379a218c47cffc83cd0af8c91c4061"), id);
}
}

[Fact]
public void CanWriteABlobFromAStream()
{
var ba = Encoding.ASCII.GetBytes("libgit2\r\n");

using (var stream = new MemoryStream(ba))
using (var repo = new Repository(InitNewRepository()))
{
var id = repo.ObjectDatabase.Write<Blob>(stream, stream.Length);
Assert.Equal(new ObjectId("99115ea359379a218c47cffc83cd0af8c91c4061"), id);
}
}

Stream PrepareMemoryStream(int contentSize)
{
var sb = new StringBuilder();
Expand Down
74 changes: 42 additions & 32 deletions LibGit2Sharp/ObjectDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public int Provider(IntPtr content, int max_length, IntPtr data)
}

/// <summary>
/// Write an object to the object database
/// Writes an object to the object database.
/// </summary>
/// <param name="data">The contents of the object</param>
/// <typeparam name="T">The type of object to write</typeparam>
Expand All @@ -187,6 +187,45 @@ public virtual ObjectId Write<T>(byte[] data) where T : GitObject
return Proxy.git_odb_write(handle, data, GitObject.TypeToKindMap[typeof(T)]);
}

/// <summary>
/// Writes an object to the object database.
/// </summary>
/// <param name="stream">The contents of the object</param>
/// <param name="numberOfBytesToConsume">The number of bytes to consume from the stream</param>
/// <typeparam name="T">The type of object to write</typeparam>
public virtual ObjectId Write<T>(Stream stream, long numberOfBytesToConsume) where T : GitObject
{
Ensure.ArgumentNotNull(stream, "stream");

if (!stream.CanRead)
{
throw new ArgumentException("The stream cannot be read from.", "stream");
}

using (var odbStream = Proxy.git_odb_open_wstream(handle, numberOfBytesToConsume, GitObjectType.Blob))
{
var buffer = new byte[4 * 1024];
long totalRead = 0;

while (totalRead < numberOfBytesToConsume)
{
long left = numberOfBytesToConsume - totalRead;
int toRead = left < buffer.Length ? (int)left : buffer.Length;
var read = stream.Read(buffer, 0, toRead);

if (read == 0)
{
throw new EndOfStreamException("The stream ended unexpectedly");
}

Proxy.git_odb_stream_write(odbStream, buffer, read);
totalRead += read;
}

return Proxy.git_odb_stream_finalize_write(odbStream);
}
}

/// <summary>
/// Inserts a <see cref="Blob"/> into the object database, created from the content of a stream.
/// <para>Optionally, git filters will be applied to the content before storing it.</para>
Expand Down Expand Up @@ -294,37 +333,8 @@ private unsafe Blob CreateBlob(Stream stream, string hintpath, long? numberOfByt
/// <returns>The created <see cref="Blob"/>.</returns>
public virtual Blob CreateBlob(Stream stream, long numberOfBytesToConsume)
{
Ensure.ArgumentNotNull(stream, "stream");

if (!stream.CanRead)
{
throw new ArgumentException("The stream cannot be read from.", "stream");
}

using (var odbStream = Proxy.git_odb_open_wstream(handle, numberOfBytesToConsume, GitObjectType.Blob))
{
var buffer = new byte[4 * 1024];
long totalRead = 0;

while (totalRead < numberOfBytesToConsume)
{
long left = numberOfBytesToConsume - totalRead;
int toRead = left < buffer.Length ? (int)left : buffer.Length;
var read = stream.Read(buffer, 0, toRead);

if (read == 0)
{
throw new EndOfStreamException("The stream ended unexpectedly");
}

Proxy.git_odb_stream_write(odbStream, buffer, read);
totalRead += read;
}

var id = Proxy.git_odb_stream_finalize_write(odbStream);

return repo.Lookup<Blob>(id);
}
var id = Write<Blob>(stream, numberOfBytesToConsume);
return repo.Lookup<Blob>(id);
}

/// <summary>
Expand Down