Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
modulo11 committed Sep 5, 2024
1 parent a5d98b2 commit f12720b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 36 deletions.
96 changes: 60 additions & 36 deletions pkg/cnbutils/copy_project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cnbutils

import (
"fmt"
"io/fs"
"os"
"path"
Expand All @@ -12,30 +13,71 @@ import (
ignore "github.com/sabhiram/go-gitignore"
)

func isFiltered(path string, knownSymlinks []string) bool {
for _, sym := range knownSymlinks {
if strings.HasPrefix(path, sym) {
return true
}
}
return false
}

func filterSourceFiles(sourceFiles []string, utils BuildUtils) ([]string, error) {
filteredFiles := []string{}
knownSymlinks := []string{}

for _, soureFile := range sourceFiles {
stat, _ := utils.Stat(soureFile)
lstat, _ := utils.Lstat(soureFile)

if isFiltered(soureFile, knownSymlinks) {
continue
}

if lstat.Mode().Type() == fs.ModeSymlink {
if stat.Mode().Type() == fs.ModeDir {
fmt.Println("Filtering out", soureFile)
knownSymlinks = append(knownSymlinks, soureFile)
}
filteredFiles = append(filteredFiles, soureFile)
} else {
if !isFiltered(soureFile, knownSymlinks) {
filteredFiles = append(filteredFiles, soureFile)
}
}
}
return filteredFiles, nil
}

func CopyProject(source, target string, include, exclude *ignore.GitIgnore, utils BuildUtils, follow bool) error {
sourceFiles, err := utils.Glob(path.Join(source, "**"))
if err != nil {
return err
}

knownSymlinks := []string{}
if !follow {
sourceFiles, err = filterSourceFiles(sourceFiles, utils)
if err != nil {
return err
}
}

for _, sourceFile := range sourceFiles {
skipFile, err := isIgnored(source, sourceFile, include, exclude, knownSymlinks)
relPath, err := filepath.Rel(source, sourceFile)
if err != nil {
return err
log.SetErrorCategory(log.ErrorBuild)
return errors.Wrapf(err, "Calculating relative path for '%s' failed", sourceFile)
}

if !skipFile {
if !isIgnored(relPath, include, exclude) {
target := path.Join(target, strings.ReplaceAll(sourceFile, source, ""))

isDir, err := utils.DirExists(sourceFile)
isSymlink, err := symlinkExists(sourceFile, utils)
if err != nil {
log.SetErrorCategory(log.ErrorBuild)
return errors.Wrapf(err, "Checking file info '%s' failed", target)
return err
}

isSymlink, err := symlinkExists(sourceFile, utils)
isDir, err := utils.DirExists(sourceFile)
if err != nil {
return err
}
Expand All @@ -45,19 +87,13 @@ func CopyProject(source, target string, include, exclude *ignore.GitIgnore, util
if err != nil {
return err
}
if !isDir || !follow {
log.Entry().Debugf("Creating symlink from %s to %s", linkTarget, target)
err = utils.Symlink(linkTarget, target)
if err != nil {
return err
}
}
if !follow {
log.Entry().Debugf("Adding %s to list of known symlinks", sourceFile)
knownSymlinks = append(knownSymlinks, sourceFile)
log.Entry().Debugf("Creating symlink from %s to %s", linkTarget, target)
err = utils.Symlink(linkTarget, target)
if err != nil {
return err
}

} else if isDir {
log.Entry().Debugf("Creating directory %s", target)
err = utils.MkdirAll(target, os.ModePerm)
if err != nil {
log.SetErrorCategory(log.ErrorBuild)
Expand Down Expand Up @@ -102,25 +138,13 @@ func copyFile(source, target string, utils BuildUtils) error {
return err
}

func isIgnored(source, sourceFile string, include, exclude *ignore.GitIgnore, knownSymlinks []string) (bool, error) {
find, err := filepath.Rel(source, sourceFile)
if err != nil {
log.SetErrorCategory(log.ErrorBuild)
return false, errors.Wrapf(err, "Calculating relative path for '%s' failed", sourceFile)
}

for _, link := range knownSymlinks {
if strings.HasPrefix(sourceFile, link) {
return true, nil
}
}

func isIgnored(find string, include, exclude *ignore.GitIgnore) bool {
if exclude != nil {
filtered := exclude.MatchesPath(find)

if filtered {
log.Entry().Debugf("%s matches exclude pattern, ignoring", find)
return true, nil
return true
}
}

Expand All @@ -129,12 +153,12 @@ func isIgnored(source, sourceFile string, include, exclude *ignore.GitIgnore, kn

if filtered {
log.Entry().Debugf("%s doesn't match include pattern, ignoring", find)
return true, nil
return true
} else {
log.Entry().Debugf("%s matches include pattern", find)
return false, nil
return false
}
}

return false, nil
return false
}
1 change: 1 addition & 0 deletions pkg/piperutils/fileUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type FileUtils interface {
Open(name string) (io.ReadWriteCloser, error)
Create(name string) (io.ReadWriteCloser, error)
Readlink(name string) (string, error)
Stat(path string) (os.FileInfo, error)
Lstat(path string) (os.FileInfo, error)
}

Expand Down

0 comments on commit f12720b

Please sign in to comment.