Skip to content

Commit 02e1f52

Browse files
committed
fixup! WIP
1 parent 1d8f4da commit 02e1f52

File tree

4 files changed

+86
-52
lines changed

4 files changed

+86
-52
lines changed

LibGit2Sharp/Core/Proxy.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,12 +1399,12 @@ public static ObjectId git_note_create(
13991399
public static string git_note_default_ref(RepositorySafeHandle repo)
14001400
{
14011401
using (ThreadAffinity())
1402+
using (var buf = new GitBuf())
14021403
{
1403-
string notes_ref;
1404-
int res = NativeMethods.git_note_default_ref(out notes_ref, repo);
1404+
int res = NativeMethods.git_note_default_ref(buf, repo);
14051405
Ensure.ZeroResult(res);
14061406

1407-
return notes_ref;
1407+
return LaxUtf8Marshaler.FromNative(buf.ptr);
14081408
}
14091409
}
14101410

@@ -1609,7 +1609,7 @@ public static ICollection<TResult> git_odb_foreach<TResult>(
16091609
IntPtr.Zero));
16101610
}
16111611

1612-
public static OdbStreamSafeHandle git_odb_open_wstream(ObjectDatabaseSafeHandle odb, UIntPtr size, GitObjectType type)
1612+
public static OdbStreamSafeHandle git_odb_open_wstream(ObjectDatabaseSafeHandle odb, long size, GitObjectType type)
16131613
{
16141614
using (ThreadAffinity())
16151615
{
@@ -2150,20 +2150,20 @@ public static void git_remote_set_push_refspecs(RemoteSafeHandle remote, IEnumer
21502150
}
21512151
}
21522152

2153-
public static void git_remote_set_url(RemoteSafeHandle remote, string url)
2153+
public static void git_remote_set_url(RepositorySafeHandle repo, string remote, string url)
21542154
{
21552155
using (ThreadAffinity())
21562156
{
2157-
int res = NativeMethods.git_remote_set_url(remote, url);
2157+
int res = NativeMethods.git_remote_set_url(repo, remote, url);
21582158
Ensure.ZeroResult(res);
21592159
}
21602160
}
21612161

2162-
public static void git_remote_set_pushurl(RemoteSafeHandle remote, string url)
2162+
public static void git_remote_set_pushurl(RepositorySafeHandle repo, string remote, string url)
21632163
{
21642164
using (ThreadAffinity())
21652165
{
2166-
int res = NativeMethods.git_remote_set_pushurl(remote, url);
2166+
int res = NativeMethods.git_remote_set_pushurl(repo, remote, url);
21672167
Ensure.ZeroResult(res);
21682168
}
21692169
}
@@ -2330,9 +2330,9 @@ public static void git_remote_save(RemoteSafeHandle remote)
23302330
}
23312331
}
23322332

2333-
public static void git_remote_set_autotag(RemoteSafeHandle remote, TagFetchMode value)
2333+
public static void git_remote_set_autotag(RepositorySafeHandle repo, string remote, TagFetchMode value)
23342334
{
2335-
NativeMethods.git_remote_set_autotag(remote, value);
2335+
NativeMethods.git_remote_set_autotag(repo, remote, value);
23362336
}
23372337

23382338
public static void git_remote_set_callbacks(RemoteSafeHandle remote, ref GitRemoteCallbacks callbacks)

LibGit2Sharp/Network.cs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Globalization;
45
using System.Linq;
6+
using System.Security.Policy;
57
using LibGit2Sharp.Core;
68
using LibGit2Sharp.Core.Handles;
79
using LibGit2Sharp.Handlers;
@@ -88,32 +90,60 @@ public virtual IEnumerable<DirectReference> ListReferences(string url)
8890
}
8991
}
9092

