From 0067298fe7466d56623d8183ac15b07ff67cd038 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 11 Feb 2025 10:33:59 -0700 Subject: [PATCH] cli: ignore watch events from backup files --- d2cli/watch.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/d2cli/watch.go b/d2cli/watch.go index 6c240a711..f34dd0a1a 100644 --- a/d2cli/watch.go +++ b/d2cli/watch.go @@ -264,6 +264,12 @@ func (w *watcher) watchLoop(ctx context.Context) error { return errors.New("fsnotify watcher closed") } w.ms.Log.Debug.Printf("received file system event %v", ev) + + if isTemp, reason := isBackupFile(ev.Name); isTemp { + w.ms.Log.Debug.Printf("skipping event for %q: detected as %s", w.ms.HumanPath(ev.Name), reason) + continue + } + mt, err := w.ensureAddWatch(ctx, ev.Name) if err != nil { return err @@ -349,6 +355,11 @@ func (w *watcher) ensureAddWatch(ctx context.Context, path string) (time.Time, e } func (w *watcher) addWatch(ctx context.Context, path string) (time.Time, error) { + if isTemp, reason := isBackupFile(path); isTemp { + w.ms.Log.Debug.Printf("skipping watch for %q: detected as %s", w.ms.HumanPath(path), reason) + return time.Time{}, nil + } + err := w.fw.Add(path) if err != nil { return time.Time{}, err @@ -671,3 +682,41 @@ func (tfs *trackedFS) Open(name string) (fs.File, error) { } return f, err } + +func isBackupFile(path string) (bool, string) { + ext := filepath.Ext(path) + baseName := filepath.Base(path) + + // This list is based off of https://github.com/gohugoio/hugo/blob/master/commands/hugobuilder.go#L795 + switch { + case strings.HasSuffix(ext, "~"): + return true, "generic backup file (~)" + case ext == ".swp": + return true, "vim swap file" + case ext == ".swx": + return true, "vim swap file" + case ext == ".tmp": + return true, "generic temp file" + case ext == ".DS_Store": + return true, "OSX thumbnail" + case ext == ".bck": + return true, "Helix backup" + case baseName == "4913": + return true, "vim temp file" + case strings.HasPrefix(ext, ".goutputstream"): + return true, "GNOME temp file" + case strings.HasSuffix(ext, "jb_old___"): + return true, "IntelliJ old backup" + case strings.HasSuffix(ext, "jb_tmp___"): + return true, "IntelliJ temp file" + case strings.HasSuffix(ext, "jb_bak___"): + return true, "IntelliJ backup" + case strings.HasPrefix(ext, ".sb-"): + return true, "Byword temp file" + case strings.HasPrefix(baseName, ".#"): + return true, "Emacs lock file" + case strings.HasPrefix(baseName, "#"): + return true, "Emacs temp file" + } + return false, "" +}