tgstation-server 6.12.0
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;
7using Microsoft.Extensions.Logging;
8
12
14{
17 {
21 readonly ILogger<LibGit2RepositoryFactory> logger;
22
27 public LibGit2RepositoryFactory(ILogger<LibGit2RepositoryFactory> logger)
28 {
29 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
30 }
31
33 public void CreateInMemory()
34 {
35 logger.LogTrace("Creating in-memory libgit2 repository...");
36 using (new LibGit2Sharp.Repository())
37 logger.LogTrace("Success");
38 }
39
41 public async ValueTask<LibGit2Sharp.IRepository> CreateFromPath(string path, CancellationToken cancellationToken)
42 {
43 ArgumentNullException.ThrowIfNull(path);
44
45 var repo = await Task.Factory.StartNew<LibGit2Sharp.IRepository>(
46 () =>
47 {
48 logger.LogTrace("Creating libgit2 repository at {repoPath}...", path);
49 return new LibGit2Sharp.Repository(path);
50 },
51 cancellationToken,
53 TaskScheduler.Current);
54
55 return repo;
56 }
57
59 public Task Clone(Uri url, CloneOptions cloneOptions, string path, CancellationToken cancellationToken) => Task.Factory.StartNew(
60 () =>
61 {
62 try
63 {
64 logger.LogTrace("Cloning {repoUrl} into {repoPath}...", url, path);
65 LibGit2Sharp.Repository.Clone(url.ToString(), path, cloneOptions);
66 }
67 catch (UserCancelledException ex)
68 {
69 logger.LogTrace(ex, "Suppressing clone cancellation exception");
70 cancellationToken.ThrowIfCancellationRequested();
71 }
72 catch (LibGit2SharpException ex)
73 {
75 throw;
76 }
77 },
78 cancellationToken,
80 TaskScheduler.Current);
81
83 public CredentialsHandler GenerateCredentialsHandler(string? username, string? password) => (a, b, supportedCredentialTypes) =>
84 {
85 var hasCreds = username != null;
86 var supportsUserPass = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.UsernamePassword);
87 var supportsAnonymous = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.Default);
88
89 logger.LogTrace(
90 "Credentials requested. Present: {credentialsPresent}. Supports anonymous: {credentialsSupportAnon}. Supports user/pass: {credentialsSupportUserPass}",
91 hasCreds,
92 supportsAnonymous,
93 supportsUserPass);
94 if (supportsUserPass && hasCreds)
95 return new UsernamePasswordCredentials
96 {
97 Username = username,
98 Password = password,
99 };
100
101 if (supportsAnonymous)
102 return new DefaultCredentials();
103
104 if (supportsUserPass)
105 throw new JobException(ErrorCode.RepoCredentialsRequired);
106
107 throw new JobException(ErrorCode.RepoCannotAuthenticate);
108 };
109
111 public void CheckBadCredentialsException(LibGit2SharpException exception)
112 {
113 ArgumentNullException.ThrowIfNull(exception);
114
115 if (exception.Message == "too many redirects or authentication replays")
116 throw new JobException("Bad git credentials exchange!", exception);
117
118 if (exception.Message == ErrorCode.RepoCredentialsRequired.Describe())
119 throw new JobException(ErrorCode.RepoCredentialsRequired);
120
121 // submodule recursion
122 if (exception.InnerException is LibGit2SharpException innerException)
123 CheckBadCredentialsException(innerException);
124 }
125 }
126}
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.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12