3using System.Threading.Tasks;
7using Microsoft.Extensions.Logging;
23 public bool InUse => semaphore.CurrentCount == 0;
66 readonly ILogger<RepositoryManager>
logger;
98 ILogger<RepositoryManager>
logger,
102 this.commands =
commands ??
throw new ArgumentNullException(nameof(
commands));
108 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
116 logger.LogTrace(
"Disposing...");
123 string? initialBranch,
127 bool recurseSubmodules,
128 CancellationToken cancellationToken)
130 ArgumentNullException.ThrowIfNull(url);
139 logger.LogInformation(
"Begin clone {url} to {path} (Branch: {initialBranch})", url, repositoryPath, initialBranch);
145 logger.LogTrace(
"Semaphore acquired for clone");
149 using var cloneProgressReporter = progressReporter.
CreateSection(
null, 0.75f);
150 using var checkoutProgressReporter = progressReporter.
CreateSection(
null, 0.25f);
151 var cloneOptions =
new CloneOptions
153 RecurseSubmodules = recurseSubmodules,
154 OnCheckoutProgress = (path, completed, remaining) =>
156 if (checkoutProgressReporter ==
null)
159 var percentage = (double)completed / remaining;
162 BranchName = initialBranch,
165 cloneOptions.FetchOptions.Hydrate(
167 cloneProgressReporter,
181 logger.LogTrace(
"Deleting partially cloned repository...");
188 logger.LogError(innerException,
"Error deleting partially cloned repository!");
195 logger.LogDebug(
"Repository exists, clone aborted!");
202 logger.LogTrace(
"Semaphore released after clone");
206 logger.LogInformation(
"Clone complete!");
211 public async ValueTask<IRepository?>
LoadRepository(CancellationToken cancellationToken)
213 logger.LogTrace(
"Begin LoadRepository...");
220 await
semaphore.WaitAsync(cancellationToken);
223 logger.LogTrace(
"Semaphore acquired for load");
239 logger.LogTrace(
"Releasing semaphore due to Repository disposal...");
245 logger.LogTrace(
"Releasing semaphore as load failed");
250 catch (RepositoryNotFoundException e)
252 logger.LogTrace(e,
"Repository not found!");
260 logger.LogInformation(
"Deleting repository...");
265 logger.LogTrace(
"Semaphore acquired, deleting Repository directory...");
271 logger.LogTrace(
"Semaphore released after delete attempt");
RepositoryManager(ILibGit2RepositoryFactory repositoryFactory, ILibGit2Commands commands, IIOManager ioManager, IEventConsumer eventConsumer, IPostWriteHandler postWriteHandler, IGitRemoteFeaturesFactory gitRemoteFeaturesFactory, ILogger< Repository > repositoryLogger, ILogger< RepositoryManager > logger, GeneralConfiguration generalConfiguration)
Initializes a new instance of the RepositoryManager class.
readonly ILogger< Repository > repositoryLogger
The ILogger created Repositorys.
async ValueTask DeleteRepository(CancellationToken cancellationToken)
Delete the current repository.A ValueTask representing the running operation.
readonly GeneralConfiguration generalConfiguration
The GeneralConfiguration for the RepositoryManager.
readonly ILibGit2RepositoryFactory repositoryFactory
The ILibGit2RepositoryFactory for the RepositoryManager.
readonly SemaphoreSlim semaphore
Used for controlling single access to the IRepository.
async ValueTask< IRepository?> LoadRepository(CancellationToken cancellationToken)
Attempt to load the IRepository from the default location.A ValueTask<TResult> resulting in the loade...
readonly ILibGit2Commands commands
The ILibGit2Commands for the RepositoryManager.
bool InUse
If something is holding a lock on the repository.
readonly IPostWriteHandler postWriteHandler
The IPostWriteHandler for the RepositoryManager.
readonly IIOManager ioManager
The IIOManager for the RepositoryManager.
async ValueTask< IRepository?> CloneRepository(Uri url, string? initialBranch, string? username, string? password, JobProgressReporter progressReporter, bool recurseSubmodules, CancellationToken cancellationToken)
Clone the repository at url .A ValueTask<TResult> resulting i the newly cloned IRepository,...
readonly ILogger< RepositoryManager > logger
The ILogger for the RepositoryManager.
readonly IEventConsumer eventConsumer
The IEventConsumer for the RepositoryManager.
bool CloneInProgress
If a CloneRepository(Uri, string, string, string, JobProgressReporter, bool, CancellationToken) opera...
readonly IGitRemoteFeaturesFactory gitRemoteFeaturesFactory
The IGitRemoteFeaturesFactory for the RepositoryManager.
General configuration options.
Operation exceptions thrown from the context of a Models.Job.
Progress reporter for a Job.
JobProgressReporter CreateSection(string? newStageName, double percentage)
Create a subsection of the JobProgressReporter with its optional own stage name.
void ReportProgress(double? progress)
Report progress.
Async lock context helper.
static async ValueTask< SemaphoreSlimContext > Lock(SemaphoreSlim semaphore, CancellationToken cancellationToken, ILogger? logger=null)
Asyncronously locks a semaphore .
Consumes EventTypes and takes the appropriate actions.
CredentialsHandler GenerateCredentialsHandler(string? username, string? password)
Generate a CredentialsHandler from a given username and password .
Factory for creating IGitRemoteFeatures.
For low level interactions with a LibGit2Sharp.IRepository.
Factory for creating LibGit2Sharp.IRepositorys.
Task Clone(Uri url, CloneOptions cloneOptions, string path, CancellationToken cancellationToken)
Clone a remote LibGit2Sharp.IRepository.
ValueTask< LibGit2Sharp.IRepository > CreateFromPath(string path, CancellationToken cancellationToken)
Load a LibGit2Sharp.IRepository from a given path .
Factory for creating and loading IRepositorys.
Interface for using filesystems.
string ResolvePath()
Retrieve the full path of the current working directory.
Task DeleteDirectory(string path, CancellationToken cancellationToken)
Recursively delete a directory, removes and does not enter any symlinks encounterd.
Task< bool > DirectoryExists(string path, CancellationToken cancellationToken)
Check that the directory at path exists.
Handles changing file modes/permissions after writing.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.