diff --git a/src/TrashLib/Repo/VersionControl/GitRepository.cs b/src/TrashLib/Repo/VersionControl/GitRepository.cs index 61437391..d8b4d609 100644 --- a/src/TrashLib/Repo/VersionControl/GitRepository.cs +++ b/src/TrashLib/Repo/VersionControl/GitRepository.cs @@ -34,7 +34,7 @@ public sealed class GitRepository : IGitRepository .WithWorkingDirectory(_paths.RepoDirectory.FullName) .ExecuteAsync(); - _log.Debug("{Output}", output.ToString()); + _log.Debug("Command Output: {Output}", output.ToString().Trim()); if (result.ExitCode != 0) { diff --git a/src/TrashLib/Repo/VersionControl/GitRepositoryFactory.cs b/src/TrashLib/Repo/VersionControl/GitRepositoryFactory.cs index 64a88fb5..8d3caa75 100644 --- a/src/TrashLib/Repo/VersionControl/GitRepositoryFactory.cs +++ b/src/TrashLib/Repo/VersionControl/GitRepositoryFactory.cs @@ -1,6 +1,8 @@ using System.IO.Abstractions; +using System.Text; using CliWrap; using Common; +using Serilog; namespace TrashLib.Repo.VersionControl; @@ -9,44 +11,60 @@ public class GitRepositoryFactory : IGitRepositoryFactory private readonly IFileUtilities _fileUtils; private readonly Func _repoFactory; private readonly IGitPath _gitPath; + private readonly ILogger _log; public GitRepositoryFactory( IFileUtilities fileUtils, Func repoFactory, - IGitPath gitPath) + IGitPath gitPath, + ILogger log) { _fileUtils = fileUtils; _repoFactory = repoFactory; _gitPath = gitPath; + _log = log; } - private async Task IsValid(IDirectoryInfo repoPath) + private async Task<(bool Succeeded, string OutputMsg, string ErrorMsg)> IsValid(IDirectoryInfo repoPath) { + var output = new StringBuilder(); + var error = new StringBuilder(); var result = await Cli.Wrap(_gitPath.Path) .WithArguments("status") + .WithStandardErrorPipe(PipeTarget.ToStringBuilder(error)) + .WithStandardOutputPipe(PipeTarget.ToStringBuilder(output)) .WithValidation(CommandResultValidation.None) .WithWorkingDirectory(repoPath.FullName) .ExecuteAsync(); - return result.ExitCode == 0; + return (result.ExitCode == 0, output.ToString(), error.ToString()); } public async Task CreateAndCloneIfNeeded(string repoUrl, string repoPath, string branch) { var repo = _repoFactory(repoPath); - if (!await IsValid(repo.Path)) + if (!repo.Path.Exists) { - await DeleteAndCloneRepo(repo, repoUrl, branch); + _log.Information("Cloning trash repository..."); + await repo.Clone(repoUrl, branch); + } + else + { + var validResult = await IsValid(repo.Path); + if (!validResult.Succeeded) + { + _log.Information("Git repository is invalid; deleting and cloning again"); + _log.Debug("Validity Check Output: {Message}", validResult.OutputMsg.Trim()); + _log.Debug("Validity Check Error: {Message}", validResult.ErrorMsg.Trim()); + + _fileUtils.DeleteReadOnlyDirectory(repo.Path.FullName); + await repo.Clone(repoUrl, branch); + } } + _log.Debug("Remote 'origin' set to {Url}", repoUrl); await repo.SetRemote("origin", repoUrl); return repo; } - - private async Task DeleteAndCloneRepo(IGitRepository repo, string repoUrl, string branch) - { - _fileUtils.DeleteReadOnlyDirectory(repo.Path.FullName); - await repo.Clone(repoUrl, branch); - } }