-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.go
More file actions
218 lines (204 loc) · 6.87 KB
/
inventory.go
File metadata and controls
218 lines (204 loc) · 6.87 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"cmp"
"context"
"fmt"
"path/filepath"
"slices"
"strings"
)
// inventoryCache owns repository facts for a single run. Returned package
// inventories and parsed snapshots alias cached maps and slices, so callers must
// treat them as read-only unless they clone before mutating.
type inventoryCache struct {
cfg config
git gitRunner
validRevisions map[string]struct{}
files map[revisionFileKey]cachedFile
fileLists map[string][]string
packages map[string]packageInventory
}
type revisionFileKey struct {
Revision string
Path string
}
// cachedFile records one path's facts at one revision. Its zero value means
// file existence is unknown. exists is meaningful only when existenceKnown is
// true. parsed implies existenceKnown and exists, and snapshot is valid only
// when parsed is true.
type cachedFile struct {
existenceKnown bool
exists bool
parsed bool
snapshot parsedFileSnapshot
}
func newInventoryCache(cfg config, git gitRunner) *inventoryCache {
return &inventoryCache{
cfg: cfg,
git: git,
validRevisions: map[string]struct{}{},
files: map[revisionFileKey]cachedFile{},
fileLists: map[string][]string{},
packages: map[string]packageInventory{},
}
}
func (cache *inventoryCache) ensureRevisionExists(ctx context.Context, revision string) error {
if _, ok := cache.validRevisions[revision]; ok {
return nil
}
if err := ensureRevisionExists(ctx, cache.cfg, cache.git, revision); err != nil {
return err
}
cache.validRevisions[revision] = struct{}{}
return nil
}
// noteFileExists records a positive file-existence fact discovered by a
// caller. The caller must have already validated that the file exists at the
// revision, typically through a successful git ls-tree directory enumeration.
func (cache *inventoryCache) noteFileExists(revision, filePath string) {
key := revisionFileKey{Revision: revision, Path: cleanGitPath(filePath)}
file := cache.files[key]
file.existenceKnown = true
file.exists = true
cache.files[key] = file
}
// parseFileAtRevision returns a parsed snapshot for an existing file. The
// returned snapshot aliases cache state and must be treated as read-only.
func (cache *inventoryCache) parseFileAtRevision(ctx context.Context, revision, filePath string) (parsedFileSnapshot, bool, error) {
key := revisionFileKey{Revision: revision, Path: cleanGitPath(filePath)}
file := cache.files[key]
if file.parsed {
return file.snapshot, true, nil
}
if err := cache.ensureRevisionExists(ctx, revision); err != nil {
return parsedFileSnapshot{}, false, err
}
if !file.existenceKnown {
exists, err := fileExistsAtRevision(ctx, cache.cfg, cache.git, revision, key.Path)
if err != nil {
return parsedFileSnapshot{}, false, err
}
file.existenceKnown = true
file.exists = exists
cache.files[key] = file
}
if !file.exists {
return parsedFileSnapshot{}, false, nil
}
result, err := cache.git(ctx, cache.cfg.RepoRoot, "show", revision+":"+key.Path)
if err != nil {
return parsedFileSnapshot{}, false, fmt.Errorf("read %s at %s: %w", key.Path, revision, err)
}
parsed, err := parseSnapshotForPath(key.Path, []byte(result.Stdout))
if err != nil {
// Do not cache parse failures. Current callers return immediately, so a
// hypothetical retry may re-read the file from git instead of storing an error.
return parsedFileSnapshot{}, true, fmt.Errorf("parse %s at %s: %w", key.Path, revision, err)
}
file.parsed = true
file.snapshot = parsed
cache.files[key] = file
return parsed, true, nil
}
func (cache *inventoryCache) parseChangeFileAtRevision(ctx context.Context, revision, filePath string) (parsedFileSnapshot, bool, error) {
if filePath == "" || !isRunnableTestFilePath(filePath) {
return parsedFileSnapshot{}, false, nil
}
return cache.parseFileAtRevision(ctx, revision, filePath)
}
// loadPackageInventory returns an inventory whose maps alias cache state. Callers
// must treat the result as read-only or clone maps before mutating.
func (cache *inventoryCache) loadPackageInventory(ctx context.Context, revision string, key packageKey) (packageInventory, error) {
cacheKey := revision + "\x00" + key.Dir + "\x00" + key.Name
if inventory, ok := cache.packages[cacheKey]; ok {
return inventory, nil
}
files, err := cache.listTestFilesInDir(ctx, revision, key.Dir)
if err != nil {
return packageInventory{}, err
}
inventory := packageInventory{
Key: key,
Tests: map[string]struct{}{},
}
for _, filePath := range files {
parsed, exists, err := cache.parseFileAtRevision(ctx, revision, filePath)
if err != nil {
return packageInventory{}, err
}
if !exists || parsed.Snapshot.packageName != key.Name {
continue
}
for testName := range parsed.Snapshot.tests {
inventory.Tests[testName] = struct{}{}
}
}
cache.packages[cacheKey] = inventory
return inventory, nil
}
func (cache *inventoryCache) listTestFilesInDir(ctx context.Context, revision, dir string) ([]string, error) {
cleanDir := filepath.ToSlash(filepath.Clean(dir))
cacheKey := revision + "\x00" + cleanDir
if files, ok := cache.fileLists[cacheKey]; ok {
return files, nil
}
if err := cache.ensureRevisionExists(ctx, revision); err != nil {
return nil, err
}
pathspec := cmp.Or(cleanDir, ".")
result, err := cache.git(ctx, cache.cfg.RepoRoot, "ls-tree", "-r", "-z", "--name-only", revision, "--", pathspec)
if err != nil {
return nil, err
}
files := make([]string, 0)
for part := range strings.SplitSeq(result.Stdout, "\x00") {
if part == "" {
continue
}
filePath := cleanGitPath(part)
if !isRunnableTestFilePath(filePath) {
continue
}
if filepath.ToSlash(filepath.Dir(filePath)) != cleanDir {
continue
}
files = append(files, filePath)
cache.noteFileExists(revision, filePath)
}
slices.Sort(files)
cache.fileLists[cacheKey] = files
return files, nil
}
func (cache *inventoryCache) loadDirectoryInventories(ctx context.Context, revision, dir string) ([]packageInventory, error) {
files, err := cache.listTestFilesInDir(ctx, revision, dir)
if err != nil {
return nil, err
}
packageNames := map[string]struct{}{}
for _, filePath := range files {
parsed, exists, err := cache.parseFileAtRevision(ctx, revision, filePath)
if err != nil {
return nil, err
}
if !exists {
continue
}
packageNames[parsed.Snapshot.packageName] = struct{}{}
}
keys := make([]packageKey, 0, len(packageNames))
for packageName := range packageNames {
keys = append(keys, packageKey{Dir: filepath.ToSlash(filepath.Clean(dir)), Name: packageName})
}
slices.SortFunc(keys, func(left, right packageKey) int {
return cmp.Compare(left.Name, right.Name)
})
inventories := make([]packageInventory, 0, len(keys))
for _, key := range keys {
inventory, err := cache.loadPackageInventory(ctx, revision, key)
if err != nil {
return nil, err
}
inventories = append(inventories, inventory)
}
return inventories, nil
}