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

Preserve sync subtree for '***'. #1813

Merged
merged 6 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 37 additions & 14 deletions pkg/skaffold/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os/exec"
"path/filepath"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
Expand Down Expand Up @@ -128,21 +129,43 @@ func intersect(context string, syncMap map[string]string, files []string, workin
return nil, errors.Wrapf(err, "changed file %s can't be found relative to context %s", f, context)
}
var matches bool
for p, dst := range syncMap {
michaelfig marked this conversation as resolved.
Show resolved Hide resolved
match, err := doublestar.PathMatch(filepath.FromSlash(p), relPath)
if err != nil {
return nil, errors.Wrapf(err, "pattern error for %s", relPath)
}
if match {
if filepath.IsAbs(dst) {
dst = filepath.ToSlash(filepath.Join(dst, filepath.Base(relPath)))
} else {
dst = filepath.ToSlash(filepath.Join(workingDir, dst, filepath.Base(relPath)))
// Prioritize '***' sync map rules, for backward compatibility.
for _, doTripleStar := range []bool{true, false} {
for p, dst := range syncMap {
// Replace triple-star with double-star to make a pattern.
pat := strings.Replace(p, "***", "**", -1)
isTripleStar := len(pat) != len(p)
if isTripleStar != doTripleStar {
// Wait until the right phase.
continue
}
match, err := doublestar.PathMatch(filepath.FromSlash(pat), relPath)
if err != nil {
return nil, errors.Wrapf(err, "pattern error for %s", relPath)
}
if match {
var subPath string
if isTripleStar {
// Map the paths as a tree from the prefix.
subtreePrefix := strings.Split(p, "***")[0]
subPath = strings.TrimPrefix(relPath, filepath.FromSlash(subtreePrefix))
} else {
// Collapse the paths.
subPath = filepath.Base(relPath)
}
if filepath.IsAbs(dst) {
dst = filepath.ToSlash(filepath.Join(dst, subPath))
} else {
dst = filepath.ToSlash(filepath.Join(workingDir, dst, subPath))
}
// Every file must match at least one sync pattern, if not we'll have to
// skip the entire sync
matches = true
if _, ok := ret[f]; !ok {
// Don't overwrite existing returns.
ret[f] = dst
}
}
// Every file must match at least one sync pattern, if not we'll have to
// skip the entire sync
matches = true
ret[f] = dst
}
}
if !matches {
Expand Down
55 changes: 55 additions & 0 deletions pkg/skaffold/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,61 @@ func TestNewSyncItem(t *testing.T) {
Delete: map[string]string{},
},
},
{
description: "triple-stars mean subtrees",
artifact: &latest.Artifact{
ImageName: "test",
Sync: map[string]string{
"dir1/***/*.js": ".",
},
Workspace: ".",
},
workingDir: "/some/dir",
builds: []build.Artifact{
{
ImageName: "test",
Tag: "test:123",
},
},
evt: watch.Events{
Added: []string{filepath.Join("dir1", "dir2/node.js")},
michaelfig marked this conversation as resolved.
Show resolved Hide resolved
},
expected: &Item{
Image: "test:123",
Copy: map[string]string{
filepath.Join("dir1", "dir2/node.js"): "/some/dir/dir2/node.js",
},
Delete: map[string]string{},
},
},
{
description: "triple-stars take precedence",
artifact: &latest.Artifact{
ImageName: "test",
Sync: map[string]string{
"dir1/***/*.js": ".",
"dir1/**/**/*.js": ".",
},
Workspace: ".",
},
workingDir: "/some/dir",
builds: []build.Artifact{
{
ImageName: "test",
Tag: "test:123",
},
},
evt: watch.Events{
Added: []string{filepath.Join("dir1", "dir2/node.js")},
},
expected: &Item{
Image: "test:123",
Copy: map[string]string{
filepath.Join("dir1", "dir2/node.js"): "/some/dir/dir2/node.js",
},
Delete: map[string]string{},
},
},
}

for _, test := range tests {
Expand Down