cli: ignore watch events from backup files

This commit is contained in:
Alexander Wang 2025-02-11 10:33:59 -07:00
parent 4a73ca9473
commit 0067298fe7
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927

View file

@ -264,6 +264,12 @@ func (w *watcher) watchLoop(ctx context.Context) error {
return errors.New("fsnotify watcher closed") return errors.New("fsnotify watcher closed")
} }
w.ms.Log.Debug.Printf("received file system event %v", ev) 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) mt, err := w.ensureAddWatch(ctx, ev.Name)
if err != nil { if err != nil {
return err 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) { 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) err := w.fw.Add(path)
if err != nil { if err != nil {
return time.Time{}, err return time.Time{}, err
@ -671,3 +682,41 @@ func (tfs *trackedFS) Open(name string) (fs.File, error) {
} }
return f, err 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, ""
}