这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix hashing uncommitted changes #1448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions cli/internal/fs/package_deps_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,40 @@ type PackageDepsOptions struct {

// GetPackageDeps Builds an object containing git hashes for the files under the specified `packagePath` folder.
func GetPackageDeps(rootPath AbsolutePath, p *PackageDepsOptions) (map[turbopath.AnchoredUnixPath]string, error) {
pkgPath := rootPath.Join(p.PackagePath)
// Add all the checked in hashes.
var result map[turbopath.AnchoredUnixPath]string
if len(p.InputPatterns) == 0 {
gitLsTreeOutput, err := gitLsTree(rootPath.Join(p.PackagePath))
gitLsTreeOutput, err := gitLsTree(pkgPath)
if err != nil {
return nil, fmt.Errorf("could not get git hashes for files in package %s: %w", p.PackagePath, err)
}
result = gitLsTreeOutput
} else {
gitLsFilesOutput, err := gitLsFiles(rootPath.Join(p.PackagePath), p.InputPatterns)
gitLsFilesOutput, err := gitLsFiles(pkgPath, p.InputPatterns)
if err != nil {
return nil, fmt.Errorf("could not get git hashes for file patterns %v in package %s: %w", p.InputPatterns, p.PackagePath, err)
}
result = gitLsFilesOutput
}

// Update the checked in hashes with the current repo status
gitStatusOutput, err := gitStatus(rootPath.Join(p.PackagePath), p.InputPatterns)
// The paths returned from this call are anchored at the package directory
gitStatusOutput, err := gitStatus(pkgPath, p.InputPatterns)
if err != nil {
return nil, fmt.Errorf("Could not get git hashes from git status: %v", err)
}

var filesToHash []turbopath.AnchoredUnixPath
var filesToHash []turbopath.AnchoredSystemPath
for filePath, status := range gitStatusOutput {
if status.isDelete() {
delete(result, filePath)
} else {
filesToHash = append(filesToHash, filePath)
filesToHash = append(filesToHash, filePath.ToSystemPath())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning this up!

}
}

convertedRootPath := turbopath.AbsoluteSystemPathFromUpstream(rootPath.ToString())
hashes, err := gitHashObject(convertedRootPath, turbopath.AnchoredUnixPathArray(filesToHash).ToSystemPathArray())
hashes, err := gitHashObject(turbopath.AbsoluteSystemPathFromUpstream(pkgPath.ToString()), filesToHash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a way to specify the "expected" anchors. I don't think I can do that in the Go type system without doing

type RepoAnchoredSystemPath interface {
  AnchoredSystemPath
}

type PackageAnchoredSystemPath interface {
  AnchoredSystemPath
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth drawing attention to this line: rootPath to pkgPath. That's the material change in this PR.

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -89,10 +90,10 @@ func GetHashableDeps(rootPath AbsolutePath, files []turbopath.AbsoluteSystemPath
// gitHashObject returns a map of paths to their SHA hashes calculated by passing the paths `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 *repository* root.
// For that reason we convert all input paths and make them relative to the rootPath prior to passing them
// 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(rootPath turbopath.AbsoluteSystemPath, filesToHash []turbopath.AnchoredSystemPath) (map[turbopath.AnchoredUnixPath]string, error) {
func gitHashObject(anchor turbopath.AbsoluteSystemPath, filesToHash []turbopath.AnchoredSystemPath) (map[turbopath.AnchoredUnixPath]string, error) {
fileCount := len(filesToHash)
output := make(map[turbopath.AnchoredUnixPath]string, fileCount)

Expand All @@ -102,7 +103,7 @@ func gitHashObject(rootPath turbopath.AbsoluteSystemPath, filesToHash []turbopat
"hash-object", // hash a file,
"--stdin-paths", // using a list of newline-separated paths from stdin.
)
cmd.Dir = rootPath.ToString() // Start at this directory.
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`
Expand All @@ -125,7 +126,7 @@ func gitHashObject(rootPath turbopath.AbsoluteSystemPath, filesToHash []turbopat
// 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(rootPath)
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.
Expand Down
73 changes: 73 additions & 0 deletions cli/internal/fs/package_deps_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
Expand Down Expand Up @@ -215,6 +216,78 @@ func Test_getTraversePath(t *testing.T) {
}
}

func requireGitCmd(t *testing.T, repoRoot AbsolutePath, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = repoRoot.ToString()
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git commit failed: %v %v", err, string(out))
}
}

func TestGetPackageDeps(t *testing.T) {
// Directory structure:
// <root>/
// my-pkg/
// committed-file
// deleted-file
// uncommitted-file <- new file not added to git

repoRoot := AbsolutePathFromUpstream(t.TempDir())
myPkgDir := repoRoot.Join("my-pkg")
committedFilePath := myPkgDir.Join("committed-file")
err := committedFilePath.EnsureDir()
assert.NilError(t, err, "EnsureDir")
err = committedFilePath.WriteFile([]byte("committed bytes"), 0644)
assert.NilError(t, err, "WriteFile")
deletedFilePath := myPkgDir.Join("deleted-file")
err = deletedFilePath.WriteFile([]byte("delete-me"), 0644)
assert.NilError(t, err, "WriteFile")
requireGitCmd(t, repoRoot, "init", ".")
requireGitCmd(t, repoRoot, "config", "--local", "user.name", "test")
requireGitCmd(t, repoRoot, "config", "--local", "user.email", "test@example.com")
requireGitCmd(t, repoRoot, "add", ".")
requireGitCmd(t, repoRoot, "commit", "-m", "foo")
err = deletedFilePath.Remove()
assert.NilError(t, err, "Remove")
uncommittedFilePath := myPkgDir.Join("uncommitted-file")
err = uncommittedFilePath.WriteFile([]byte("uncommitted bytes"), 0644)
assert.NilError(t, err, "WriteFile")

tests := []struct {
opts *PackageDepsOptions
expected map[turbopath.AnchoredUnixPath]string
}{
{
opts: &PackageDepsOptions{
PackagePath: "my-pkg",
},
expected: map[turbopath.AnchoredUnixPath]string{
"committed-file": "3a29e62ea9ba15c4a4009d1f605d391cdd262033",
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
},
},
{
opts: &PackageDepsOptions{
PackagePath: "my-pkg",
InputPatterns: []string{"uncommitted-file"},
},
expected: map[turbopath.AnchoredUnixPath]string{
"uncommitted-file": "4e56ad89387e6379e4e91ddfe9872cf6a72c9976",
},
},
}
for _, tt := range tests {
got, err := GetPackageDeps(repoRoot, tt.opts)
if err != nil {
t.Errorf("GetPackageDeps got error %v", err)
continue
}
assert.DeepEqual(t, got, tt.expected)
}
}

func Test_memoizedGetTraversePath(t *testing.T) {
fixturePath := getFixture(1)

Expand Down