From 3d8258626ae39b1a2a23b7d26a27ce862ffc49bf Mon Sep 17 00:00:00 2001 From: Justin Swanson Date: Thu, 21 Nov 2024 21:02:20 -0600 Subject: [PATCH] Just skip if we have issues initializing backup repo closes #469 --- .../Settings/BackupSettings.cs | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/Synthesis.Bethesda.GUI/Settings/BackupSettings.cs b/Synthesis.Bethesda.GUI/Settings/BackupSettings.cs index c9ec83ab..e9d074d5 100644 --- a/Synthesis.Bethesda.GUI/Settings/BackupSettings.cs +++ b/Synthesis.Bethesda.GUI/Settings/BackupSettings.cs @@ -34,19 +34,55 @@ public BackupSettings( public void Start() { - _initRepository.Init(RepoDirectory); - CreateGitIgnore(); - using var repo = _repositoryCheckouts.Get(RepoDirectory); - StageIfExists(GitIgnorePath, repo.Repository); - StageIfExists(PipelineSettings, repo.Repository); - StageIfExists(GuiSettings, repo.Repository); try { + _initRepository.Init(RepoDirectory); + } + catch (LibGit2SharpException e) + { + _logger.Warning(e, "Could not initiate backup repository."); + return; + } + try + { + CreateGitIgnore(); + using var repo = _repositoryCheckouts.Get(RepoDirectory); + StageIfExists(GitIgnorePath, repo.Repository); + StageIfExists(PipelineSettings, repo.Repository); + StageIfExists(GuiSettings, repo.Repository); repo.Repository.Commit("Settings changed"); } catch (EmptyCommitException) { } + catch (Exception ex) + { + HandleException(ex); + } + } + + private void HandleException(Exception? ex) + { + switch (ex) + { + case LibGit2SharpException: + case NullReferenceException: + WipeBackup(ex); + break; + case AggregateException agg: + HandleException(agg.InnerException); + break; + case null: + return; + default: + throw ex; + } + } + + private void WipeBackup(Exception ex) + { + _logger.Warning(ex, "Wiping backup settings."); + _fileSystem.Directory.DeleteEntireFolder(RepoDirectory); } private void CreateGitIgnore()