Skip to content

Jwonagel/proxy options initializing #1853

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

Closed
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion LibGit2Sharp/Commands/Fetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static void Fetch(Repository repository, string remote, IEnumerable<strin
fetchOptions.CustomHeaders = GitStrArrayManaged.BuildFrom(options.CustomHeaders);
}

fetchOptions.ProxyOptions = new GitProxyOptions { Version = 1 };
fetchOptions.ProxyOptions = GitProxyOptionsFactory.CreateGitProxyOptions(options.ProxyOptions);

Proxy.git_remote_fetch(remoteHandle, refspecs, fetchOptions, logMessage);
}
Expand Down
25 changes: 25 additions & 0 deletions LibGit2Sharp/Core/GitProxyOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace LibGit2Sharp.Core
{
Expand All @@ -20,4 +21,28 @@ internal struct GitProxyOptions
public IntPtr CertificateCheck;
public IntPtr CbPayload;
}

internal static class GitProxyOptionsFactory
{
internal static GitProxyOptions CreateGitProxyOptions(ProxyOptions proxyOptions)
{
var options = new GitProxyOptions
{
Version = 1,
Type = GitProxyType.None,
};

if (proxyOptions != null)
{
options.Type = (GitProxyType)proxyOptions.ProxyType;

if (!string.IsNullOrWhiteSpace(proxyOptions.Url))
{
options.Url = EncodingMarshaler.FromManaged(Encoding.UTF8, proxyOptions.Url);
}
}

return options;
}
}
}
10 changes: 10 additions & 0 deletions LibGit2Sharp/FetchOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,15 @@ internal FetchOptionsBase()
/// Completed operating on the current repository.
/// </summary>
public RepositoryOperationCompleted RepositoryOperationCompleted { get; set; }

/// <summary>
/// Configures operating behind a proxy
/// <para>
/// If not set, any proxy settings will be ignored
/// </para>
/// </summary>
public ProxyOptions ProxyOptions { get; set; }
}


}
4 changes: 2 additions & 2 deletions LibGit2Sharp/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private IEnumerable<Reference> ListReferencesInternal(string url, CredentialsHan
using (RemoteHandle remoteHandle = BuildRemoteHandle(repository.Handle, url))
{
GitRemoteCallbacks gitCallbacks = new GitRemoteCallbacks { version = 1 };
GitProxyOptions proxyOptions = new GitProxyOptions { Version = 1 };
GitProxyOptions proxyOptions = GitProxyOptionsFactory.CreateGitProxyOptions(null);

if (credentialsProvider != null)
{
Expand Down Expand Up @@ -375,7 +375,7 @@ public virtual void Push(Remote remote, IEnumerable<string> pushRefSpecs, PushOp
{
PackbuilderDegreeOfParallelism = pushOptions.PackbuilderDegreeOfParallelism,
RemoteCallbacks = gitCallbacks,
ProxyOptions = new GitProxyOptions { Version = 1 },
ProxyOptions = GitProxyOptionsFactory.CreateGitProxyOptions(pushOptions.ProxyOptions)
});
}
}
Expand Down
45 changes: 45 additions & 0 deletions LibGit2Sharp/ProxyOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace LibGit2Sharp
{
/// <summary>
/// Collection of parameters controlling proxy behavior.
/// </summary>
public sealed class ProxyOptions
{
/// <summary>
/// Constructor.
/// </summary>
public ProxyOptions()
{ }

/// <summary>
/// The type of proxy to use, by URL, auto-detect.
/// </summary>
public ProxyType ProxyType { get; set; }

/// <summary>
/// The URL of the proxy. (ProxyType must be Specified)
/// </summary>
public string Url { get; set; }
}

/// <summary>
/// The type of proxy to use.
/// </summary>
public enum ProxyType
{
/// <summary>
/// Do not attempt to connect through a proxy
/// If built against libcurl, it itself may attempt to connect
/// to a proxy if the environment variables specify it.
/// </summary>
None = 0,
/// <summary>
/// Try to auto-detect the proxy from the git configuration.
/// </summary>
Auto = 1,
/// <summary>
/// Connect via the URL given in the options
/// </summary>
Specified = 2
}
}
8 changes: 8 additions & 0 deletions LibGit2Sharp/PushOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@ public sealed class PushOptions
/// information about what updates will be performed.
/// </summary>
public PrePushHandler OnNegotiationCompletedBeforePush { get; set; }

/// <summary>
/// Configures operating behind a proxy
/// <para>
/// If not set, any proxy settings will be ignored
/// </para>
/// </summary>
public ProxyOptions ProxyOptions { get; set; }
}
}
22 changes: 22 additions & 0 deletions LibGit2Sharp/RemoteOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace LibGit2Sharp
{
/// <summary>
/// Collection of parameters controlling reote behavior.
/// </summary>
public sealed class RemoteOptions
{
/// <summary>
/// Constructor.
/// </summary>
public RemoteOptions()
{ }

/// <summary>
/// Configures operating behind a proxy
/// <para>
/// If not set, any proxy settings will be ignored
/// </para>
/// </summary>
public ProxyOptions ProxyOptions { get; set; }
}
}
21 changes: 19 additions & 2 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,14 +671,31 @@ public static IEnumerable<Reference> ListRemoteReferences(string url)
/// <param name="credentialsProvider">The <see cref="Func{Credentials}"/> used to connect to remote repository.</param>
/// <returns>The references in the remote repository.</returns>
public static IEnumerable<Reference> ListRemoteReferences(string url, CredentialsHandler credentialsProvider)
{
return ListRemoteReferences(url, credentialsProvider, new RemoteOptions());
}

