Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new overload to list remote references accepting both an url and a CredentialsHandler #1099

Merged
merged 1 commit into from
Jun 18, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions LibGit2Sharp/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public virtual RemoteCollection Remotes
/// <returns>The references in the <see cref="Remote"/> repository.</returns>
public virtual IEnumerable<DirectReference> ListReferences(Remote remote)
{
return ListReferences(remote, null);
Ensure.ArgumentNotNull(remote, "remote");

return ListReferencesInternal(remote.Url, null);
}

/// <summary>
Expand All @@ -68,20 +70,27 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote)
public virtual IEnumerable<DirectReference> ListReferences(Remote remote, CredentialsHandler credentialsProvider)
{
Ensure.ArgumentNotNull(remote, "remote");
Ensure.ArgumentNotNull(credentialsProvider, "credentialsProvider");

using (RemoteSafeHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true))
{
var gitCallbacks = new GitRemoteCallbacks { version = 1 };
return ListReferencesInternal(remote.Url, credentialsProvider);
}

if (credentialsProvider != null)
{
var callbacks = new RemoteCallbacks(credentialsProvider);
gitCallbacks = callbacks.GenerateCallbacks();
}
/// <summary>
/// List references in a remote repository.
/// <para>
/// When the remote tips are ahead of the local ones, the retrieved
/// <see cref="DirectReference"/>s may point to non existing
/// <see cref="GitObject"/>s in the local repository. In that
/// case, <see cref="DirectReference.Target"/> will return <c>null</c>.
/// </para>
/// </summary>
/// <param name="url">The url to list from.</param>
/// <returns>The references in the remote repository.</returns>
public virtual IEnumerable<DirectReference> ListReferences(string url)
{
Ensure.ArgumentNotNull(url, "url");

Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks);
return Proxy.git_remote_ls(repository, remoteHandle);
}
return ListReferencesInternal(url, null);
}

/// <summary>
Expand All @@ -94,14 +103,28 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote, Creden
/// </para>
/// </summary>
/// <param name="url">The url to list from.</param>
/// <param name="credentialsProvider">The <see cref="Func{Credentials}"/> used to connect to remote repository.</param>
/// <returns>The references in the remote repository.</returns>
public virtual IEnumerable<DirectReference> ListReferences(string url)
public virtual IEnumerable<DirectReference> ListReferences(string url, CredentialsHandler credentialsProvider)
{
Ensure.ArgumentNotNull(url, "url");
Ensure.ArgumentNotNull(credentialsProvider, "credentialsProvider");

using (RemoteSafeHandle remoteHandle = Proxy.git_remote_create_anonymous(repository.Handle, url))
return ListReferencesInternal(url, credentialsProvider);
}

private IEnumerable<DirectReference> ListReferencesInternal(string url, CredentialsHandler credentialsProvider)
{
using (RemoteSafeHandle remoteHandle = BuildRemoteSafeHandle(repository.Handle, url))
{
GitRemoteCallbacks gitCallbacks = new GitRemoteCallbacks { version = 1 };

if (credentialsProvider != null)
{
var callbacks = new RemoteCallbacks(credentialsProvider);
gitCallbacks = callbacks.GenerateCallbacks();
}

Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks);
return Proxy.git_remote_ls(repository, remoteHandle);
}
Expand Down