Skip to content

Commit b827e64

Browse files
committed
fix: exclude file ! pattern
1 parent 8daea4c commit b827e64

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

pkg/devspace/services/sync/controller.go

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sync
22

33
import (
4+
"bufio"
45
"bytes"
56
"context"
67
"fmt"
@@ -26,7 +27,6 @@ import (
2627
"github.com/loft-sh/devspace/pkg/devspace/sync"
2728
logpkg "github.com/loft-sh/devspace/pkg/util/log"
2829
"github.com/loft-sh/devspace/pkg/util/scanner"
29-
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
3030
"github.com/pkg/errors"
3131
v1 "k8s.io/api/core/v1"
3232
)
@@ -541,14 +541,66 @@ func parseExcludeFile(path string) ([]string, error) {
541541
}
542542
defer reader.Close()
543543

544-
paths, err := dockerignore.ReadAll(reader)
544+
paths, err := readAll(reader)
545545
if err != nil {
546546
return nil, errors.Wrap(err, "read exclude file")
547547
}
548548

549549
return paths, nil
550550
}
551551

552+
// Taken from Dockerignore
553+
// ReadAll reads a .dockerignore file and returns the list of file patterns
554+
// to ignore. Note this will trim whitespace from each line as well
555+
// as use GO's "clean" func to get the shortest/cleanest path for each.
556+
func readAll(reader io.Reader) ([]string, error) {
557+
if reader == nil {
558+
return nil, nil
559+
}
560+
561+
scanner := bufio.NewScanner(reader)
562+
var excludes []string
563+
currentLine := 0
564+
565+
utf8bom := []byte{0xEF, 0xBB, 0xBF}
566+
for scanner.Scan() {
567+
scannedBytes := scanner.Bytes()
568+
// We trim UTF8 BOM
569+
if currentLine == 0 {
570+
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
571+
}
572+
pattern := string(scannedBytes)
573+
currentLine++
574+
// Lines starting with # (comments) are ignored before processing
575+
if strings.HasPrefix(pattern, "#") {
576+
continue
577+
}
578+
pattern = strings.TrimSpace(pattern)
579+
if pattern == "" {
580+
continue
581+
}
582+
// normalize absolute paths to paths relative to the context
583+
// (taking care of '!' prefix)
584+
invert := pattern[0] == '!'
585+
if invert {
586+
pattern = strings.TrimSpace(pattern[1:])
587+
}
588+
if len(pattern) > 0 {
589+
pattern = filepath.Clean(pattern)
590+
pattern = filepath.ToSlash(pattern)
591+
}
592+
if invert {
593+
pattern = "!" + pattern
594+
}
595+
596+
excludes = append(excludes, pattern)
597+
}
598+
if err := scanner.Err(); err != nil {
599+
return nil, fmt.Errorf("Error reading .dockerignore: %v", err)
600+
}
601+
return excludes, nil
602+
}
603+
552604
func StartStream(ctx context.Context, client kubectl.Client, pod *v1.Pod, container string, command []string, reader io.Reader, stdoutWriter io.Writer, buffer bool, log logpkg.Logger) error {
553605
stderrBuffer := &bytes.Buffer{}
554606
stderrReader, stderrWriter := io.Pipe()

0 commit comments

Comments
 (0)