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: ignore files in gitignore patterns #59

Merged
merged 1 commit into from
Jan 9, 2024
Merged
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
42 changes: 41 additions & 1 deletion internal/util/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"strings"
)

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

err := filepath.Walk(baseFolder, func(path string, info os.FileInfo, err error) error {
patterns, err := getGitignorePatterns(baseFolder)
if err != nil {
return nil, fmt.Errorf("getting gitignore patterns: %w", err)
}

err = filepath.Walk(baseFolder, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walking to %s: %w", path, err)
}
Expand All @@ -35,6 +42,10 @@ 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 {
Expand Down Expand Up @@ -111,3 +122,32 @@ func wrapNodeFunction(baseFolder string, envVars map[string]string) ([]byte, err

return buf.Bytes(), nil
}

func getGitignorePatterns(baseFolder string) ([]string, error) {
gitignorePath := path.Join(baseFolder, ".gitignore")
gitignoreContent, err := os.ReadFile(gitignorePath)
if err != nil {
// If .gitignore file doesn't exist, return an empty list
if os.IsNotExist(err) {
return []string{}, nil
}
return nil, fmt.Errorf("reading .gitignore file: %w", err)
}

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

func matchesGitignorePattern(filePath string, patterns []string) bool {
for _, pattern := range patterns {
matched, err := path.Match(pattern, filePath)
if err != nil {
// Handle error if path matching fails
continue
}
if matched {
return true
}
}
return false
}
Loading