Repository navigation
Expand file tree
/
Copy pathstore.go
More file actions
154 lines (140 loc) · 3.61 KB
/
Copy pathstore.go
File metadata and controls
154 lines (140 loc) · 3.61 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
package main
import (
"os"
"path/filepath"
"strings"
"sync"
"time"
)
const (
maxFileSize = 1 << 20 // 1 MiB; larger files are treated as binary
maxCached = 4096 // safety cap on tracked files
)
// snapshot holds the last-known content of one file.
type snapshot struct {
content string
binary bool
}
// fileStore is the baseline content cache: diffs are computed between
// the cached previous content and the current on-disk content.
type fileStore struct {
mu sync.RWMutex
files map[string]snapshot
}
func newFileStore() *fileStore {
return &fileStore{files: make(map[string]snapshot)}
}
func (s *fileStore) get(rel string) (snapshot, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
snap, ok := s.files[rel]
return snap, ok
}
func (s *fileStore) put(rel string, snap snapshot) {
s.mu.Lock()
defer s.mu.Unlock()
s.files[rel] = snap
}
func (s *fileStore) remove(rel string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.files, rel)
}
func (s *fileStore) reset(snaps map[string]snapshot) {
s.mu.Lock()
defer s.mu.Unlock()
s.files = snaps
}
// readFile reads f and reports whether it is binary (non-UTF-8-ish).
// Writers are often mid-write when the fs event fires, so this retries
// briefly until size+mtime are stable, avoiding baselines built on
// partial content.
func readFile(path string) (content string, binary bool, err error) {
const attempts = 3
for i := 0; i < attempts; i++ {
c, b, stable, rerr := readFileOnce(path)
if rerr != nil {
return "", false, rerr
}
if stable || i == attempts-1 {
return c, b, nil
}
time.Sleep(20 * time.Millisecond)
}
return "", false, nil // unreachable
}
func readFileOnce(path string) (content string, binary, stable bool, err error) {
before, err := os.Stat(path)
if err != nil {
return "", false, false, err
}
if !before.Mode().IsRegular() || before.Size() > maxFileSize {
return "", true, true, nil
}
data, err := os.ReadFile(path)
if err != nil {
return "", false, false, err
}
after, err := os.Stat(path)
if err != nil {
return "", false, false, err
}
stable = before.Size() == after.Size() && before.ModTime().Equal(after.ModTime())
if isBinary(data) {
return "", true, stable, nil
}
return string(data), false, stable, nil
}
// isBinary reports whether data looks non-textual (NUL byte in the
// first 8 KiB, the same heuristic grep uses).
func isBinary(data []byte) bool {
n := len(data)
if n > 8192 {
n = 8192
}
for _, b := range data[:n] {
if b == 0 {
return true
}
}
return false
}
// scan walks root and snapshots every file that passes the filters.
func scan(root string, ignores *ignoreSet, respectVCS bool) (map[string]snapshot, error) {
snaps := make(map[string]snapshot)
ign, err := loadIgnorer(root, respectVCS)
if err != nil {
return nil, err
}
err = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil {
return nil // skip unreadable entries
}
rel, rerr := filepath.Rel(root, path)
if rerr != nil {
return nil
}
if d.IsDir() {
if rel != "." && (defaultSkips[d.Name()] || ignores.dirSkippedByInclude(rel) || excludesOnly(ignores, rel) || ign.ignored(rel, true)) {
return filepath.SkipDir
}
return nil
}
if rel == "." || ignores.ignored(rel) || ign.ignored(rel, false) {
return nil
}
if len(snaps) >= maxCached {
return filepath.SkipAll
}
content, binary, rerr := readFile(path)
if rerr == nil {
snaps[rel] = snapshot{content: content, binary: binary}
}
return nil
})
return snaps, err
}
func splitLines(s string) []string {
s = strings.ReplaceAll(s, "\r\n", "\n")
return strings.Split(strings.TrimSuffix(s, "\n"), "\n")
}