From ad0773be2ff48839abaf34bad8db0bb58994003b Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 26 Aug 2024 17:50:13 +0800 Subject: [PATCH] enhance: avoid crashing when failed to create filesystem watcher for repository (#411) --- src/ViewModels/Repository.cs | 38 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 30e46dc2..2a395c4a 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -331,7 +331,15 @@ namespace SourceGit.ViewModels _settings = new Models.RepositorySettings(); } - _watcher = new Models.Watcher(this); + try + { + _watcher = new Models.Watcher(this); + } + catch (Exception ex) + { + App.RaiseException(string.Empty, $"Failed to start watcher for repository: '{_fullpath}'. You may need to press 'F5' to refresh repository manually!\n\nReason: {ex.Message}"); + } + _histories = new Histories(this); _workingCopy = new WorkingCopy(this); _stashesPage = new StashesPage(this); @@ -351,7 +359,7 @@ namespace SourceGit.ViewModels File.WriteAllText(Path.Combine(_gitDir, "sourcegit.settings"), settingsSerialized); _settings = null; - _watcher.Dispose(); + _watcher?.Dispose(); _histories.Cleanup(); _workingCopy.Cleanup(); _stashesPage.Cleanup(); @@ -580,19 +588,33 @@ namespace SourceGit.ViewModels public void SetWatcherEnabled(bool enabled) { - if (_watcher != null) - _watcher.SetEnabled(enabled); + _watcher?.SetEnabled(enabled); } public void MarkBranchesDirtyManually() { - if (_watcher != null) + if (_watcher == null) + { + Task.Run(() => + { + RefreshBranches(); + RefreshCommits(); + }); + + Task.Run(RefreshWorkingCopyChanges); + Task.Run(RefreshWorktrees); + } + else + { _watcher.MarkBranchDirtyManually(); + } } public void MarkWorkingCopyDirtyManually() { - if (_watcher != null) + if (_watcher == null) + Task.Run(RefreshWorkingCopyChanges); + else _watcher.MarkWorkingCopyDirtyManually(); } @@ -787,9 +809,7 @@ namespace SourceGit.ViewModels public void RefreshSubmodules() { var submodules = new Commands.QuerySubmodules(_fullpath).Result(); - if (_watcher != null) - _watcher.SetSubmodules(submodules); - + _watcher?.SetSubmodules(submodules); Dispatcher.UIThread.Invoke(() => Submodules = submodules); }