Skip to content
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

Fix bug where dirty submodules broke hash generation #711

Merged
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
19 changes: 8 additions & 11 deletions pkg/skaffold/build/tag/git_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
Expand Down Expand Up @@ -152,7 +150,8 @@ func commitOrTag(currentTag string, tags []string, opts *Options) string {
return fmt.Sprintf("%s:%s", opts.ImageName, currentTag)
}

// The file state is dirty. To generate a unique suffix, let's hash all the modified files.
// The file state is dirty. To generate a unique suffix, let's hash the diffs
// of all modified files.
// We add a -dirty-unique-id suffix to work well with local iterations.
func dirtyTag(root string, opts *Options, currentTag string, lines []string) (string, error) {
h := sha256.New()
Expand All @@ -162,25 +161,23 @@ func dirtyTag(root string, opts *Options, currentTag string, lines []string) (st
}

if _, err := h.Write([]byte(statusLine)); err != nil {
return "", errors.Wrap(err, "adding deleted file to diff")
return "", errors.Wrap(err, "adding status line to hash")
}

// If the file has been deleted, there's no diff to generate.
if strings.HasPrefix(statusLine, "D") {
continue
}

changedPath := strings.Trim(statusLine[2:], " ")
f, err := os.Open(filepath.Join(root, changedPath))
changedPath := filepath.Join(root, strings.Trim(statusLine[2:], " "))
diff, err := runGit(root, "diff", changedPath)
if err != nil {
return "", errors.Wrap(err, "reading diff")
}

if _, err := io.Copy(h, f); err != nil {
f.Close()
return "", errors.Wrap(err, "reading diff")
if _, err := h.Write([]byte(diff)); err != nil {
return "", errors.Wrap(err, "adding diff to hash")
}

f.Close()
}

sha := h.Sum(nil)
Expand Down
12 changes: 6 additions & 6 deletions pkg/skaffold/build/tag/git_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
opts: &Options{
ImageName: "test",
},
expectedName: "test:eefe1b9-dirty-af8de1fde8be4367",
expectedName: "test:eefe1b9-dirty-8b8c4dad90faa822",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
Expand All @@ -95,7 +95,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
ImageName: "test",
Digest: "sha256:12345abcde",
},
expectedName: "test:eefe1b9-dirty-af8de1fde8be4367",
expectedName: "test:eefe1b9-dirty-8b8c4dad90faa822",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
Expand All @@ -110,7 +110,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
opts: &Options{
ImageName: "test",
},
expectedName: "test:eefe1b9-dirty-bfe9b4566c9d3fec",
expectedName: "test:eefe1b9-dirty-e0bc2923501f63b7",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
opts: &Options{
ImageName: "test",
},
expectedName: "test:eefe1b9-dirty-9c858d88cc0bf792",
expectedName: "test:eefe1b9-dirty-c9417af5dc664b60",
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
Expand All @@ -168,7 +168,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
opts: &Options{
ImageName: "test",
},
expectedName: "test:eefe1b9-dirty-6534adc17ccd1cf4", // Must be <> each time a new name is used
expectedName: "test:eefe1b9-dirty-91fd2028ff0a5cf3", // Must be <> each time a new name is used
createGitRepo: func(dir string) {
gitInit(t, dir).
write("source.go", []byte("code")).
Expand All @@ -195,7 +195,7 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {
opts: &Options{
ImageName: "test",
},
expectedName: "test:a7b32a6-dirty-83715cdc64e43ee9",
expectedName: "test:a7b32a6-dirty-2dfb095d0f4830ad",
createGitRepo: func(dir string) {
gitInit(t, dir).
mkdir("sub/sub").
Expand Down