From a2855300cdbcd569d2dfcc46e18a811a676e90a8 Mon Sep 17 00:00:00 2001 From: Matej Vasek Date: Thu, 9 Mar 2023 19:25:39 +0100 Subject: [PATCH 1/3] refactor: Copy() takes a predicate not a set Predicate is more universal than a set (Go map). Signed-off-by: Matej Vasek --- pkg/scm/downloaders/file/download.go | 7 ++++++- pkg/test/fs/fs.go | 10 +++++----- pkg/util/fs/fs.go | 20 ++++++++++---------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pkg/scm/downloaders/file/download.go b/pkg/scm/downloaders/file/download.go index acf233355..3d36f7ef9 100644 --- a/pkg/scm/downloaders/file/download.go +++ b/pkg/scm/downloaders/file/download.go @@ -54,9 +54,14 @@ func (f *File) Download(config *api.Config) (*git.SourceInfo, error) { return nil, lerr } + isIgnored := func(path string) bool { + _, ok := filesToIgnore[path] + return ok + } + if copySrc != config.WorkingSourceDir { f.KeepSymlinks(config.KeepSymlinks) - err := f.CopyContents(copySrc, config.WorkingSourceDir, filesToIgnore) + err = f.CopyContents(copySrc, config.WorkingSourceDir, isIgnored) if err != nil { return nil, err } diff --git a/pkg/test/fs/fs.go b/pkg/test/fs/fs.go index d378f6354..3b5402509 100644 --- a/pkg/test/fs/fs.go +++ b/pkg/test/fs/fs.go @@ -32,7 +32,7 @@ type FakeFileSystem struct { CopyDest string CopyError error - FilesToIgnore map[string]string + IsIgnored func(path string) bool RemoveDirName string RemoveDirError error @@ -161,18 +161,18 @@ func (f *FakeFileSystem) Exists(file string) bool { } // Copy copies files on the fake filesystem -func (f *FakeFileSystem) Copy(sourcePath, targetPath string, filesToIgnore map[string]string) error { +func (f *FakeFileSystem) Copy(sourcePath, targetPath string, IsIgnored func(path string) bool) error { f.CopySource = sourcePath f.CopyDest = targetPath - f.FilesToIgnore = filesToIgnore + f.IsIgnored = IsIgnored return f.CopyError } // CopyContents copies directory contents on the fake filesystem -func (f *FakeFileSystem) CopyContents(sourcePath, targetPath string, filesToIgnore map[string]string) error { +func (f *FakeFileSystem) CopyContents(sourcePath, targetPath string, isIgnored func(path string) bool) error { f.CopySource = sourcePath f.CopyDest = targetPath - f.FilesToIgnore = filesToIgnore + f.IsIgnored = isIgnored return f.CopyError } diff --git a/pkg/util/fs/fs.go b/pkg/util/fs/fs.go index 92d01303a..ba6e886ff 100644 --- a/pkg/util/fs/fs.go +++ b/pkg/util/fs/fs.go @@ -26,8 +26,8 @@ type FileSystem interface { MkdirAllWithPermissions(dirname string, perm os.FileMode) error Mkdir(dirname string) error Exists(file string) bool - Copy(sourcePath, targetPath string, filesToIgnore map[string]string) error - CopyContents(sourcePath, targetPath string, filesToIgnore map[string]string) error + Copy(sourcePath, targetPath string, isIgnored func(path string) bool) error + CopyContents(sourcePath, targetPath string, isIgnored func(path string) bool) error RemoveDirectory(dir string) error CreateWorkingDirectory() (string, error) Open(file string) (io.ReadCloser, error) @@ -184,8 +184,8 @@ func (h *fs) Exists(file string) bool { // If the source is a directory, then the destination has to be a directory and // we copy the content of the source directory to destination directory // recursively. -func (h *fs) Copy(source string, dest string, filesToIgnore map[string]string) (err error) { - return doCopy(h, source, dest, filesToIgnore) +func (h *fs) Copy(source string, dest string, isIgnored func(path string) bool) (err error) { + return doCopy(h, source, dest, isIgnored) } // KeepSymlinks configures fs to copy symlinks from src as symlinks to dst. @@ -227,7 +227,7 @@ func handleSymlink(h FileSystem, source, dest string) (bool, error) { return false, nil } -func doCopy(h FileSystem, source, dest string, filesToIgnore map[string]string) error { +func doCopy(h FileSystem, source, dest string, isIgnored func(path string) bool) error { if handled, err := handleSymlink(h, source, dest); handled || err != nil { return err } @@ -242,13 +242,13 @@ func doCopy(h FileSystem, source, dest string, filesToIgnore map[string]string) } if sourceinfo.IsDir() { - _, ok := filesToIgnore[source] + ok := isIgnored != nil && isIgnored(source) if ok { log.V(5).Infof("Directory %q ignored", source) return nil } log.V(5).Infof("D %q -> %q", source, dest) - return h.CopyContents(source, dest, filesToIgnore) + return h.CopyContents(source, dest, isIgnored) } destinfo, _ := h.Stat(dest) @@ -260,7 +260,7 @@ func doCopy(h FileSystem, source, dest string, filesToIgnore map[string]string) return err } defer destfile.Close() - _, ok := filesToIgnore[source] + ok := isIgnored != nil && isIgnored(source) if ok { log.V(5).Infof("File %q ignored", source) return nil @@ -279,7 +279,7 @@ func doCopy(h FileSystem, source, dest string, filesToIgnore map[string]string) // The source directory itself will not be copied, only its content. If you // want this behavior, the destination must include the source directory name. // It will skip any files provided in filesToIgnore from being copied -func (h *fs) CopyContents(src string, dest string, filesToIgnore map[string]string) (err error) { +func (h *fs) CopyContents(src, dest string, isIgnored func(path string) bool) (err error) { sourceinfo, err := h.Stat(src) if err != nil { return err @@ -300,7 +300,7 @@ func (h *fs) CopyContents(src string, dest string, filesToIgnore map[string]stri for _, obj := range objects { source := filepath.Join(src, obj.Name()) destination := filepath.Join(dest, obj.Name()) - if err := h.Copy(source, destination, filesToIgnore); err != nil { + if err := h.Copy(source, destination, isIgnored); err != nil { return err } } From 73fe5b59e17e7f3badea21ed229947a43e40a66f Mon Sep 17 00:00:00 2001 From: Matej Vasek Date: Thu, 9 Mar 2023 19:53:07 +0100 Subject: [PATCH 2/3] fix: honor --exclude when using --as-dockerfile Signed-off-by: Matej Vasek --- pkg/scm/downloaders/file/download.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/scm/downloaders/file/download.go b/pkg/scm/downloaders/file/download.go index 3d36f7ef9..f63597d39 100644 --- a/pkg/scm/downloaders/file/download.go +++ b/pkg/scm/downloaders/file/download.go @@ -3,6 +3,7 @@ package file import ( "fmt" "path/filepath" + "regexp" "github.com/openshift/source-to-image/pkg/api" "github.com/openshift/source-to-image/pkg/api/constants" @@ -54,9 +55,14 @@ func (f *File) Download(config *api.Config) (*git.SourceInfo, error) { return nil, lerr } + exclude, err := regexp.Compile(config.ExcludeRegExp) + if err != nil { + return nil, err + } + isIgnored := func(path string) bool { _, ok := filesToIgnore[path] - return ok + return ok || exclude.MatchString(path) } if copySrc != config.WorkingSourceDir { From 0bc1f25dac382d9ac1e999216dfdf2159c0ba24d Mon Sep 17 00:00:00 2001 From: Matej Vasek Date: Thu, 9 Mar 2023 22:57:56 +0100 Subject: [PATCH 3/3] fixup Signed-off-by: Matej Vasek --- pkg/scm/downloaders/file/download.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pkg/scm/downloaders/file/download.go b/pkg/scm/downloaders/file/download.go index f63597d39..34326e5cc 100644 --- a/pkg/scm/downloaders/file/download.go +++ b/pkg/scm/downloaders/file/download.go @@ -55,14 +55,22 @@ func (f *File) Download(config *api.Config) (*git.SourceInfo, error) { return nil, lerr } - exclude, err := regexp.Compile(config.ExcludeRegExp) - if err != nil { - return nil, err - } + var isIgnored func(path string) bool - isIgnored := func(path string) bool { - _, ok := filesToIgnore[path] - return ok || exclude.MatchString(path) + if config.ExcludeRegExp != "" { + exclude, err := regexp.Compile(config.ExcludeRegExp) + if err != nil { + return nil, err + } + isIgnored = func(path string) bool { + _, ok := filesToIgnore[path] + return ok || exclude.MatchString(path) + } + } else { + isIgnored = func(path string) bool { + _, ok := filesToIgnore[path] + return ok + } } if copySrc != config.WorkingSourceDir {