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: honor --exclude when using --as-dockerfile #1105

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
13 changes: 12 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,19 @@ func (f *File) Download(config *api.Config) (*git.SourceInfo, error) {
return nil, lerr
}

exclude, err := regexp.Compile(config.ExcludeRegExp)
matejvasek marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

isIgnored := func(path string) bool {
_, ok := filesToIgnore[path]
return ok || exclude.MatchString(path)
}

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