watch: move more of the directory-skipping logic into the interface (#1864)

This commit is contained in:
Nick Santos 2019-07-16 16:23:05 -04:00 committed by Nicolas De loof
parent d744c97f13
commit a31350ede1
2 changed files with 11 additions and 24 deletions

View File

@ -49,19 +49,18 @@ type Notify interface {
// - Watch /src/repo, but ignore everything in /src/repo/bazel-bin except /src/repo/bazel-bin/app-binary // - Watch /src/repo, but ignore everything in /src/repo/bazel-bin except /src/repo/bazel-bin/app-binary
// //
// The PathMatcher inteface helps us manage these ignores. // The PathMatcher inteface helps us manage these ignores.
// By design, fileutils.PatternMatcher (the interface that implements dockerignore)
// satisfies this interface
// https://godoc.org/github.com/docker/docker/pkg/fileutils#PatternMatcher
type PathMatcher interface { type PathMatcher interface {
Matches(file string) (bool, error) Matches(file string) (bool, error)
Exclusions() bool
// If this matches the entire dir, we can often optimize filetree walks a bit.
MatchesEntireDir(file string) (bool, error)
} }
type EmptyMatcher struct { type EmptyMatcher struct {
} }
func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil } func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil }
func (EmptyMatcher) Exclusions() bool { return false } func (EmptyMatcher) MatchesEntireDir(f string) (bool, error) { return false, nil }
var _ PathMatcher = EmptyMatcher{} var _ PathMatcher = EmptyMatcher{}

View File

@ -207,28 +207,16 @@ func (d *naiveNotify) shouldNotify(path string) bool {
} }
func (d *naiveNotify) shouldSkipDir(path string) (bool, error) { func (d *naiveNotify) shouldSkipDir(path string) (bool, error) {
var err error
ignore := false
// If path is directly in the notifyList, we should always watch it. // If path is directly in the notifyList, we should always watch it.
if !d.notifyList[path] { if d.notifyList[path] {
ignore, err = d.ignore.Matches(path) return false, nil
if err != nil {
return false, errors.Wrapf(err, "Error matching %s: %v", path, err)
}
} }
// The ignore filter is telling us to ignore this file, skip, err := d.ignore.MatchesEntireDir(path)
// but we may have to watch it anyway to catch files underneath it. if err != nil {
if ignore { return false, errors.Wrap(err, "shouldSkipDir")
if !d.ignore.Exclusions() {
return true, nil
}
// TODO(nick): Add more complex logic for interpreting exclusion patterns.
} }
return skip, nil
return false, nil
} }
func (d *naiveNotify) add(path string) error { func (d *naiveNotify) add(path string) error {