From b41ddc60580da31201ea3d53769907336b0d01c4 Mon Sep 17 00:00:00 2001 From: Nao YONASHIRO Date: Sat, 15 Dec 2018 02:42:28 +0900 Subject: [PATCH] feat: improves ValidateContextDirectory performance Signed-off-by: Nao YONASHIRO --- cli/command/image/build/context.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cli/command/image/build/context.go b/cli/command/image/build/context.go index 5d0e96737d..4920ce3676 100644 --- a/cli/command/image/build/context.go +++ b/cli/command/image/build/context.go @@ -41,6 +41,20 @@ func ValidateContextDirectory(srcPath string, excludes []string) error { if err != nil { return err } + + pm, err := fileutils.NewPatternMatcher(excludes) + if err != nil { + return err + } + matches := func(file string) (bool, error) { + file = filepath.Clean(file) + if file == "." { + // Don't let them exclude everything, kind of silly. + return false, nil + } + return pm.Matches(file) + } + return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error { if err != nil { if os.IsPermission(err) { @@ -55,7 +69,7 @@ func ValidateContextDirectory(srcPath string, excludes []string) error { // skip this directory/file if it's not in the path, it won't get added to the context if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil { return err - } else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil { + } else if skip, err := matches(relFilePath); err != nil { return err } else if skip { if f.IsDir() {