Skip to content

Commit

Permalink
Merge pull request #1105 from matejvasek/fix-exclude-for-as-dockerfile
Browse files Browse the repository at this point in the history
fix: honor --exclude when using --as-dockerfile
  • Loading branch information
openshift-merge-robot authored Mar 10, 2023
2 parents 78363ee + 0bc1f25 commit db599d2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
21 changes: 20 additions & 1 deletion pkg/scm/downloaders/file/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -54,9 +55,27 @@ func (f *File) Download(config *api.Config) (*git.SourceInfo, error) {
return nil, lerr
}

var isIgnored func(path string) bool

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 {
f.KeepSymlinks(config.KeepSymlinks)
err := f.CopyContents(copySrc, config.WorkingSourceDir, filesToIgnore)
err = f.CopyContents(copySrc, config.WorkingSourceDir, isIgnored)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/test/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type FakeFileSystem struct {
CopyDest string
CopyError error

FilesToIgnore map[string]string
IsIgnored func(path string) bool

RemoveDirName string
RemoveDirError error
Expand Down Expand Up @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/util/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down

0 comments on commit db599d2

Please sign in to comment.