-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
package_deps_hash.go
456 lines (389 loc) · 16.3 KB
/
package_deps_hash.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package hashing
import (
"bufio"
"fmt"
"io"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/pkg/errors"
gitignore "github.com/sabhiram/go-gitignore"
"github.com/vercel/turbo/cli/internal/doublestar"
"github.com/vercel/turbo/cli/internal/encoding/gitoutput"
"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/globby"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/util"
)
// PackageDepsOptions are parameters for getting git hashes for a filesystem
type PackageDepsOptions struct {
// PackagePath is the folder path to derive the package dependencies from. This is typically the folder
// containing package.json. If omitted, the default value is the current working directory.
PackagePath turbopath.AnchoredSystemPath
InputPatterns []string
}
func safeCompileIgnoreFile(filepath turbopath.AbsoluteSystemPath) (*gitignore.GitIgnore, error) {
if filepath.FileExists() {
return gitignore.CompileIgnoreFile(filepath.ToString())
}
// no op
return gitignore.CompileIgnoreLines([]string{}...), nil
}
func getPackageFileHashesFromProcessingGitIgnore(rootPath turbopath.AbsoluteSystemPath, packagePath turbopath.AnchoredSystemPath, inputs []string) (map[turbopath.AnchoredUnixPath]string, error) {
result := make(map[turbopath.AnchoredUnixPath]string)
absolutePackagePath := packagePath.RestoreAnchor(rootPath)
// Instead of implementing all gitignore properly, we hack it. We only respect .gitignore in the root and in
// the directory of a package.
ignore, err := safeCompileIgnoreFile(rootPath.UntypedJoin(".gitignore"))
if err != nil {
return nil, err
}
ignorePkg, err := safeCompileIgnoreFile(absolutePackagePath.UntypedJoin(".gitignore"))
if err != nil {
return nil, err
}
includePattern := ""
excludePattern := ""
if len(inputs) > 0 {
var includePatterns []string
var excludePatterns []string
for _, pattern := range inputs {
if len(pattern) > 0 && pattern[0] == '!' {
excludePatterns = append(excludePatterns, absolutePackagePath.UntypedJoin(pattern[1:]).ToString())
} else {
includePatterns = append(includePatterns, absolutePackagePath.UntypedJoin(pattern).ToString())
}
}
if len(includePatterns) > 0 {
includePattern = "{" + strings.Join(includePatterns, ",") + "}"
}
if len(excludePatterns) > 0 {
excludePattern = "{" + strings.Join(excludePatterns, ",") + "}"
}
}
err = fs.Walk(absolutePackagePath.ToStringDuringMigration(), func(name string, isDir bool) error {
convertedName := turbopath.AbsoluteSystemPathFromUpstream(name)
rootMatch := ignore.MatchesPath(convertedName.ToString())
otherMatch := ignorePkg.MatchesPath(convertedName.ToString())
if !rootMatch && !otherMatch {
if !isDir {
if includePattern != "" {
val, err := doublestar.PathMatch(includePattern, convertedName.ToString())
if err != nil {
return err
}
if !val {
return nil
}
}
if excludePattern != "" {
val, err := doublestar.PathMatch(excludePattern, convertedName.ToString())
if err != nil {
return err
}
if val {
return nil
}
}
hash, err := fs.GitLikeHashFile(convertedName)
if err != nil {
return fmt.Errorf("could not hash file %v. \n%w", convertedName.ToString(), err)
}
relativePath, err := convertedName.RelativeTo(absolutePackagePath)
if err != nil {
return fmt.Errorf("File path cannot be made relative: %w", err)
}
result[relativePath.ToUnixPath()] = hash
}
}
return nil
})
if err != nil {
return nil, err
}
return result, nil
}
func getPackageFileHashesFromInputs(rootPath turbopath.AbsoluteSystemPath, packagePath turbopath.AnchoredSystemPath, inputs []string) (map[turbopath.AnchoredUnixPath]string, error) {
absolutePackagePath := packagePath.RestoreAnchor(rootPath)
// Add all the checked in hashes.
// make a copy of the inputPatterns array, because we may be appending to it later.
calculatedInputs := make([]string, len(inputs))
copy(calculatedInputs, inputs)
// Add in package.json and turbo.json to input patterns. Both file paths are relative to pkgPath
//
// - package.json is an input because if the `scripts` in
// the package.json change (i.e. the tasks that turbo executes), we want
// a cache miss, since any existing cache could be invalid.
// - turbo.json because it's the definition of the tasks themselves. The root turbo.json
// is similarly included in the global hash. This file may not exist in the workspace, but
// that is ok, because it will get ignored downstream.
calculatedInputs = append(calculatedInputs, "package.json")
calculatedInputs = append(calculatedInputs, "turbo.json")
// The input patterns are relative to the package.
// However, we need to change the globbing to be relative to the repo root.
// Prepend the package path to each of the input patterns.
prefixedInputPatterns := []string{}
prefixedExcludePatterns := []string{}
for _, pattern := range calculatedInputs {
if len(pattern) > 0 && pattern[0] == '!' {
rerooted, err := rootPath.PathTo(absolutePackagePath.UntypedJoin(pattern[1:]))
if err != nil {
return nil, err
}
prefixedExcludePatterns = append(prefixedExcludePatterns, rerooted)
} else {
rerooted, err := rootPath.PathTo(absolutePackagePath.UntypedJoin(pattern))
if err != nil {
return nil, err
}
prefixedInputPatterns = append(prefixedInputPatterns, rerooted)
}
}
absoluteFilesToHash, err := globby.GlobFiles(rootPath.ToStringDuringMigration(), prefixedInputPatterns, prefixedExcludePatterns)
if err != nil {
return nil, errors.Wrapf(err, "failed to resolve input globs %v", calculatedInputs)
}
filesToHash := make([]turbopath.AnchoredSystemPath, len(absoluteFilesToHash))
for i, rawPath := range absoluteFilesToHash {
relativePathString, err := absolutePackagePath.RelativePathString(rawPath)
if err != nil {
return nil, errors.Wrapf(err, "not relative to package: %v", rawPath)
}
filesToHash[i] = turbopath.AnchoredSystemPathFromUpstream(relativePathString)
}
// Note that in this scenario, we don't need to check git status.
// We're hashing the current state, not state at a commit.
result, err := GetHashesForFiles(absolutePackagePath, filesToHash)
if err != nil {
return nil, errors.Wrap(err, "failed hashing resolved inputs globs")
}
return result, nil
}
// GetPackageFileHashes Builds an object containing git hashes for the files under the specified `packagePath` folder.
func GetPackageFileHashes(rootPath turbopath.AbsoluteSystemPath, packagePath turbopath.AnchoredSystemPath, inputs []string) (map[turbopath.AnchoredUnixPath]string, error) {
if len(inputs) == 0 {
result, err := getPackageFileHashesFromGitIndex(rootPath, packagePath)
if err != nil {
return getPackageFileHashesFromProcessingGitIgnore(rootPath, packagePath, nil)
}
return result, nil
}
result, err := getPackageFileHashesFromInputs(rootPath, packagePath, inputs)
if err != nil {
return getPackageFileHashesFromProcessingGitIgnore(rootPath, packagePath, inputs)
}
return result, nil
}
// GetHashesForFiles hashes the list of given files, then returns a map of normalized path to hash.
// This map is suitable for cross-platform caching.
func GetHashesForFiles(rootPath turbopath.AbsoluteSystemPath, files []turbopath.AnchoredSystemPath) (map[turbopath.AnchoredUnixPath]string, error) {
// Try to use `git` first.
gitHashedFiles, err := gitHashObject(rootPath, files)
if err == nil {
return gitHashedFiles, nil
}
// Fall back to manual hashing.
return manuallyHashFiles(rootPath, files)
}
// gitHashObject returns a map of paths to their SHA hashes calculated by passing the paths to `git hash-object`.
// `git hash-object` expects paths to use Unix separators, even on Windows.
//
// Note: paths of files to hash passed to `git hash-object` are processed as relative to the given anchor.
// For that reason we convert all input paths and make them relative to the anchor prior to passing them
// to `git hash-object`.
func gitHashObject(anchor turbopath.AbsoluteSystemPath, filesToHash []turbopath.AnchoredSystemPath) (map[turbopath.AnchoredUnixPath]string, error) {
fileCount := len(filesToHash)
output := make(map[turbopath.AnchoredUnixPath]string, fileCount)
if fileCount > 0 {
cmd := exec.Command(
"git", // Using `git` from $PATH,
"hash-object", // hash a file,
"--stdin-paths", // using a list of newline-separated paths from stdin.
)
cmd.Dir = anchor.ToString() // Start at this directory.
// The functionality for gitHashObject is different enough that it isn't reasonable to
// generalize the behavior for `runGitCmd`. In fact, it doesn't even use the `gitoutput`
// encoding library, instead relying on its own separate `bufio.Scanner`.
// We're going to send the list of files in via `stdin`, so we grab that pipe.
// This prevents a huge number of encoding issues and shell compatibility issues
// before they even start.
stdinPipe, stdinPipeError := cmd.StdinPipe()
if stdinPipeError != nil {
return nil, stdinPipeError
}
// Kick the processing off in a goroutine so while that is doing its thing we can go ahead
// and wire up the consumer of `stdout`.
go func() {
defer util.CloseAndIgnoreError(stdinPipe)
// `git hash-object` understands all relative paths to be relative to the repository.
// This function's result needs to be relative to `rootPath`.
// We convert all files to absolute paths and assume that they will be inside of the repository.
for _, file := range filesToHash {
converted := file.RestoreAnchor(anchor)
// `git hash-object` expects paths to use Unix separators, even on Windows.
// `git hash-object` expects paths to be one per line so we must escape newlines.
// In order to understand the escapes, the path must be quoted.
// In order to quote the path, the quotes in the path must be escaped.
// Other than that, we just write everything with full Unicode.
stringPath := converted.ToString()
toSlashed := filepath.ToSlash(stringPath)
escapedNewLines := strings.ReplaceAll(toSlashed, "\n", "\\n")
escapedQuotes := strings.ReplaceAll(escapedNewLines, "\"", "\\\"")
prepared := fmt.Sprintf("\"%s\"\n", escapedQuotes)
_, err := io.WriteString(stdinPipe, prepared)
if err != nil {
return
}
}
}()
// This gives us an io.ReadCloser so that we never have to read the entire input in
// at a single time. It is doing stream processing instead of string processing.
stdoutPipe, stdoutPipeError := cmd.StdoutPipe()
if stdoutPipeError != nil {
return nil, fmt.Errorf("failed to read `git hash-object`: %w", stdoutPipeError)
}
startError := cmd.Start()
if startError != nil {
return nil, fmt.Errorf("failed to read `git hash-object`: %w", startError)
}
// The output of `git hash-object` is a 40-character SHA per input, then a newline.
// We need to track the SHA that corresponds to the input file path.
index := 0
hashes := make([]string, len(filesToHash))
scanner := bufio.NewScanner(stdoutPipe)
// Read the output line-by-line (which is our separator) until exhausted.
for scanner.Scan() {
bytes := scanner.Bytes()
scanError := scanner.Err()
if scanError != nil {
return nil, fmt.Errorf("failed to read `git hash-object`: %w", scanError)
}
hashError := gitoutput.CheckObjectName(bytes)
if hashError != nil {
return nil, fmt.Errorf("failed to read `git hash-object`: %s", "invalid hash received")
}
// Worked, save it off.
hashes[index] = string(bytes)
index++
}
// Waits until stdout is closed before proceeding.
waitErr := cmd.Wait()
if waitErr != nil {
return nil, fmt.Errorf("failed to read `git hash-object`: %w", waitErr)
}
// Make sure we end up with a matching number of files and hashes.
hashCount := len(hashes)
if fileCount != hashCount {
return nil, fmt.Errorf("failed to read `git hash-object`: %d files %d hashes", fileCount, hashCount)
}
// The API of this method specifies that we return a `map[turbopath.AnchoredUnixPath]string`.
for i, hash := range hashes {
filePath := filesToHash[i]
output[filePath.ToUnixPath()] = hash
}
}
return output, nil
}
func manuallyHashFiles(rootPath turbopath.AbsoluteSystemPath, files []turbopath.AnchoredSystemPath) (map[turbopath.AnchoredUnixPath]string, error) {
hashObject := make(map[turbopath.AnchoredUnixPath]string)
for _, file := range files {
hash, err := fs.GitLikeHashFile(file.RestoreAnchor(rootPath))
if err != nil {
return nil, fmt.Errorf("could not hash file %v. \n%w", file.ToString(), err)
}
hashObject[file.ToUnixPath()] = hash
}
return hashObject, nil
}
// runGitCommand provides boilerplate command handling for `ls-tree`, `ls-files`, and `status`
// Rather than doing string processing, it does stream processing of `stdout`.
func runGitCommand(cmd *exec.Cmd, commandName string, handler func(io.Reader) *gitoutput.Reader) ([][]string, error) {
stdoutPipe, pipeError := cmd.StdoutPipe()
if pipeError != nil {
return nil, fmt.Errorf("failed to read `git %s`: %w", commandName, pipeError)
}
startError := cmd.Start()
if startError != nil {
return nil, fmt.Errorf("failed to read `git %s`: %w", commandName, startError)
}
reader := handler(stdoutPipe)
entries, readErr := reader.ReadAll()
if readErr != nil {
return nil, fmt.Errorf("failed to read `git %s`: %w", commandName, readErr)
}
waitErr := cmd.Wait()
if waitErr != nil {
return nil, fmt.Errorf("failed to read `git %s`: %w", commandName, waitErr)
}
return entries, nil
}
// gitLsTree returns a map of paths to their SHA hashes starting at a particular directory
// that are present in the `git` index at a particular revision.
func gitLsTree(rootPath turbopath.AbsoluteSystemPath) (map[turbopath.AnchoredUnixPath]string, error) {
cmd := exec.Command(
"git", // Using `git` from $PATH,
"ls-tree", // list the contents of the git index,
"-r", // recursively,
"-z", // with each file path relative to the invocation directory and \000-terminated,
"HEAD", // at this specified version.
)
cmd.Dir = rootPath.ToString() // Include files only from this directory.
entries, err := runGitCommand(cmd, "ls-tree", gitoutput.NewLSTreeReader)
if err != nil {
return nil, err
}
output := make(map[turbopath.AnchoredUnixPath]string, len(entries))
for _, entry := range entries {
lsTreeEntry := gitoutput.LsTreeEntry(entry)
output[turbopath.AnchoredUnixPathFromUpstream(lsTreeEntry.GetField(gitoutput.Path))] = lsTreeEntry[2]
}
return output, nil
}
// getTraversePath gets the distance of the current working directory to the repository root.
// This is used to convert repo-relative paths to cwd-relative paths.
//
// `git rev-parse --show-cdup` always returns Unix paths, even on Windows.
func getTraversePath(rootPath turbopath.AbsoluteSystemPath) (turbopath.RelativeUnixPath, error) {
cmd := exec.Command("git", "rev-parse", "--show-cdup")
cmd.Dir = rootPath.ToString()
traversePath, err := cmd.Output()
if err != nil {
return "", err
}
trimmedTraversePath := strings.TrimSuffix(string(traversePath), "\n")
return turbopath.RelativeUnixPathFromUpstream(trimmedTraversePath), nil
}
// Don't shell out if we already know where you are in the repository.
// `memoize` is a good candidate for generics.
func memoizeGetTraversePath() func(turbopath.AbsoluteSystemPath) (turbopath.RelativeUnixPath, error) {
cacheMutex := &sync.RWMutex{}
cachedResult := map[turbopath.AbsoluteSystemPath]turbopath.RelativeUnixPath{}
cachedError := map[turbopath.AbsoluteSystemPath]error{}
return func(rootPath turbopath.AbsoluteSystemPath) (turbopath.RelativeUnixPath, error) {
cacheMutex.RLock()
result, resultExists := cachedResult[rootPath]
err, errExists := cachedError[rootPath]
cacheMutex.RUnlock()
if resultExists && errExists {
return result, err
}
invokedResult, invokedErr := getTraversePath(rootPath)
cacheMutex.Lock()
cachedResult[rootPath] = invokedResult
cachedError[rootPath] = invokedErr
cacheMutex.Unlock()
return invokedResult, invokedErr
}
}
var memoizedGetTraversePath = memoizeGetTraversePath()
// statusCode represents the two-letter status code from `git status` with two "named" fields, x & y.
// They have different meanings based upon the actual state of the working tree. Using x & y maps
// to upstream behavior.
type statusCode struct {
x string
y string
}
func (s statusCode) isDelete() bool {
return s.x == "D" || s.y == "D"
}