tgstation-server 6.19.1
The /tg/station 13 server suite
Loading...
Searching...
No Matches
LibGit2RepositoryFactory.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
5using LibGit2Sharp;
6using LibGit2Sharp.Handlers;
7
8using Microsoft.Extensions.Logging;
9
13
15{
18 {
22 readonly ILogger<LibGit2RepositoryFactory> logger;
23
28 public LibGit2RepositoryFactory(ILogger<LibGit2RepositoryFactory> logger)
29 {
30 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
31 }
32
34 public void CreateInMemory()
35 {
36 logger.LogTrace("Creating in-memory libgit2 repository...");
37 using (new LibGit2Sharp.Repository())
38 logger.LogTrace("Success");
39 }
40
42 public async ValueTask<LibGit2Sharp.IRepository> CreateFromPath(string path, CancellationToken cancellationToken)
43 {
44 ArgumentNullException.ThrowIfNull(path);
45
46 var repo = await Task.Factory.StartNew<LibGit2Sharp.IRepository>(
47 () =>
48 {
49 logger.LogTrace("Creating libgit2 repository at {repoPath}...", path);
50 return new LibGit2Sharp.Repository(path);
51 },
52 cancellationToken,
54 TaskScheduler.Current);
55
56 return repo;
57 }
58
60 public Task Clone(Uri url, CloneOptions cloneOptions, string path, CancellationToken cancellationToken) => Task.Factory.StartNew(
61 () =>
62 {
63 try
64 {
65 logger.LogTrace("Cloning {repoUrl} into {repoPath}...", url, path);
66 LibGit2Sharp.Repository.Clone(url.ToString(), path, cloneOptions);
67 }
68 catch (UserCancelledException ex)
69 {
70 logger.LogTrace(ex, "Suppressing clone cancellation exception");
71 cancellationToken.ThrowIfCancellationRequested();
72 }
73 catch (LibGit2SharpException ex)
74 {
76 throw;
77 }
78 },
79 cancellationToken,
81 TaskScheduler.Current);
82
84 public async ValueTask<CredentialsHandler> GenerateCredentialsHandler(IGitRemoteFeatures gitRemoteFeatures, string? username, string? password, CancellationToken cancellationToken)
85 {
86 ArgumentNullException.ThrowIfNull(gitRemoteFeatures);
87
88 var transformedPassword = await gitRemoteFeatures.TransformRepositoryPassword(password, cancellationToken);
89 return (a, b, supportedCredentialTypes) =>
90 {
91 var hasCreds = username != null;
92 var supportsUserPass = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.UsernamePassword);
93 var supportsAnonymous = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.Default);
94
95 logger.LogTrace(
96 "Credentials requested. Present: {credentialsPresent}. Supports anonymous: {credentialsSupportAnon}. Supports user/pass: {credentialsSupportUserPass}",
97 hasCreds,
98 supportsAnonymous,
99 supportsUserPass);
100 if (supportsUserPass && hasCreds)
101 return new UsernamePasswordCredentials
102 {
103 Username = username,
104 Password = transformedPassword,
105 };
106
107 if (supportsAnonymous)
108 return new DefaultCredentials();
109
110 if (supportsUserPass)
111 throw new JobException(ErrorCode.RepoCredentialsRequired);
112
113 throw new JobException(ErrorCode.RepoCannotAuthenticate);
114 };
115 }
116
118 public void CheckBadCredentialsException(LibGit2SharpException exception)
119 {
120 ArgumentNullException.ThrowIfNull(exception);
121
122 if (exception.Message == "too many redirects or authentication replays")
123 throw new JobException("Bad git credentials exchange!", exception);
124
125 if (exception.Message == ErrorCode.RepoCredentialsRequired.Describe())
126 throw new JobException(ErrorCode.RepoCredentialsRequired);
127
128 // submodule recursion
129 if (exception.InnerException is LibGit2SharpException innerException)
130 CheckBadCredentialsException(innerException);
131 }
132 }
133}
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.
ValueTask< string?> TransformRepositoryPassword(string? rawPassword, CancellationToken cancellationToken)
Transform a service's rawPassword into a password usable by git.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12