diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-08-02 20:53:24 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-08-02 21:50:44 +1000 |
commit | 92f0aa23cc600acc0ab1bf61339912c750a26418 (patch) | |
tree | 649fba5b1f600e0f2a2a13c1938b0de0b97f03dc /pkg | |
parent | c8520fbe78453b7c593a1071a3cdc3295ad47772 (diff) |
Remove file watcher code
Now that we refresh upon focus, we can scrap this file watching code.
Stefan says few git UIs use file watching, and I understand why: the
reason this code was problematic in the first place is that watching
files is expensive and if you have too many open file handles that
can cause problems.
Importantly: this code that's being removed was _already_ dead.
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/controllers.go | 1 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/refresh_helper.go | 7 | ||||
-rw-r--r-- | pkg/gui/file_watching.go | 137 | ||||
-rw-r--r-- | pkg/gui/gui.go | 7 | ||||
-rw-r--r-- | pkg/gui/types/common.go | 4 |
5 files changed, 0 insertions, 156 deletions
diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go index 9d76349c7..cc45d833a 100644 --- a/pkg/gui/controllers.go +++ b/pkg/gui/controllers.go @@ -56,7 +56,6 @@ func (gui *Gui) resetHelpersAndControllers() { stagingHelper, mergeConflictsHelper, worktreeHelper, - gui.fileWatcher, ) diffHelper := helpers.NewDiffHelper(helperCommon) cherryPickHelper := helpers.NewCherryPickHelper( diff --git a/pkg/gui/controllers/helpers/refresh_helper.go b/pkg/gui/controllers/helpers/refresh_helper.go index bcff2d627..992a7a161 100644 --- a/pkg/gui/controllers/helpers/refresh_helper.go +++ b/pkg/gui/controllers/helpers/refresh_helper.go @@ -28,7 +28,6 @@ type RefreshHelper struct { stagingHelper *StagingHelper mergeConflictsHelper *MergeConflictsHelper worktreeHelper *WorktreeHelper - fileWatcher types.IFileWatcher } func NewRefreshHelper( @@ -39,7 +38,6 @@ func NewRefreshHelper( stagingHelper *StagingHelper, mergeConflictsHelper *MergeConflictsHelper, worktreeHelper *WorktreeHelper, - fileWatcher types.IFileWatcher, ) *RefreshHelper { return &RefreshHelper{ c: c, @@ -49,7 +47,6 @@ func NewRefreshHelper( stagingHelper: stagingHelper, mergeConflictsHelper: mergeConflictsHelper, worktreeHelper: worktreeHelper, - fileWatcher: fileWatcher, } } @@ -548,10 +545,6 @@ func (self *RefreshHelper) refreshStateFiles() error { fileTreeViewModel.SetTree() fileTreeViewModel.RWMutex.Unlock() - if err := self.fileWatcher.AddFilesToFileWatcher(files); err != nil { - return err - } - return nil } diff --git a/pkg/gui/file_watching.go b/pkg/gui/file_watching.go deleted file mode 100644 index 2c8addf8f..000000000 --- a/pkg/gui/file_watching.go +++ /dev/null @@ -1,137 +0,0 @@ -package gui - -import ( - "os" - "path/filepath" - - "github.com/fsnotify/fsnotify" - "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/utils" - "github.com/sirupsen/logrus" -) - -// macs for some bizarre reason cap the number of watchable files to 256. -// there's no obvious platform agnostic way to check the situation of the user's -// computer so we're just arbitrarily capping at 200. This isn't so bad because -// file watching is only really an added bonus for faster refreshing. -const MAX_WATCHED_FILES = 50 - -var _ types.IFileWatcher = new(fileWatcher) - -type fileWatcher struct { - Watcher *fsnotify.Watcher - WatchedFilenames []string - Log *logrus.Entry - Disabled bool -} - -func NewFileWatcher(log *logrus.Entry) *fileWatcher { - // TODO: get this going again, and ensure we don't see any crashes from it - return &fileWatcher{ - Disabled: true, - } -} - -func (w *fileWatcher) watchingFilename(filename string) bool { - for _, watchedFilename := range w.WatchedFilenames { - if watchedFilename == filename { - return true - } - } - return false -} - -func (w *fileWatcher) popOldestFilename() { - // shift the last off the array to make way for this one - oldestFilename := w.WatchedFilenames[0] - w.WatchedFilenames = w.WatchedFilenames[1:] - if err := w.Watcher.Remove(oldestFilename); err != nil { - // swallowing errors here because it doesn't really matter if we can't unwatch a file - w.Log.Error(err) - } -} - -func (w *fileWatcher) watchFilename(filename string) { - if err := w.Watcher.Add(filename); err != nil { - // swallowing errors here because it doesn't really matter if we can't watch a file - w.Log.Error(err) - } - - // assume we're watching it now to be safe - w.WatchedFilenames = append(w.WatchedFilenames, filename) -} - -func (w *fileWatcher) AddFilesToFileWatcher(files []*models.File) error { - if w.Disabled { - return nil - } - - if len(files) == 0 { - return nil - } - - // watch the files for changes - dirName, err := os.Getwd() - if err != nil { - return err - } - - for _, file := range files[0:min(MAX_WATCHED_FILES, len(files))] { - if file.Deleted { - continue - } - filename := filepath.Join(dirName, file.Name) - if w.watchingFilename(filename) { - continue - } - if len(w.WatchedFilenames) > MAX_WATCHED_FILES { - w.popOldestFilename() - } - - w.watchFilename(filename) - } - - return nil -} - -func min(a int, b int) int { - if a < b { - return a - } - return b -} - -// NOTE: given that we often edit files ourselves, this may make us end up refreshing files too often -// TODO: consider watching the whole directory recursively (could be more expensive) -func (gui *Gui) WatchFilesForChanges() { - gui.fileWatcher = NewFileWatcher(gui.Log) - if gui.fileWatcher.Disabled { - return - } - go utils.Safe(func() { - for { - select { - // watch for events - case event := <-gui.fileWatcher.Watcher.Events: - if event.Op == fsnotify.Chmod { - // for some reason we pick up chmod events when they don't actually happen - continue - } - // only refresh if we're not already - if !gui.IsRefreshingFiles { - gui.c.OnUIThread(func() error { - // TODO: find out if refresh needs to be run on the UI thread - return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.FILES}}) - }) - } - - // watch for errors - case err := <-gui.fileWatcher.Watcher.Errors: - if err != nil { - gui.c.Log.Error(err) - } - } - } - }) -} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index ad3dc3e34..4be909dc8 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -72,7 +72,6 @@ type Gui struct { Updater *updates.Updater statusManager *status.StatusManager waitForIntro sync.WaitGroup - fileWatcher *fileWatcher viewBufferManagerMap map[string]*tasks.ViewBufferManager // holds a mapping of view names to ptmx's. This is for rendering command outputs // from within a pty. The point of keeping track of them is so that if we re-size @@ -476,8 +475,6 @@ func NewGui( afterLayoutFuncs: make(chan func() error, 1000), } - gui.WatchFilesForChanges() - gui.PopupHandler = popup.NewPopupHandler( cmn, func(ctx goContext.Context, opts types.CreatePopupPanelOpts) error { @@ -680,10 +677,6 @@ func (gui *Gui) RunAndHandleError(startArgs appTypes.StartArgs) error { manager.Close() } - if !gui.fileWatcher.Disabled { - gui.fileWatcher.Watcher.Close() - } - close(gui.stopChan) switch err { diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index 111a2a420..919c15b0e 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -282,10 +282,6 @@ const ( COMPLETE ) -type IFileWatcher interface { - AddFilesToFileWatcher(files []*models.File) error -} - // screen sizing determines how much space your selected window takes up (window // as in panel, not your terminal's window). Sometimes you want a bit more space // to see the contents of a panel, and this keeps track of how much maximisation |