tgstation-server  4.4.0
The /tg/station 13 server suite
LibGit2RepositoryFactory.cs
Go to the documentation of this file.
1 using LibGit2Sharp;
2 using LibGit2Sharp.Handlers;
3 using Microsoft.Extensions.Logging;
4 using System;
5 using System.Threading;
6 using System.Threading.Tasks;
9 
10 namespace Tgstation.Server.Host.Components.Repository
11 {
14  {
18  readonly ILogger<LibGit2RepositoryFactory> logger;
19 
24  public LibGit2RepositoryFactory(ILogger<LibGit2RepositoryFactory> logger)
25  {
26  this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
27  }
28 
30  public LibGit2Sharp.IRepository CreateInMemory()
31  {
32  logger.LogTrace("Creating in-memory libgit2 repository...");
33  var repo = new LibGit2Sharp.Repository();
34  try
35  {
36  logger.LogTrace("Successfully created in-memory libgit2 repository.");
37  return repo;
38  }
39  catch
40  {
41  repo.Dispose();
42  throw;
43  }
44  }
45 
47  public Task<LibGit2Sharp.IRepository> CreateFromPath(string path, CancellationToken cancellationToken)
48  {
49  if (path == null)
50  throw new ArgumentNullException(nameof(path));
51 
52  return Task.Factory.StartNew<LibGit2Sharp.IRepository>(
53  () =>
54  {
55  logger.LogTrace("Creating libgit2 repostory at {0}...", path);
56  return new LibGit2Sharp.Repository(path);
57  },
58  cancellationToken,
59  TaskCreationOptions.LongRunning,
60  TaskScheduler.Current);
61  }
62 
64  public Task Clone(Uri url, CloneOptions cloneOptions, string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
65  {
66  try
67  {
68  logger.LogTrace("Cloning {0} into {1}...", url, path);
69  LibGit2Sharp.Repository.Clone(url.ToString(), path, cloneOptions);
70  }
71  catch (UserCancelledException ex)
72  {
73  logger.LogTrace("Suppressing clone cancellation exception: {0}", ex);
74  cancellationToken.ThrowIfCancellationRequested();
75  }
76  }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
77 
79  public CredentialsHandler GenerateCredentialsHandler(string username, string password) => (a, b, supportedCredentialTypes) =>
80  {
81  var hasCreds = username != null;
82  var supportsUserPass = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.UsernamePassword);
83  var supportsAnonymous = supportedCredentialTypes.HasFlag(SupportedCredentialTypes.Default);
84 
85  logger.LogTrace("Credentials requested. Present: {0}. Supports anonymous: {1}. Supports user/pass: {2}", hasCreds, supportsAnonymous, supportsUserPass);
86  if (supportsUserPass && hasCreds)
87  return new UsernamePasswordCredentials
88  {
89  Username = username,
90  Password = password
91  };
92 
93  if (supportsAnonymous)
94  return new DefaultCredentials();
95 
96  if (supportsUserPass)
97  throw new JobException(ErrorCode.RepoCredentialsRequired);
98 
99  throw new JobException(ErrorCode.RepoCannotAuthenticate);
100  };
101  }
102 }
ErrorCode
Types of ErrorMessages that the API may return.
Definition: ErrorCode.cs:10
readonly ILogger< LibGit2RepositoryFactory > logger
The ILogger for the LibGit2RepositoryFactory.
LibGit2Sharp.IRepository CreateInMemory()
Create an in-memeory LibGit2Sharp.IRepository.
LibGit2RepositoryFactory(ILogger< LibGit2RepositoryFactory > logger)
Initializes a new instance of the LibGit2RepositoryFactory .
Operation exceptions thrown from the context of a Models.Job
Definition: JobException.cs:9
Represents a git repository
Definition: Repository.cs:9
Task< LibGit2Sharp.IRepository > CreateFromPath(string path, CancellationToken cancellationToken)
Load a LibGit2Sharp.IRepository from a given path .