-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch_helpers.go
More file actions
192 lines (173 loc) · 4.66 KB
/
Copy pathwatch_helpers.go
File metadata and controls
192 lines (173 loc) · 4.66 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package contexting
import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
func syncWatchDirectories(watcher *fsnotify.Watcher, root string, ignored map[string]bool, watched map[string]struct{}) error {
seen := make(map[string]struct{})
err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil {
if errors.Is(err, os.ErrNotExist) {
LogWarnf("Skipping inaccessible path: %s", path)
if d != nil && d.IsDir() {
return filepath.SkipDir
}
return nil
}
return err
}
if !d.IsDir() {
return nil
}
rel, err := filepath.Rel(root, path)
if err != nil {
return err
}
if rel != "." && shouldIgnorePath(rel, d.Name(), ignored) {
return filepath.SkipDir
}
seen[path] = struct{}{}
if _, exists := watched[path]; !exists {
if err := watcher.Add(path); err != nil {
if errors.Is(err, os.ErrNotExist) {
LogWarnf("Skipping broken symlink: %s", path)
return filepath.SkipDir
}
return fmt.Errorf("add watch %s: %w", path, err)
}
watched[path] = struct{}{}
}
return nil
})
if err != nil {
return err
}
for dir := range watched {
if _, exists := seen[dir]; exists {
continue
}
_ = watcher.Remove(dir)
delete(watched, dir)
}
return nil
}
func shouldSkipEvent(root string, event fsnotify.Event, ignored map[string]bool, outputPath string, cachePath string, configPath string) bool {
if shouldSkipInternalOutput(event.Name, outputPath, cachePath, configPath) {
return true
}
rel, err := filepath.Rel(root, event.Name)
if err != nil {
return false
}
if rel == "." {
return false
}
return shouldIgnorePath(rel, filepath.Base(event.Name), ignored)
}
// isInsideProject returns true if path is inside rootPath or equals rootPath.
// Uses slash-normalized comparison so it works cross-platform.
func isInsideProject(path string, root string) bool {
rel, err := filepath.Rel(root, path)
if err != nil {
return false
}
return rel == "." || (!strings.HasPrefix(rel, "..") && rel != "..")
}
func shouldSkipInternalOutput(eventPath string, outputPath string, cachePath string, configPath string) bool {
if eventPath == outputPath || eventPath == cachePath || eventPath == configPath || eventPath == configPath+".example" {
return true
}
base := filepath.Base(eventPath)
if matched, _ := filepath.Match(".tmp-*.json", base); matched {
return true
}
// Doctor temp files (.ctxt/.doctor-*.tmp) are created and immediately removed,
// so they never persist long enough to trigger watch events.
// Additionally, .ctxt/ is a hidden directory and is ignored by default.
return false
}
func logChangeSummary(changes map[string]fsnotify.Op, verbose bool) {
if len(changes) == 0 || !verbose {
return
}
created := 0
modified := 0
removed := 0
renamed := 0
details := make([]string, 0, len(changes))
paths := make([]string, 0, len(changes))
for path := range changes {
paths = append(paths, path)
}
sort.Strings(paths)
for _, path := range paths {
op := changes[path]
if op&fsnotify.Create != 0 {
created++
}
if op&fsnotify.Write != 0 || op&fsnotify.Chmod != 0 {
modified++
}
if op&fsnotify.Remove != 0 {
removed++
}
if op&fsnotify.Rename != 0 {
renamed++
}
details = append(details, fmt.Sprintf("%s (%s)", path, summarizeOp(op)))
}
LogInfof("Filesystem changes: created=%d modified=%d removed=%d renamed=%d", created, modified, removed, renamed)
const maxDetails = 10
if len(details) <= maxDetails {
LogInfof("Changed files: %s", strings.Join(details, ", "))
return
}
LogInfof("Changed files: %s, ... and %d more", strings.Join(details[:maxDetails], ", "), len(details)-maxDetails)
}
func summarizeOp(op fsnotify.Op) string {
parts := make([]string, 0, 4)
if op&fsnotify.Create != 0 {
parts = append(parts, "create")
}
if op&fsnotify.Write != 0 {
parts = append(parts, "write")
}
if op&fsnotify.Remove != 0 {
parts = append(parts, "remove")
}
if op&fsnotify.Rename != 0 {
parts = append(parts, "rename")
}
if op&fsnotify.Chmod != 0 {
parts = append(parts, "chmod")
}
if len(parts) == 0 {
return "unknown"
}
return strings.Join(parts, "|")
}
func parsePersistMode(value string) (WatchPersistMode, error) {
normalized := strings.ToLower(strings.TrimSpace(value))
switch WatchPersistMode(normalized) {
case PersistShutdown:
return PersistShutdown, nil
case PersistInterval:
return PersistInterval, nil
case PersistChange:
return PersistChange, nil
default:
return "", fmt.Errorf("invalid persist mode %q (expected shutdown|interval|change)", value)
}
}
func tickerChan(ticker *time.Ticker) <-chan time.Time {
if ticker == nil {
return nil
}
return ticker.C
}