8000 Fix credential acquire callback interop · mm201/libgit2sharp@0ace1a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ace1a4

Browse files
committed
Fix credential acquire callback interop
1 parent 7f723ca commit 0ace1a4

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ internal static extern int git_config_foreach(
304304
internal delegate int git_cred_acquire_cb(
305305
out IntPtr cred,
306306
IntPtr url,
307+
IntPtr username_from_url,
307308
uint allowed_types,
308309
IntPtr payload);
309310

LibGit2Sharp/Network.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ public virtual void Push(
147147
Ensure.ArgumentNotNull(remote, "remote");
148148
Ensure.ArgumentNotNull(pushRefSpecs, "pushRefSpecs");
149149

150+
// We need to keep a reference to the git_cred_acquire_cb callback around
151+
// so it will not be garbage collected before we are done with it.
152+
// Note that we also have a GC.KeepAlive call at the end of the method.
153+
NativeMethods.git_cred_acquire_cb credentialCallback = null;
154+
150155
// Return early if there is nothing to push.
151156
if (!pushRefSpecs.Any())
152157
{
@@ -160,10 +165,12 @@ public virtual void Push(
160165
{
161166
if (credentials != null)
162167
{
168+
credentialCallback = (out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
169+
NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
170+
163171
Proxy.git_remote_set_cred_acquire_cb(
164172
remoteHandle,
165-
(out IntPtr cred, IntPtr url, uint types, IntPtr payload) =>
166-
NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password),
173+
credentialCallback,
167174
IntPtr.Zero);
168175
}
169176

@@ -197,6 +204,10 @@ public virtual void Push(
197204
Proxy.git_remote_disconnect(remoteHandle);
198205
}
199206
}
207+
208+
// To be safe, make sure the credential callback is kept until
209+
// alive until at least this point.
210+
GC.KeepAlive(credentialCallback);
200211
}
201212

202213
/// <summary>

LibGit2Sharp/Remote.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,26 @@ public virtual void Fetch(
6969
TransferProgressHandler onTransferProgress = null,
7070
Credentials credentials = null)
7171
{
72+
// We need to keep a reference to the git_cred_acquire_cb callback around
73+
// so it will not be garbage collected before we are done with it.
74+
// Note that we also have a GC.KeepAlive call at the end of the method.
75+
NativeMethods.git_cred_acquire_cb credentialCallback = null;
76+
7277
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, this.Name, true))
7378
{
7479
var callbacks = new RemoteCallbacks(onProgress, onCompletion, onUpdateTips);
7580
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
7681

7782
Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode);
7883

79-
// Username/password auth
8084
if (credentials != null)
8185
{
86+
credentialCallback = (out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
87+
NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
88+
8289
Proxy.git_remot 8000 e_set_cred_acquire_cb(
8390
remoteHandle,
84-
(out IntPtr cred, IntPtr url, uint types, IntPtr payload) =>
85-
NativeMethods.git_cred_userpass_plaintext_new(out cred,
86-
credentials.Username,
87-
credentials.Password),
91+
credentialCallback,
8892
IntPtr.Zero);
8993
}
9094

@@ -109,6 +113,10 @@ public virtual void Fetch(
109113
Proxy.git_remote_disconnect(remoteHandle);
110114
}
111115
}
116+
117+
// To be safe, make sure the credential callback is kept until
118+
// alive until at least this point.
119+
GC.KeepAlive(credentialCallback);
112120
}
113121

114122
/// <summary>

LibGit2Sharp/Repository.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,16 @@ public static Repository Clone(string sourceUrl, string workdirPath,
503503
if (credentials != null)
504504
{
505505
cloneOpts.CredAcquireCallback =
506-
(out IntPtr cred, IntPtr url, uint types, IntPtr payload) =>
506+
(out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
507507
NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
508508
}
509509

510510
using(Proxy.git_clone(sourceUrl, workdirPath, cloneOpts)) {}
511+
512+
// To be safe, make sure the credential callback is kept until
513+
// alive until at least this point.
514+
GC.KeepAlive(cloneOpts.CredAcquireCallback);
515+
511516
return new Repository(workdirPath, options);
512517
}
513518

0 commit comments

Comments
 (0)
0