Skip to content

Commit 3050303

Browse files
committed
Detect external ref changes via background polling
Add a 2-second background poll that calls Status.RefsSnapshot and compares against the snapshot stored at the end of the last refs- touching refresh. On a diff, trigger a full refresh — same scope as the focus-in handler, because once we know something changed externally we can't be sure what (an agent might have created a worktree or stashed something alongside the commit we detected). Refresh runs in SYNC mode because goEvery already serializes iterations via <-done: a slow refresh delays the next tick naturally instead of letting work stack. The post-refresh hook from the previous commit updates the snapshot, so in-app commands don't cause the next poll to spuriously re-fire. Disabled in the integration test config, like autoRefresh and autoFetch, because demo replays make repo changes throughout the run; at 2-second cadence the resulting full refreshes compete with the demo's own choreography and push some demos past their 40-second timeout. Also list the two new config keys in checkForChangedConfigsThatDontAutoReload so a config edit warns the user that lazygit needs a restart.
1 parent c1eeacd commit 3050303

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

pkg/gui/background.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ func (self *BackgroundRoutineMgr) startBackgroundRoutines() {
6262
}
6363
}
6464

65+
if userConfig.Git.AutoDetectExternalChanges {
66+
interval := userConfig.Refresher.ExternalChangeCheckInterval
67+
if interval > 0 {
68+
go utils.Safe(self.startBackgroundExternalChangeDetection)
69+
} else {
70+
self.gui.c.Log.Errorf(
71+
"Value of config option 'refresher.externalChangeCheckInterval' (%d) is invalid, disabling external change detection",
72+
interval)
73+
}
74+
}
75+
6576
if self.gui.Config.GetDebug() {
6677
self.goEvery(time.Second*time.Duration(10), self.gui.stopChan, func(_ bool) error {
6778
formatBytes := func(b uint64) string {
@@ -127,6 +138,55 @@ func (self *BackgroundRoutineMgr) startBackgroundFilesRefresh() {
127138
})
128139
}
129140

141+
func (self *BackgroundRoutineMgr) startBackgroundExternalChangeDetection() {
142+
self.gui.waitForIntro.Wait()
143+
144+
// We don't seed the snapshot here. The startup refresh captures one on
145+
// entry (like every refs-touching refresh), and until one has been
146+
// captured RefsSnapshotChangedSince treats the empty baseline as
147+
// "unchanged", so we never fire a spurious refresh before a baseline
148+
// exists — no need to depend on the timing of that startup refresh.
149+
150+
userConfig := self.gui.UserConfig()
151+
self.goEvery(
152+
userConfig.Refresher.ExternalChangeCheckIntervalDuration(),
153+
self.gui.stopChan,
154+
func(_ bool) error {
155+
self.checkForExternalChanges()
156+
return nil
157+
},
158+
)
159+
}
160+
161+
func (self *BackgroundRoutineMgr) checkForExternalChanges() {
162+
current, err := self.gui.git.Status.RefsSnapshot()
163+
if err != nil {
164+
// Transient error (e.g. git process couldn't start). Don't update the
165+
// stored snapshot; we'll retry next tick.
166+
self.gui.c.Log.Warnf("RefsSnapshot failed: %v", err)
167+
return
168+
}
169+
170+
if !self.gui.helpers.Refresh.RefsSnapshotChangedSince(current) {
171+
return
172+
}
173+
174+
// goEvery checks the pause count before starting us, but a git operation
175+
// may have begun (and paused refreshes) after that check, while we were
176+
// reading the snapshot above. In that case the change we detected is the
177+
// operation's own intermediate state, so back off: the operation will
178+
// refresh and re-snapshot when it finishes, and if the change was really
179+
// external we'll catch it on the next tick after the pause lifts. We don't
180+
// update the stored snapshot, so nothing is swallowed.
181+
if self.backgroundRefreshesPaused() {
182+
return
183+
}
184+
185+
// No need to update the stored snapshot here; Refresh does that.
186+
self.gui.c.Log.Info("External ref change detected — refreshing")
187+
self.gui.c.Refresh(types.RefreshOptions{})
188+
}
189+
130190
// returns a channel that can be used to trigger the callback immediately
131191
func (self *BackgroundRoutineMgr) goEvery(interval time.Duration, stop chan struct{}, function func(bool) error) chan struct{} {
132192
done := make(chan struct{})

pkg/gui/gui.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,10 @@ func (gui *Gui) checkForChangedConfigsThatDontAutoReload(oldConfig *config.UserC
515515
configsThatDontAutoReload := []string{
516516
"Git.AutoFetch",
517517
"Git.AutoRefresh",
518+
"Git.AutoDetectExternalChanges",
518519
"Refresher.RefreshInterval",
519520
"Refresher.FetchInterval",
521+
"Refresher.ExternalChangeCheckInterval",
520522
"Update.Method",
521523
"Update.Days",
522524
}

test/default_test_config/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ git:
2020
# TODO: add tests which explicitly test auto-refresh functionality
2121
autoRefresh: false
2222
autoFetch: false
23+
autoDetectExternalChanges: false

0 commit comments

Comments
 (0)