91-
static void DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, string logMessage)
93+
static RemoteSafeHandle Build(RepositorySafeHandle repoHandle, Remote remote, string url)
94+
{
95+
Debug.Assert((remote == null) ^ (url == null));
96+
97+
RemoteSafeHandle remoteHandle;
98+
99+
if (url != null)
100+
{
101+
remoteHandle = Proxy.git_remote_create_anonymous(repoHandle, url, null);
102+
}
103+
else
104+
{
105+
remoteHandle = Proxy.git_remote_lookup(repoHandle, remote.Name, true);
106+
}
107+
108+
return remoteHandle;
109+
}
110+
111+
static void DoFetch(RepositorySafeHandle repoHandle, Remote remote, string url,
112+
FetchOptions options, string logMessage,
113+
IEnumerable<string> refspecs = null)
92114
{
93115
if (options == null)
94116
{
95117
options = new FetchOptions();
96118
}
97119

98-
if (options.TagFetchMode.HasValue)
120+
using (RemoteSafeHandle remoteHandle = Build(repoHandle, remote, url))
99121
{
100-
Proxy.git_remote_set_autotag(remoteHandle, options.TagFetchMode.Value);
101-
}
122+
if (options.TagFetchMode.HasValue)
123+
{
124+
Proxy.git_remote_set_autotag(repoHandle, remote.Name, options.TagFetchMode.Value);
125+
}
126+
127+
if (refspecs != null)
128+
{
129+
Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs);
130+
}
102131

103-
var callbacks = new RemoteCallbacks(options);
104-
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
132+
var callbacks = new RemoteCallbacks(options);
133+
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
105134

106-
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
107-
// the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
108-
// to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
109-
// where the managed layer could move the git_remote_callbacks to a different location in memory,
110-
// but libgit2 would still reference the old address.
111-
//
112-
// Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
113-
// GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
114-
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
135+
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
136+
// the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
137+
// to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
138+
// where the managed layer could move the git_remote_callbacks to a different location in memory,
139+
// but libgit2 would still reference the old address.
140+
//
141+
// Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
142+
// GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
143+
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
115144

116-
Proxy.git_remote_fetch(remoteHandle, logMessage);
145+
Proxy.git_remote_fetch(remoteHandle, logMessage);
146+
}
117147
}
118148

119149
/// <summary>
@@ -127,10 +157,7 @@ public virtual void Fetch(Remote remote, FetchOptions options = null,
127157
{
128158
Ensure.ArgumentNotNull(remote, "remote");
129159

130-
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true))
131-
{
132-
DoFetch(remoteHandle, options, logMessage);
133-
}
160+
DoFetch(repository.Handle, remote, null, options, logMessage);
134161
}
135162

136163
/// <summary>
@@ -146,12 +173,7 @@ public virtual void Fetch(Remote remote, IEnumerable<string> refspecs, FetchOpti
146173
Ensure.ArgumentNotNull(remote, "remote");
147174
Ensure.ArgumentNotNull(refspecs, "refspecs");
148175

149-
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true))
150-
{
151-
Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs);
152-
153-
DoFetch(remoteHandle, options, logMessage);
154-
}
176+
DoFetch(repository.Handle, remote, null, options, logMessage, refspecs);
155177
}
156178

157179
/// <summary>
@@ -170,12 +192,7 @@ public virtual void Fetch(
170192
Ensure.ArgumentNotNull(url, "url");
171193
Ensure.ArgumentNotNull(refspecs, "refspecs");
172194

173-
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_create_anonymous(repository.Handle, url, null))
174-
{
175-
Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs);
176-
177-
DoFetch(remoteHandle, options, logMessage);
178-
}
195+
DoFetch(repository.Handle, null, options, logMessage, refspecs);
179196
}
180197

