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

use go-gitignore #62

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ require (

require (
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ github.com/containerd/stargz-snapshotter/estargz v0.14.3/go.mod h1:KY//uOCIkSuNA
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 h1:0nsrg//Dc7xC74H/TZ5sYR8uk4UQRNjsw8zejqH5a4Q=
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817/go.mod h1:C/+sI4IFnEpCn6VQ3GIPEp+FrQnQw+YQP3+n+GdGq7o=
github.com/docker/cli v24.0.0+incompatible h1:0+1VshNwBQzQAx9lOl+OYCTCEAD8fKs/qeXMx3O0wqM=
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=
github.com/docker/docker v24.0.0+incompatible h1:z4bf8HvONXX9Tde5lGBMQ7yCJgNahmJumdrStZAbeY4=
Expand Down
17 changes: 12 additions & 5 deletions internal/util/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path"
"path/filepath"
"strings"

"github.com/denormal/go-gitignore"
)

func PackZip() ([]byte, string, error) {
Expand All @@ -28,7 +30,7 @@ func wrapNodeFunction(baseFolder string, envVars map[string]string) ([]byte, err
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)

patterns, err := getGitignorePatterns(baseFolder)
ignore, err := gitignore.NewFromFile(baseFolder + "/.gitignore")
if err != nil {
return nil, fmt.Errorf("getting gitignore patterns: %w", err)
}
Expand All @@ -42,16 +44,17 @@ func wrapNodeFunction(baseFolder string, envVars map[string]string) ([]byte, err
return nil
}

if matchesGitignorePattern(path, patterns) {
return nil
}

// This will ensure only the content inside baseFolder is included at the root of the ZIP.
relativePath, err := filepath.Rel(baseFolder, path)
if err != nil {
return fmt.Errorf("getting relative path: %w", err)
}

match := ignore.Match(relativePath)
if match != nil {
return nil
}

lstat, err := os.Lstat(path)
if err != nil {
return fmt.Errorf("lstat: %w", err)
Expand Down Expand Up @@ -135,17 +138,21 @@ func getGitignorePatterns(baseFolder string) ([]string, error) {
}

patterns := strings.Split(string(gitignoreContent), "\n")
fmt.Println("patterns: " + strings.Join(patterns, ","))
return patterns, nil
}

func matchesGitignorePattern(filePath string, patterns []string) bool {
fmt.Print("filePath: " + filePath)

for _, pattern := range patterns {
matched, err := path.Match(pattern, filePath)
if err != nil {
// Handle error if path matching fails
continue
}
if matched {
fmt.Println(" matched")
return true
}
}
Expand Down
Loading