Repository navigation
Expand file tree
/
Copy pathdiscovery.go
More file actions
303 lines (274 loc) · 8.34 KB
/
Copy pathdiscovery.go
File metadata and controls
303 lines (274 loc) · 8.34 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package manifests
import (
"errors"
"fmt"
"io/fs"
"path"
"sort"
"strings"
"github.com/bmatcuk/doublestar/v4"
)
// RepositoryReader provides bounded access to files in a repository. Paths and
// glob patterns use forward slashes and are relative to the repository root.
// Glob must support the doublestar dialect, including recursive ** patterns.
// ReadFile should return an error matching fs.ErrNotExist for absent files.
type RepositoryReader interface {
ReadFile(name string) ([]byte, error)
Glob(pattern string) ([]string, error)
}
// FSReader adapts an fs.FS to RepositoryReader. It supports recursive ** globs.
type FSReader struct {
fsys fs.FS
}
// NewFSReader returns a repository reader rooted at fsys. Use os.DirFS to
// discover manifests in a working tree.
func NewFSReader(fsys fs.FS) *FSReader {
return &FSReader{fsys: fsys}
}
// ReadFile reads a repository-relative file.
func (r *FSReader) ReadFile(name string) ([]byte, error) {
if r == nil || r.fsys == nil {
return nil, errors.New("nil repository filesystem")
}
return fs.ReadFile(r.fsys, name)
}
// Glob returns files matching a repository-relative doublestar pattern.
func (r *FSReader) Glob(pattern string) ([]string, error) {
if r == nil || r.fsys == nil {
return nil, errors.New("nil repository filesystem")
}
return doublestar.Glob(r.fsys, pattern,
doublestar.WithFilesOnly(),
doublestar.WithFailOnIOErrors(),
)
}
// DiscoveredManifest identifies a project or workspace manifest. ParentPath is
// the repository-relative workspace configuration that selected a nested
// manifest; it is empty for root and path-based manifests.
type DiscoveredManifest struct {
Path string
Ecosystem string
Kind Kind
ParentPath string
}
type manifestDiscovery struct {
reader RepositoryReader
items map[discoveredManifestKey]DiscoveredManifest
warnings []error
}
func (d *manifestDiscovery) warn(err error) {
d.warnings = append(d.warnings, err)
}
type discoveredManifestKey struct {
path string
ecosystem string
kind Kind
}
// DiscoverManifests returns the project and workspace manifests selected from
// a rooted repository. It does not parse dependency declarations. Warnings
// report workspace configurations or lookups that could not be processed;
// successfully discovered manifests are still returned.
func DiscoverManifests(reader RepositoryReader) ([]DiscoveredManifest, []error) {
if reader == nil {
return nil, []error{errors.New("nil repository reader")}
}
discovery := &manifestDiscovery{
reader: reader,
items: make(map[discoveredManifestKey]DiscoveredManifest),
}
for _, pattern := range []string{
"*", ".github/workflows/*.yml", ".github/workflows/*.yaml",
".cargo/config.toml", ".cargo/config", ".bundle/config",
} {
if err := discovery.addMatches(pattern, ""); err != nil {
discovery.warn(fmt.Errorf("discovering manifests matching %q: %w", pattern, err))
}
}
workspaceDiscoveries := []func() error{
discovery.discoverCargoWorkspace,
discovery.discoverGoWorkspace,
discovery.discoverNPMWorkspace,
discovery.discoverPnpmWorkspace,
}
for _, discover := range workspaceDiscoveries {
if err := discover(); err != nil {
discovery.warn(err)
}
}
return discovery.sorted(), discovery.warnings
}
func (d *manifestDiscovery) addMatches(pattern, parentPath string) error {
matches, err := d.reader.Glob(pattern)
if err != nil {
return err
}
sort.Strings(matches)
for _, match := range matches {
d.add(match, parentPath)
}
return nil
}
func (d *manifestDiscovery) add(manifestPath, parentPath string) {
manifestPath, ok := normalizeRepositoryPath(manifestPath)
if !ok {
return
}
matches := IdentifyAll(manifestPath)
if len(matches) == 0 {
return
}
if parentPath != "" {
var valid bool
parentPath, valid = normalizeRepositoryPath(parentPath)
if !valid {
return
}
}
for _, match := range matches {
key := discoveredManifestKey{path: manifestPath, ecosystem: match.Ecosystem, kind: match.Kind}
item := DiscoveredManifest{
Path: manifestPath,
Ecosystem: match.Ecosystem,
Kind: match.Kind,
ParentPath: parentPath,
}
if current, exists := d.items[key]; !exists || current.ParentPath == "" && parentPath != "" {
d.items[key] = item
}
}
}
func (d *manifestDiscovery) addWorkspaceManifests(
includePatterns, excludePatterns []string,
manifestName, parentPath string,
) error {
excluded, err := d.workspaceManifestPaths(excludePatterns, manifestName)
if err != nil {
return err
}
included := make(map[string]struct{})
for _, pattern := range includePatterns {
normalized, ok := normalizeRepositoryPattern(pattern)
if !ok {
continue
}
matches, err := d.reader.Glob(path.Join(normalized, manifestName))
if err != nil {
return fmt.Errorf("expanding workspace pattern %q: %w", pattern, err)
}
if len(matches) == 0 && isLiteralPattern(normalized) && !patternCovers(excludePatterns, normalized) {
d.warn(fmt.Errorf("%s: workspace member %q has no %s", parentPath, pattern, manifestName))
}
for _, match := range matches {
if p, valid := normalizeRepositoryPath(match); valid {
included[p] = struct{}{}
}
}
}
paths := make([]string, 0, len(included))
for manifestPath := range included {
if _, skip := excluded[manifestPath]; !skip {
paths = append(paths, manifestPath)
}
}
sort.Strings(paths)
for _, manifestPath := range paths {
d.add(manifestPath, parentPath)
}
return nil
}
// isLiteralPattern reports whether a workspace member pattern names a
// single directory rather than a glob. Wildcard entries that expand to
// nothing are not treated as missing.
func isLiteralPattern(pattern string) bool {
return !strings.ContainsAny(pattern, "*?[{")
}
// patternCovers reports whether name matches any of the given patterns.
// Used to suppress a missing-member warning when the same entry is also
// excluded.
func patternCovers(patterns []string, name string) bool {
for _, p := range patterns {
p, ok := normalizeRepositoryPattern(p)
if !ok {
continue
}
if ok, _ := doublestar.PathMatch(p, name); ok {
return true
}
}
return false
}
func (d *manifestDiscovery) workspaceManifestPaths(patterns []string, manifestName string) (map[string]struct{}, error) {
result := make(map[string]struct{})
for _, pattern := range patterns {
pattern, ok := normalizeRepositoryPattern(pattern)
if !ok {
continue
}
matches, err := d.reader.Glob(path.Join(pattern, manifestName))
if err != nil {
return nil, fmt.Errorf("expanding workspace pattern %q: %w", pattern, err)
}
for _, match := range matches {
if normalized, valid := normalizeRepositoryPath(match); valid {
result[normalized] = struct{}{}
}
}
}
return result, nil
}
func (d *manifestDiscovery) readOptional(name string) ([]byte, bool, error) {
content, err := d.reader.ReadFile(name)
if errors.Is(err, fs.ErrNotExist) {
return nil, false, nil
}
if err != nil {
return nil, false, err
}
return content, true, nil
}
func (d *manifestDiscovery) sorted() []DiscoveredManifest {
result := make([]DiscoveredManifest, 0, len(d.items))
for _, item := range d.items {
result = append(result, item)
}
sort.Slice(result, func(i, j int) bool {
if result[i].Path != result[j].Path {
return result[i].Path < result[j].Path
}
if result[i].Ecosystem != result[j].Ecosystem {
return result[i].Ecosystem < result[j].Ecosystem
}
return result[i].Kind < result[j].Kind
})
return result
}
func normalizeRepositoryPath(value string) (string, bool) {
value = strings.TrimSpace(strings.ReplaceAll(value, `\`, "/"))
if value == "" || strings.HasPrefix(value, "/") || hasWindowsVolume(value) {
return "", false
}
value = path.Clean(value)
if value == "." || value == ".." || strings.HasPrefix(value, "../") {
return "", false
}
return value, true
}
// hasWindowsVolume reports whether s begins with a Windows drive
// letter (e.g. "C:/"). Such paths are absolute on Windows and outside
// the repository reader's root, so they are rejected alongside
// "/"-prefixed and "../" paths.
func hasWindowsVolume(s string) bool {
if len(s) < 2 || s[1] != ':' {
return false
}
c := s[0]
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
}
func normalizeRepositoryPattern(value string) (string, bool) {
value = strings.TrimSpace(strings.ReplaceAll(value, `\`, "/"))
value = strings.TrimSuffix(value, "/")
if value == "." {
return value, true
}
return normalizeRepositoryPath(value)
}