You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
recyclarr/src/VersionControl/GitRepositoryFactory.cs

54 lines
1.6 KiB

using System;
using Common;
using LibGit2Sharp;
using VersionControl.Wrappers;
namespace VersionControl;
public class GitRepositoryFactory : IGitRepositoryFactory
{
private readonly IFileUtilities _fileUtils;
private readonly IRepositoryStaticWrapper _staticWrapper;
private readonly Func<string, IGitRepository> _repoFactory;
public GitRepositoryFactory(
IFileUtilities fileUtils,
IRepositoryStaticWrapper staticWrapper,
Func<string, IGitRepository> repoFactory)
{
_fileUtils = fileUtils;
_staticWrapper = staticWrapper;
_repoFactory = repoFactory;
}
public IGitRepository CreateAndCloneIfNeeded(string repoUrl, string repoPath, string branch)
{
if (!_staticWrapper.IsValid(repoPath))
{
DeleteAndCloneRepo(repoUrl, repoPath, branch);
}
return _repoFactory(repoPath);
}
private void DeleteAndCloneRepo(string repoUrl, string repoPath, string branch)
{
_fileUtils.DeleteReadOnlyDirectory(repoPath);
Console.Write("Requesting and parsing guide markdown ");
using var progress = new ProgressBar();
_staticWrapper.Clone(repoUrl, repoPath, new CloneOptions
{
RecurseSubmodules = false,
BranchName = branch,
OnTransferProgress = gitProgress =>
{
// ReSharper disable once AccessToDisposedClosure
progress.Report((float) gitProgress.ReceivedObjects / gitProgress.TotalObjects);
return true;
}
});
}
}