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
namespaceLibGit2Sharp.Core
55
{
66
[StructLayout(LayoutKind.Sequential)]
7-
internalclassGitWriteStream
7+
internalstructGitWriteStream
88
{
99
[MarshalAs(UnmanagedType.FunctionPtr)]
1010
publicwrite_fnwrite;

‎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
intmax_length,
127127
IntPtrdata);
128128

129+
[DllImport(libgit2)]
130+
internalstaticexternunsafeintgit_blob_create_fromstream(
131+
outIntPtrstream,
132+
git_repository*repositoryPtr,
133+
[MarshalAs(UnmanagedType.CustomMarshaler,MarshalCookie=UniqueId.UniqueIdentifier,MarshalTypeRef=typeof(StrictFilePathMarshaler))]FilePathhintpath);
134+
135+
[DllImport(libgit2)]
136+
internalstaticexternunsafeintgit_blob_create_fromstream_commit(
137+
refGitOidoid,
138+
IntPtrstream);
139+
129140
[DllImport(libgit2)]
130141
internalstaticexternunsafeintgit_blob_create_fromchunks(
131142
refGitOidoid,

‎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-
publicstaticunsafeObjectIdgit_blob_create_fromchunks(RepositoryHandlerepo,FilePathhintpath,NativeMethods.source_callbackfileCallback)
118+
publicstaticunsafeIntPtrgit_blob_create_fromstream(RepositoryHandlerepo,FilePathhintpath)
119119
{
120-
varoid=newGitOid();
121-
intres=NativeMethods.git_blob_create_fromchunks(refoid,repo,hintpath,fileCallback,IntPtr.Zero);
122-
123-
if(res==(int)GitErrorCode.User)
124-
{
125-
thrownewEndOfStreamException("The stream ended unexpectedly");
126-
}
120+
IntPtrwritestream_ptr;
127121

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

126+
publicstaticunsafeObjectIdgit_blob_create_fromstream_commit(IntPtrwritestream_ptr)
127+
{
128+
varoid=newGitOid();
129+
Ensure.ZeroResult(NativeMethods.git_blob_create_fromstream_commit(refoid,writestream_ptr));
130130
returnoid;
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=newGitWriteStream();
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=newWriteStream(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
returnCreateBlob(stream,hintpath,(long?)numberOfBytesToConsume);
214214
}
215215

216-
privateBlobCreateBlob(Streamstream,stringhintpath,long?numberOfBytesToConsume)
216+
privateunsafeBlobCreateBlob(Streamstream,stringhintpath,long?numberOfBytesToConsume)
217217
{
218218
Ensure.ArgumentNotNull(stream,"stream");
219219

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

231-
varproc=newProcessor(stream,numberOfBytesToConsume);
232-
ObjectIdid=Proxy.git_blob_create_fromchunks(repo.Handle,hintpath,proc.Provider);
231+
IntPtrwritestream_ptr=Proxy.git_blob_create_fromstream(repo.Handle,hintpath);
232+
GitWriteStreamwritestream=(GitWriteStream)Marshal.PtrToStructure(writestream_ptr,typeof(GitWriteStream));
233233

234+
try
235+
{
236+
varbuffer=newbyte[4*1024];
237+
longtotalRead=0;
238+
intread=0;
239+
240+
while(true)
241+
{
242+
inttoRead=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+
thrownewEndOfStreamException("The stream ended unexpectedly");
267+
}
268+
}
269+
catch(Exceptione)
270+
{
271+
writestream.free(writestream_ptr);
272+
throwe;
273+
}
274+
275+
ObjectIdid=Proxy.git_blob_create_fromstream_commit(writestream_ptr);
234276
returnrepo.Lookup<Blob>(id);
235277
}
236278

0 commit comments

Comments
(0)