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

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
//
// 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 {
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 {
}
func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil }
func (EmptyMatcher) Exclusions() bool { return false }
func (EmptyMatcher) Matches(f string) (bool, error) { return false, nil }
func (EmptyMatcher) MatchesEntireDir(f string) (bool, error) { return false, nil }
var _ PathMatcher = EmptyMatcher{}

View File

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