Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: ignore watch events from backup files #2345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions d2cli/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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, ""
}