-
Notifications
You must be signed in to change notification settings - Fork 288
/
notify.go
92 lines (72 loc) · 2.08 KB
/
notify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package watch
import (
"expvar"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/tilt-dev/tilt/pkg/logger"
)
var (
numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
)
type FileEvent struct {
path string
}
func NewFileEvent(p string) FileEvent {
if !filepath.IsAbs(p) {
panic(fmt.Sprintf("NewFileEvent only accepts absolute paths. Actual: %s", p))
}
return FileEvent{path: p}
}
func (e FileEvent) Path() string {
return e.path
}
type Notify interface {
// Start watching the paths set at init time
Start() error
// Stop watching and close all channels
Close() error
// A channel to read off incoming file changes
Events() chan FileEvent
// A channel to read off show-stopping errors
Errors() chan error
}
// When we specify directories to watch, we often want to
// ignore some subset of the files under those directories.
//
// For example:
// - Watch /src/repo, but ignore /src/repo/.git
// - 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.
type PathMatcher interface {
Matches(file string) (bool, error)
// 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) MatchesEntireDir(f string) (bool, error) { return false, nil }
var _ PathMatcher = EmptyMatcher{}
func NewWatcher(paths []string, ignore PathMatcher, l logger.Logger) (Notify, error) {
return newWatcher(paths, ignore, l)
}
const WindowsBufferSizeEnvVar = "TILT_WATCH_WINDOWS_BUFFER_SIZE"
const defaultBufferSize int = 65536
func DesiredWindowsBufferSize() int {
envVar := os.Getenv(WindowsBufferSizeEnvVar)
if envVar != "" {
size, err := strconv.Atoi(envVar)
if err == nil {
return size
}
}
return defaultBufferSize
}
func IsWindowsShortReadError(err error) bool {
return runtime.GOOS == "windows" && err != nil && strings.Contains(err.Error(), "short read")
}