Skip to content

Commit 15505bf

Browse files
author
Edward Thomson
committed
Use git_blob_fromstream instead of fromchunks
Make GitWriteStream a struct instead of a class so that we can accept a native `git_writestream`. Use `git_blob_fromstream` to stream data into a blob instead of the now-removed `git_blob_fromchunks`.
1 parent 3aa531f commit 15505bf

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

LibGit2Sharp/Core/GitWriteStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace LibGit2Sharp.Core
55
{
66
[StructLayout(LayoutKind.Sequential)]
7-
internal class GitWriteStream
7+
internal struct GitWriteStream
88
{
99
[MarshalAs(UnmanagedType.FunctionPtr)]
1010
public write_fn write;

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ internal delegate int source_callback(
126126
int max_length,
127127
IntPtr data);
128128

129+
[DllImport(libgit2)]
130+
internal static extern unsafe int git_blob_create_fromstream(
131+
out IntPtr stream,
132+
git_repository* repositoryPtr,
133+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath hintpath);
134+
135+
[DllImport(libgit2)]
136+
internal static extern unsafe int git_blob_create_fromstream_commit(
137+
ref GitOid oid,
138+
IntPtr stream);
139+
129140
[DllImport(libgit2)]
130141
internal static extern unsafe int git_blob_create_fromchunks(
131142
ref GitOid oid,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,18 @@ public static unsafe BlameHandle git_blame_file(
115115

116116
#region git_blob_
117117

118-
public static unsafe ObjectId git_blob_create_fromchunks(RepositoryHandle repo, FilePath hintpath, NativeMethods.source_callback fileCallback)
118+
public static unsafe IntPtr git_blob_create_fromstream(RepositoryHandle repo, FilePath hintpath)
119119
{
120-
var oid = new GitOid();
121-
int res = NativeMethods.git_blob_create_fromchunks(ref oid, repo, hintpath, fileCallback, IntPtr.Zero);
122-
123-
if (res == (int)GitErrorCode.User)
124-
{
125-
throw new EndOfStreamException("The stream ended unexpectedly");
126-
}
120+
IntPtr writestream_ptr;
127121

128-
Ensure.ZeroResult(res);
122+
Ensure.ZeroResult(NativeMethods.git_blob_create_fromstream(out writestream_ptr, repo, hintpath));
123+
return writestream_ptr;
124+
}
129125

126+
public static unsafe ObjectId git_blob_create_fromstream_commit(IntPtr writestream_ptr)
127+
{
128+
var oid = new GitOid();
129+
Ensure.ZeroResult(NativeMethods.git_blob_create_fromstream_commit(ref oid, writestream_ptr));
130130
return oid;
131131
}
132132

LibGit2Sharp/Filter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ int StreamCreateCallback(out IntPtr git_writestream_out, GitFilter self, IntPtr
261261
Marshal.StructureToPtr(state.thisStream, state.thisPtr, false);
262262

263263
state.nextPtr = git_writestream_next;
264-
state.nextStream = new GitWriteStream();
265-
Marshal.PtrToStructure(state.nextPtr, state.nextStream);
264+
state.nextStream = (GitWriteStream)Marshal.PtrToStructure(state.nextPtr, typeof(GitWriteStream));
266265

267266
state.filterSource = FilterSource.FromNativePtr(filterSourcePtr);
268267
state.output = new WriteStream(state.nextStream, state.nextPtr);

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public virtual Blob CreateBlob(Stream stream, string hintpath, long numberOfByte
213213
return CreateBlob(stream, hintpath, (long?)numberOfBytesToConsume);
214214
}
215215

216-
private Blob CreateBlob(Stream stream, string hintpath, long? numberOfBytesToConsume)
216+
private unsafe Blob CreateBlob(Stream stream, string hintpath, long? numberOfBytesToConsume)
217217
{
218218
Ensure.ArgumentNotNull(stream, "stream");
219219

@@ -228,9 +228,51 @@ private Blob CreateBlob(Stream stream, string hintpath, long? numberOfBytesToCon
228228
throw new ArgumentException("The stream cannot be read from.", "stream");
229229
}
230230

231-
var proc = new Processor(stream, numberOfBytesToConsume);
232-
ObjectId id = Proxy.git_blob_create_fromchunks(repo.Handle, hintpath, proc.Provider);
231+
IntPtr writestream_ptr = Proxy.git_blob_create_fromstream(repo.Handle, hintpath);
232+
GitWriteStream writestream = (GitWriteStream)Marshal.PtrToStructure(writestream_ptr, typeof(GitWriteStream));
233233

234+
try
235+
{
236+
var buffer = new byte[4 * 1024];
237+
long totalRead = 0;
238+
int read = 0;
239+
240+
while (true)
241+
{
242+
int toRead = numberOfBytesToConsume.HasValue ?
243+
(int)Math.Min(numberOfBytesToConsume.Value - totalRead, (long)buffer.Length) :
244+
buffer.Length;
245+
246+
if (toRead > 0)
247+
{
248+
read = (toRead > 0) ? stream.Read(buffer, 0, toRead) : 0;
249+
}
250+
251+
if (read == 0)
252+
{
253+
break;
254+
}
255+
256+
fixed (byte* buffer_ptr = buffer)
257+
{
258+
writestream.write(writestream_ptr, (IntPtr)buffer_ptr, (UIntPtr)read);
259+
}
260+
261+
totalRead += read;
262+
}
263+
264+
if (numberOfBytesToConsume.HasValue && totalRead < numberOfBytesToConsume.Value)
265+
{
266+
throw new EndOfStreamException("The stream ended unexpectedly");
267+
}
268+
}
269+
catch(Exception e)
270+
{
271+
writestream.free(writestream_ptr);
272+
throw e;
273+
}
274+
275+
ObjectId id = Proxy.git_blob_create_fromstream_commit(writestream_ptr);
234276
return repo.Lookup<Blob>(id);
235277
}
236278

0 commit comments

Comments
 (0)