Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit 9962cb8

Browse files
committed
Make Network.ListReferences(Remote) accept Credentials
Add CanListRemoteReferencesWithCredentials() test.
1 parent 9015578 commit 9962cb8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

LibGit2Sharp.Tests/NetworkFixture.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ public void CanListRemoteReferenceObjects()
107107
}
108108
}
109109

110+
[SkippableFact]
111+
public void CanListRemoteReferencesWithCredentials()
112+
{
113+
InconclusiveIf(() => string.IsNullOrEmpty(Constants.PrivateRepoUrl),
114+
"Populate Constants.PrivateRepo* to run this test");
115+
116+
string remoteName = "origin";
117+
118+
string repoPath = InitNewRepository();
119+
120+
using (var repo = new Repository(repoPath))
121+
{
122+
Remote remote = repo.Network.Remotes.Add(remoteName, Constants.PrivateRepoUrl);
123+
124+
var references = repo.Network.ListReferences(remote, Constants.PrivateRepoCredentials);
125+
126+
foreach (var directReference in references)
127+
{
128+
Assert.NotNull(directReference);
129+
}
130+
}
131+
}
132+
110133
[Theory]
111134
[InlineData(FastForwardStrategy.Default)]
112135
[InlineData(FastForwardStrategy.NoFastFoward)]

LibGit2Sharp/Network.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,21 @@ public virtual RemoteCollection Remotes
4646
/// </para>
4747
/// </summary>
4848
/// <param name="remote">The <see cref="Remote"/> to list from.</param>
49+
/// <param name="credentials">The optional <see cref="Credentials"/> used to connect to remote repository.</param>
4950
/// <returns>The references in the <see cref="Remote"/> repository.</returns>
50-
public virtual IEnumerable<DirectReference> ListReferences(Remote remote)
51+
public virtual IEnumerable<DirectReference> ListReferences(Remote remote, Credentials credentials = null)
5152
{
5253
Ensure.ArgumentNotNull(remote, "remote");
5354

5455
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true))
5556
{
57+
if (credentials != null)
58+
{
59+
var callbacks = new RemoteCallbacks(null, null, null, credentials);
60+
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
61+
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
62+
}
63+
5664
Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch);
5765
return Proxy.git_remote_ls(repository, remoteHandle);
5866
}

0 commit comments

Comments
 (0)