Skip to content

Commit 333a4cf

Browse files
committed
fixup! WIP
1 parent ea58197 commit 333a4cf

File tree

4 files changed

+85
-51
lines changed

4 files changed

+85
-51
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;
@@ -104,32 +106,60 @@ public virtual IEnumerable<DirectReference> ListReferences(string url)
104106
}
105107
}
106108

107-
static void DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, string logMessage)
109+
static RemoteSafeHandle Build(RepositorySafeHandle repoHandle, Remote remote, string url)
110+
{
111+
Debug.Assert((remote == null) ^ (url == null));
112+
113+
RemoteSafeHandle remoteHandle;
114+
115+
if (url != null)
116+
{
117+
remoteHandle = Proxy.git_remote_create_anonymous(repoHandle, url, null);
118+
}
119+
else
120+
{
121+
remoteHandle = Proxy.git_remote_lookup(repoHandle, remote.Name, true);
122+
}
123+
124+
return remoteHandle;
125+
}
126+
127+
static void DoFetch(RepositorySafeHandle repoHandle, Remote remote, string url,
128+
FetchOptions options, string logMessage,
129+
IEnumerable<string> refspecs = null)
108130
{
109131
if (options == null)
110132
{
111133
options = new FetchOptions();
112134
}
113135

114-
if (options.TagFetchMode.HasValue)
136+
using (RemoteSafeHandle remoteHandle = Build(repoHandle, remote, url))
115137
{
116-
Proxy.git_remote_set_autotag(remoteHandle, options.TagFetchMode.Value);
117-
}
138+
if (options.TagFetchMode.HasValue)
139+
{
140+
Proxy.git_remote_set_autotag(repoHandle, remote.Name, options.TagFetchMode.Value);
141+
}
142+
143+
if (refspecs != null)
144+
{
145+
Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs);
146+
}
118147

119-
var callbacks = new RemoteCallbacks(options);
120-
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
148+
var callbacks = new RemoteCallbacks(options);
149+
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
121150

122-
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
123-
// the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
124-
// to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
125-
// where the managed layer could move the git_remote_callbacks to a different location in memory,
126-
// but libgit2 would still reference the old address.
127-
//
128-
// Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
129-
// GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
130-
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
151+
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
152+
// the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
153+
// to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
154+
// where the managed layer could move the git_remote_callbacks to a different location in memory,
155+
// but libgit2 would still reference the old address.
156+
//
157+
// Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
158+
// GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
159+
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
131160

132-
Proxy.git_remote_fetch(remoteHandle, logMessage);
161+
Proxy.git_remote_fetch(remoteHandle, logMessage);
162+
}
133163
}
134164

135165
/// <summary>
@@ -171,10 +201,7 @@ public virtual void Fetch(Remote remote, FetchOptions options, string logMessage
171201
{
172202
Ensure.ArgumentNotNull(remote, "remote");
173203

174-
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true))
175-
{
176-
DoFetch(remoteHandle, options, logMessage);
177-
}
204+
DoFetch(repository.Handle, remote, null, options, logMessage);
178205
}
179206

180207
/// <summary>
@@ -221,12 +248,7 @@ public virtual void Fetch(Remote remote, IEnumerable<string> refspecs, FetchOpti
221248
Ensure.ArgumentNotNull(remote, "remote");
222249
Ensure.ArgumentNotNull(refspecs, "refspecs");
223250

224-
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true))
225-
{
226-
Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs);
227-
228-
DoFetch(remoteHandle, options, logMessage);
229-
}
251+
DoFetch(repository.Handle, remote, null, options, logMessage, refspecs);
230252
}
231253

232254
/// <summary>
@@ -285,12 +307,7 @@ public virtual void Fetch(
285307
Ensure.ArgumentNotNull(url, "url");
286308
Ensure.ArgumentNotNull(refspecs, "refspecs");
287309

288-
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_create_anonymous(repository.Handle, url, null))
289-
{
290-
Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs);
291-
292-
DoFetch(remoteHandle, options, logMessage);
293-
}
310+
DoFetch(repository.Handle, null, options, logMessage, refspecs);
294311
}
295312

296313
/// <summary>

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,24 +240,37 @@ internal Blob CreateBlob(Stream stream, string hintpath, int? numberOfBytesToCon
240240
/// <param name="stream">The stream from which will be read the content of the blob to be created.</param>
241241
/// <param name="numberOfBytesToConsume">Number of bytes to consume from the stream.</param>
242242
/// <returns>The created <see cref="Blob"/>.</returns>
243+
[Obsolete("This method will be removed in the next release. Please use CreateBlob(Stream stream, long) instead.")]
243244
public virtual Blob CreateBlob(Stream stream, int numberOfBytesToConsume)
244245
{
246+
return CreateBlob(stream, (long)numberOfBytesToConsume);
247+
}
248+
249+
/// <summary>
250+
/// Inserts a <see cref="Blob"/> into the object database created from the content of the stream.
251+
/// </summary>
252+
/// <param name="stream">The stream from which will be read the content of the blob to be created.</param>
253+
/// <param name="numberOfBytesToConsume">Number of bytes to consume from the stream.</param>
254+
/// <returns>The created <see cref="Blob"/>.</returns>
255+
public virtual Blob CreateBlob(Stream stream, long numberOfBytesToConsume)
256+
{
257+
245258
Ensure.ArgumentNotNull(stream, "stream");
246259

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

252-
using (var odbStream = Proxy.git_odb_open_wstream(handle, (UIntPtr)numberOfBytesToConsume, GitObjectType.Blob))
265+
using (var odbStream = Proxy.git_odb_open_wstream(handle, numberOfBytesToConsume, GitObjectType.Blob))
253266
{
254267
var buffer = new byte[4*1024];
255-
int totalRead = 0;
268+
long totalRead = 0;
256269

257270
while (totalRead < numberOfBytesToConsume)
258271
{
259-
var left = numberOfBytesToConsume - totalRead;
260-
var toRead = left < buffer.Length ? left : buffer.Length;
272+
long left = numberOfBytesToConsume - totalRead;
273+
int toRead = left < buffer.Length ? (int)left : buffer.Length;
261274
var read = stream.Read(buffer, 0, toRead);
262275

263276
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)