3using System.Threading.Tasks;
6using LibGit2Sharp.Handlers;
8using Microsoft.Extensions.Logging;
22 readonly ILogger<LibGit2RepositoryFactory>
logger;
30 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
36 logger.LogTrace(
"Creating in-memory libgit2 repository...");
37 using (
new LibGit2Sharp.Repository())
38 logger.LogTrace(
"Success");
42 public async ValueTask<LibGit2Sharp.IRepository>
CreateFromPath(
string path, CancellationToken cancellationToken)
44 ArgumentNullException.ThrowIfNull(path);
46 var repo = await Task.Factory.StartNew<LibGit2Sharp.IRepository>(
49 logger.LogTrace(
"Creating libgit2 repository at {repoPath}...", path);
50 return new LibGit2Sharp.Repository(path);
54 TaskScheduler.Current);
60 public Task
Clone(Uri url, CloneOptions cloneOptions,
string path, CancellationToken cancellationToken) => Task.Factory.StartNew(
65 logger.LogTrace(
"Cloning {repoUrl} into {repoPath}...", url, path);
66 LibGit2Sharp.Repository.Clone(url.ToString(), path, cloneOptions);
68 catch (UserCancelledException ex)
70 logger.LogTrace(ex,
"Suppressing clone cancellation exception");
71 cancellationToken.ThrowIfCancellationRequested();
73 catch (LibGit2SharpException ex)
81 TaskScheduler.Current);
86 ArgumentNullException.ThrowIfNull(gitRemoteFeatures);
89 return (a, b, supportedCredentialTypes) =>
91 var hasCreds = username !=
null;
92 var supportsUserPass = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.UsernamePassword);
93 var supportsAnonymous = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.Default);
96 "Credentials requested. Present: {credentialsPresent}. Supports anonymous: {credentialsSupportAnon}. Supports user/pass: {credentialsSupportUserPass}",
100 if (supportsUserPass && hasCreds)
101 return new UsernamePasswordCredentials
104 Password = transformedPassword,
107 if (supportsAnonymous)
110 if (supportsUserPass)
120 ArgumentNullException.ThrowIfNull(exception);
122 if (exception.Message ==
"too many redirects or authentication replays")
123 throw new JobException(
"Bad git credentials exchange!", exception);
125 if (exception.Message ==
ErrorCode.RepoCredentialsRequired.Describe())
129 if (exception.InnerException is LibGit2SharpException innerException)
Represents initial credentials used by the server.
readonly ILogger< LibGit2RepositoryFactory > logger
The ILogger for the LibGit2RepositoryFactory.
async ValueTask< CredentialsHandler > GenerateCredentialsHandler(IGitRemoteFeatures gitRemoteFeatures, string? username, string? password, CancellationToken cancellationToken)
Generate a CredentialsHandler from a given username and password .A ValueTask<TResult> resulting in ...
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.
Provides features for remote git services.
ValueTask< string?> TransformRepositoryPassword(string? rawPassword, CancellationToken cancellationToken)
Transform a service's rawPassword into a password usable by git.
Factory for creating LibGit2Sharp.IRepositorys.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.