Skip to content

Commit

Permalink
Prefactor package hashing (#4816)
Browse files Browse the repository at this point in the history
We currently do file hashing for packages (and fallbacks) in _lots_ of
different places, making it _very_ difficult to trace through or
(more-importantly) modify the set of files whose hashes we need to
include.

This PR makes no material changes, each commit passes all of our tests.
At the end of it all of the hashing logic ends up in just one spot.

---------

Co-authored-by: Nathan Hammond <Nathan Hammond>
  • Loading branch information
nathanhammond committed May 4, 2023
1 parent 00f5fa0 commit 0cae83b
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 436 deletions.
21 changes: 3 additions & 18 deletions cli/internal/fs/hash.go
Expand Up @@ -5,9 +5,9 @@ import (
"encoding/hex"
"fmt"
"io"
"os"
"strconv"

"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/xxhash"
)

Expand All @@ -19,25 +19,10 @@ func HashObject(i interface{}) (string, error) {
return hex.EncodeToString(hash.Sum(nil)), err
}

func HashFile(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()

hash := xxhash.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}

return hex.EncodeToString(hash.Sum(nil)), nil
}

// GitLikeHashFile is a function that mimics how Git
// calculates the SHA1 for a file (or, in Git terms, a "blob") (without git)
func GitLikeHashFile(filePath string) (string, error) {
file, err := os.Open(filePath)
func GitLikeHashFile(filePath turbopath.AbsoluteSystemPath) (string, error) {
file, err := filePath.Open()
if err != nil {
return "", err
}
Expand Down

0 comments on commit 0cae83b

Please sign in to comment.