diff --git a/pkg/gui/background.go b/pkg/gui/background.go index 2575aedd1..afd343df3 100644 --- a/pkg/gui/background.go +++ b/pkg/gui/background.go @@ -62,6 +62,17 @@ func (self *BackgroundRoutineMgr) startBackgroundRoutines() { } } + if userConfig.Git.AutoDetectExternalChanges { + interval := userConfig.Refresher.ExternalChangeCheckInterval + if interval > 0 { + go utils.Safe(self.startBackgroundExternalChangeDetection) + } else { + self.gui.c.Log.Errorf( + "Value of config option 'refresher.externalChangeCheckInterval' (%d) is invalid, disabling external change detection", + interval) + } + } + if self.gui.Config.GetDebug() { self.goEvery(time.Second*time.Duration(10), self.gui.stopChan, func(_ bool) error { formatBytes := func(b uint64) string { @@ -127,6 +138,55 @@ func (self *BackgroundRoutineMgr) startBackgroundFilesRefresh() { }) } +func (self *BackgroundRoutineMgr) startBackgroundExternalChangeDetection() { + self.gui.waitForIntro.Wait() + + // We don't seed the snapshot here. The startup refresh captures one on + // entry (like every refs-touching refresh), and until one has been + // captured RefsSnapshotChangedSince treats the empty baseline as + // "unchanged", so we never fire a spurious refresh before a baseline + // exists — no need to depend on the timing of that startup refresh. + + userConfig := self.gui.UserConfig() + self.goEvery( + userConfig.Refresher.ExternalChangeCheckIntervalDuration(), + self.gui.stopChan, + func(_ bool) error { + self.checkForExternalChanges() + return nil + }, + ) +} + +func (self *BackgroundRoutineMgr) checkForExternalChanges() { + current, err := self.gui.git.Status.RefsSnapshot() + if err != nil { + // Transient error (e.g. git process couldn't start). Don't update the + // stored snapshot; we'll retry next tick. + self.gui.c.Log.Warnf("RefsSnapshot failed: %v", err) + return + } + + if !self.gui.helpers.Refresh.RefsSnapshotChangedSince(current) { + return + } + + // goEvery checks the pause count before starting us, but a git operation + // may have begun (and paused refreshes) after that check, while we were + // reading the snapshot above. In that case the change we detected is the + // operation's own intermediate state, so back off: the operation will + // refresh and re-snapshot when it finishes, and if the change was really + // external we'll catch it on the next tick after the pause lifts. We don't + // update the stored snapshot, so nothing is swallowed. + if self.backgroundRefreshesPaused() { + return + } + + // No need to update the stored snapshot here; Refresh does that. + self.gui.c.Log.Info("External ref change detected — refreshing") + self.gui.c.Refresh(types.RefreshOptions{}) +} + // returns a channel that can be used to trigger the callback immediately func (self *BackgroundRoutineMgr) goEvery(interval time.Duration, stop chan struct{}, function func(bool) error) chan struct{} { done := make(chan struct{}) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index e2881cca1..ee58bbfb8 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -515,8 +515,10 @@ func (gui *Gui) checkForChangedConfigsThatDontAutoReload(oldConfig *config.UserC configsThatDontAutoReload := []string{ "Git.AutoFetch", "Git.AutoRefresh", + "Git.AutoDetectExternalChanges", "Refresher.RefreshInterval", "Refresher.FetchInterval", + "Refresher.ExternalChangeCheckInterval", "Update.Method", "Update.Days", } diff --git a/test/default_test_config/config.yml b/test/default_test_config/config.yml index 5a822ae77..198fcbdd1 100644 --- a/test/default_test_config/config.yml +++ b/test/default_test_config/config.yml @@ -20,3 +20,4 @@ git: # TODO: add tests which explicitly test auto-refresh functionality autoRefresh: false autoFetch: false + autoDetectExternalChanges: false