181198
/// <summary>

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public virtual bool Contains(ObjectId objectId)
7575
/// <summary>
7676
/// Retrieves the header of a GitObject from the object database. The header contains the Size
7777
/// and Type of the object. Note that most backends do not support reading only the header
78-
/// of an object, so the whole object will be read and then size would be returned.
78+
/// of an object, so the whole object will be read and then size would be returned.
7979
/// </summary>
8080
/// <param name="objectId">Object Id of the queried object</param>
8181
/// <returns>GitObjectMetadata object instance containg object header information</returns>
@@ -212,24 +212,37 @@ public virtual Blob CreateBlob(Stream stream, string hintpath = null, int? numbe
212212
/// <param name="stream">The stream from which will be read the content of the blob to be created.</param>
213213
/// <param name="numberOfBytesToConsume">Number of bytes to consume from the stream.</param>
214214
/// <returns>The created <see cref="Blob"/>.</returns>
215+
[Obsolete("This method will be removed in the next release. Please use CreateBlob(Stream stream, long) instead.")]
215216
public virtual Blob CreateBlob(Stream stream, int numberOfBytesToConsume)
216217
{
218+
return CreateBlob(stream, (long)numberOfBytesToConsume);
219+
}
220+
221+
/// <summary>
222+
/// Inserts a <see cref="Blob"/> into the object database created from the content of the stream.
223+
/// </summary>
224+
/// <param name="stream">The stream from which will be read the content of the blob to be created.</param>
225+
/// <param name="numberOfBytesToConsume">Number of bytes to consume from the stream.</param>
226+
/// <returns>The created <see cref="Blob"/>.</returns>
227+
public virtual Blob CreateBlob(Stream stream, long numberOfBytesToConsume)
228+
{
229+
217230
Ensure.ArgumentNotNull(stream, "stream");
218231

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

224-
using (var odbStream = Proxy.git_odb_open_wstream(handle, (UIntPtr)numberOfBytesToConsume, GitObjectType.Blob))
237+
using (var odbStream = Proxy.git_odb_open_wstream(handle, numberOfBytesToConsume, GitObjectType.Blob))
225238
{
226239
var buffer = new byte[4*1024];
227-
int totalRead = 0;
240+
long totalRead = 0;
228241

229242
while (totalRead < numberOfBytesToConsume)
230243
{
231-
var left = numberOfBytesToConsume - totalRead;
232-
var toRead = left < buffer.Length ? left : buffer.Length;
244+
long left = numberOfBytesToConsume - totalRead;
245+
int toRead = left < buffer.Length ? (int)left : buffer.Length;
233246
var read = stream.Read(buffer, 0, toRead);
234247

235248
if (read == 0)

LibGit2Sharp/RemoteUpdater.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class RemoteUpdater : IDisposable
1414
private readonly UpdatingCollection<string> fetchRefSpecs;
1515
private readonly UpdatingCollection<string> pushRefSpecs;
1616
private readonly RemoteSafeHandle remoteHandle;
17+
private readonly Repository repo;
18+
private readonly Remote remote;
1719

1820
/// <summary>
1921
/// Needed for mocking purposes.
@@ -26,6 +28,9 @@ internal RemoteUpdater(Repository repo, Remote remote)
2628
Ensure.ArgumentNotNull(repo, "repo");
2729
Ensure.ArgumentNotNull(remote, "remote");
2830

31+
this.repo = repo;
32+
this.remote = remote;
33+
2934
fetchRefSpecs = new UpdatingCollection<string>(GetFetchRefSpecs, SetFetchRefSpecs);
3035
pushRefSpecs = new UpdatingCollection<string>(GetPushRefSpecs, SetPushRefSpecs);
3136

@@ -61,8 +66,7 @@ public virtual TagFetchMode TagFetchMode
6166
{
6267
set
6368
{
64-
Proxy.git_remote_set_autotag(remoteHandle, value);
65-
Proxy.git_remote_save(remoteHandle);
69+
Proxy.git_remote_set_autotag(repo.Handle, remote.Name, value);
6670
}
6771
}
6872

@@ -73,19 +77,19 @@ public virtual string Url
7377
{
7478
set
7579
{
76-
Proxy.git_remote_set_url(remoteHandle, value);
80+
Proxy.git_remote_set_url(repo.Handle, remote.Name, value);
7781
Proxy.git_remote_save(remoteHandle);
7882
}
7983
}
80-
84+
8185
/// <summary>
8286
/// Sets the push url defined for this <see cref="Remote"/>
8387
/// </summary>
8488
public virtual string PushUrl
8589
{
8690
set
8791
{
88-
Proxy.git_remote_set_pushurl(remoteHandle, value);
92+
Proxy.git_remote_set_pushurl(repo.Handle, remote.Name, value);
8993
Proxy.git_remote_save(remoteHandle);
9094
}
9195
}

0 commit comments

Comments
 (0)