3using System.Threading.Tasks;
6using LibGit2Sharp.Handlers;
7using Microsoft.Extensions.Logging;
21 readonly ILogger<LibGit2RepositoryFactory>
logger;
29 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
35 logger.LogTrace(
"Creating in-memory libgit2 repository...");
36 using (
new LibGit2Sharp.Repository())
37 logger.LogTrace(
"Success");
41 public async ValueTask<LibGit2Sharp.IRepository>
CreateFromPath(
string path, CancellationToken cancellationToken)
43 ArgumentNullException.ThrowIfNull(path);
45 var repo = await Task.Factory.StartNew<LibGit2Sharp.IRepository>(
48 logger.LogTrace(
"Creating libgit2 repository at {repoPath}...", path);
49 return new LibGit2Sharp.Repository(path);
53 TaskScheduler.Current);
59 public Task
Clone(Uri url, CloneOptions cloneOptions,
string path, CancellationToken cancellationToken) => Task.Factory.StartNew(
64 logger.LogTrace(
"Cloning {repoUrl} into {repoPath}...", url, path);
65 LibGit2Sharp.Repository.Clone(url.ToString(), path, cloneOptions);
67 catch (UserCancelledException ex)
69 logger.LogTrace(ex,
"Suppressing clone cancellation exception");
70 cancellationToken.ThrowIfCancellationRequested();
72 catch (LibGit2SharpException ex)
80 TaskScheduler.Current);
85 var hasCreds = username !=
null;
86 var supportsUserPass = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.UsernamePassword);
87 var supportsAnonymous = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.Default);
90 "Credentials requested. Present: {credentialsPresent}. Supports anonymous: {credentialsSupportAnon}. Supports user/pass: {credentialsSupportUserPass}",
94 if (supportsUserPass && hasCreds)
95 return new UsernamePasswordCredentials
101 if (supportsAnonymous)
104 if (supportsUserPass)
113 ArgumentNullException.ThrowIfNull(exception);
115 if (exception.Message ==
"too many redirects or authentication replays")
116 throw new JobException(
"Bad git credentials exchange!", exception);
118 if (exception.Message ==
ErrorCode.RepoCredentialsRequired.Describe())
122 if (exception.InnerException is LibGit2SharpException innerException)
Represents initial credentials used by the server.
readonly ILogger< LibGit2RepositoryFactory > logger
The ILogger for the LibGit2RepositoryFactory.
CredentialsHandler GenerateCredentialsHandler(string? username, string? password)
Generate a CredentialsHandler from a given username and password .A new CredentialsHandler.
void CreateInMemory()
Create and destory an in-memeory LibGit2Sharp.IRepository.Used as a test of the libgit2 native librar...
LibGit2RepositoryFactory(ILogger< LibGit2RepositoryFactory > logger)
Initializes a new instance of the LibGit2RepositoryFactory class.
async ValueTask< LibGit2Sharp.IRepository > CreateFromPath(string path, CancellationToken cancellationToken)
Load a LibGit2Sharp.IRepository from a given path .A ValueTask<TResult> resulting in the loaded LibGi...
Task Clone(Uri url, CloneOptions cloneOptions, string path, CancellationToken cancellationToken)
Clone a remote LibGit2Sharp.IRepository.A Task representing the running operation.
void CheckBadCredentialsException(LibGit2SharpException exception)
Rethrow the authentication failure message as a JobException if it is one.
IIOManager that resolves paths to Environment.CurrentDirectory.
const TaskCreationOptions BlockingTaskCreationOptions
The TaskCreationOptions used to spawn Tasks for potentially long running, blocking operations.
Operation exceptions thrown from the context of a Models.Job.
Factory for creating LibGit2Sharp.IRepositorys.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.