/// <summary>
/// Lists the Remote Repository References.
/// </summary>
/// <para>
/// Does not require a local Repository. The retrieved
/// <see cref="IBelongToARepository.Repository"/>
/// throws <see cref="InvalidOperationException"/> in this case.
/// </para>
/// <param name="url">The url to list from.</param>
/// <param name="credentialsProvider">The <see cref="Func{Credentials}"/> used to connect to remote repository.</param>
/// <param name="options">Options for remote behavior <see cref="RemoteOptions"/></param>
/// <returns>The references in the remote repository.</returns>
public static IEnumerable<Reference> ListRemoteReferences(string url, CredentialsHandler credentialsProvider, RemoteOptions options)
{
Ensure.ArgumentNotNull(url, "url");

using (RepositoryHandle repositoryHandle = Proxy.git_repository_new())
using (RemoteHandle remoteHandle = Proxy.git_remote_create_anonymous(repositoryHandle, url))
{
var gitCallbacks = new GitRemoteCallbacks { version = 1 };
var proxyOptions = new GitProxyOptions { Version = 1 };
var proxyOptions = GitProxyOptionsFactory.CreateGitProxyOptions(options?.ProxyOptions);

if (credentialsProvider != null)
{
Expand Down Expand Up @@ -768,7 +785,7 @@ public static string Clone(string sourceUrl, string workdirPath,
var gitCheckoutOptions = checkoutOptionsWrapper.Options;

var gitFetchOptions = fetchOptionsWrapper.Options;
gitFetchOptions.ProxyOptions = new GitProxyOptions { Version = 1 };
gitFetchOptions.ProxyOptions = GitProxyOptionsFactory.CreateGitProxyOptions(options.ProxyOptions);
gitFetchOptions.RemoteCallbacks = new RemoteCallbacks(options).GenerateCallbacks();
if (options.FetchOptions != null && options.FetchOptions.CustomHeaders != null)
{
Expand Down
4 changes: 3 additions & 1 deletion LibGit2Sharp/SubmoduleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ public virtual void Update(string name, SubmoduleUpdateOptions options)
var remoteCallbacks = new RemoteCallbacks(options);
var gitRemoteCallbacks = remoteCallbacks.GenerateCallbacks();

var proxyOptions = GitProxyOptionsFactory.CreateGitProxyOptions(options.ProxyOptions);

var gitSubmoduleUpdateOpts = new GitSubmoduleUpdateOptions
{
Version = 1,
CheckoutOptions = gitCheckoutOptions,
FetchOptions = new GitFetchOptions { ProxyOptions = new GitProxyOptions { Version = 1 }, RemoteCallbacks = gitRemoteCallbacks },
FetchOptions = new GitFetchOptions { ProxyOptions = proxyOptions, RemoteCallbacks = gitRemoteCallbacks },
CloneCheckoutStrategy = CheckoutStrategy.GIT_CHECKOUT_SAFE
};

Expand Down