Merge pull request #4514 from thaJeztah/patternmatcher_dockerignore

replace dockerfile/dockerignore with patternmatcher/ignorefile
This commit is contained in:
Sebastiaan van Stijn 2023-08-23 13:53:41 +02:00 committed by GitHub
commit 41183c2c92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 16 deletions

View File

@ -1,11 +1,12 @@
package build package build
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
"github.com/moby/patternmatcher" "github.com/moby/patternmatcher"
"github.com/moby/patternmatcher/ignorefile"
) )
// ReadDockerignore reads the .dockerignore file in the context directory and // ReadDockerignore reads the .dockerignore file in the context directory and
@ -22,7 +23,11 @@ func ReadDockerignore(contextDir string) ([]string, error) {
} }
defer f.Close() defer f.Close()
return dockerignore.ReadAll(f) patterns, err := ignorefile.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("error reading .dockerignore: %w", err)
}
return patterns, nil
} }
// TrimBuildFilesFromExcludes removes the named Dockerfile and .dockerignore from // TrimBuildFilesFromExcludes removes the named Dockerfile and .dockerignore from

View File

@ -23,7 +23,7 @@ require (
github.com/mattn/go-runewidth v0.0.14 github.com/mattn/go-runewidth v0.0.14
github.com/mitchellh/mapstructure v1.3.2 github.com/mitchellh/mapstructure v1.3.2
github.com/moby/buildkit v0.11.6 github.com/moby/buildkit v0.11.6
github.com/moby/patternmatcher v0.5.0 github.com/moby/patternmatcher v0.6.0
github.com/moby/swarmkit/v2 v2.0.0-20230713153928-bc71908479e5 github.com/moby/swarmkit/v2 v2.0.0-20230713153928-bc71908479e5
github.com/moby/sys/sequential v0.5.0 github.com/moby/sys/sequential v0.5.0
github.com/moby/sys/signal v0.7.0 github.com/moby/sys/signal v0.7.0

View File

@ -154,8 +154,8 @@ github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYG
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/buildkit v0.11.6 h1:VYNdoKk5TVxN7k4RvZgdeM4GOyRvIi4Z8MXOY7xvyUs= github.com/moby/buildkit v0.11.6 h1:VYNdoKk5TVxN7k4RvZgdeM4GOyRvIi4Z8MXOY7xvyUs=
github.com/moby/buildkit v0.11.6/go.mod h1:GCqKfHhz+pddzfgaR7WmHVEE3nKKZMMDPpK8mh3ZLv4= github.com/moby/buildkit v0.11.6/go.mod h1:GCqKfHhz+pddzfgaR7WmHVEE3nKKZMMDPpK8mh3ZLv4=
github.com/moby/patternmatcher v0.5.0 h1:YCZgJOeULcxLw1Q+sVR636pmS7sPEn1Qo2iAN6M7DBo= github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
github.com/moby/patternmatcher v0.5.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
github.com/moby/swarmkit/v2 v2.0.0-20230713153928-bc71908479e5 h1:o6x+wIX1vKD0kJlEqe8M9TLIe0SK8lnGcA6XoJtaFqg= github.com/moby/swarmkit/v2 v2.0.0-20230713153928-bc71908479e5 h1:o6x+wIX1vKD0kJlEqe8M9TLIe0SK8lnGcA6XoJtaFqg=
github.com/moby/swarmkit/v2 v2.0.0-20230713153928-bc71908479e5/go.mod h1:XUMlwIIC+wrwBDMUjxEvk5Z8FPoIPM8LdBw7w/Zu1rg= github.com/moby/swarmkit/v2 v2.0.0-20230713153928-bc71908479e5/go.mod h1:XUMlwIIC+wrwBDMUjxEvk5Z8FPoIPM8LdBw7w/Zu1rg=
github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc= github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc=

View File

@ -1,4 +1,4 @@
package dockerignore package ignorefile
import ( import (
"bufio" "bufio"
@ -6,23 +6,31 @@ import (
"io" "io"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/pkg/errors"
) )
// ReadAll reads a .dockerignore file and returns the list of file patterns // ReadAll reads an ignore file from a reader and returns the list of file
// to ignore. Note this will trim whitespace from each line as well // patterns to ignore, applying the following rules:
// as use GO's "clean" func to get the shortest/cleanest path for each. //
// - An UTF8 BOM header (if present) is stripped.
// - Lines starting with "#" are considered comments and are skipped.
//
// For remaining lines:
//
// - Leading and trailing whitespace is removed from each ignore pattern.
// - It uses [filepath.Clean] to get the shortest/cleanest path for
// ignore patterns.
// - Leading forward-slashes ("/") are removed from ignore patterns,
// so "/some/path" and "some/path" are considered equivalent.
func ReadAll(reader io.Reader) ([]string, error) { func ReadAll(reader io.Reader) ([]string, error) {
if reader == nil { if reader == nil {
return nil, nil return nil, nil
} }
scanner := bufio.NewScanner(reader)
var excludes []string var excludes []string
currentLine := 0 currentLine := 0
utf8bom := []byte{0xEF, 0xBB, 0xBF} utf8bom := []byte{0xEF, 0xBB, 0xBF}
scanner := bufio.NewScanner(reader)
for scanner.Scan() { for scanner.Scan() {
scannedBytes := scanner.Bytes() scannedBytes := scanner.Bytes()
// We trim UTF8 BOM // We trim UTF8 BOM
@ -59,7 +67,7 @@ func ReadAll(reader io.Reader) ([]string, error) {
excludes = append(excludes, pattern) excludes = append(excludes, pattern)
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
return nil, errors.Wrap(err, "error reading .dockerignore") return nil, err
} }
return excludes, nil return excludes, nil
} }

4
vendor/modules.txt vendored
View File

@ -167,11 +167,11 @@ github.com/miekg/pkcs11
github.com/mitchellh/mapstructure github.com/mitchellh/mapstructure
# github.com/moby/buildkit v0.11.6 # github.com/moby/buildkit v0.11.6
## explicit; go 1.18 ## explicit; go 1.18
github.com/moby/buildkit/frontend/dockerfile/dockerignore
github.com/moby/buildkit/util/appcontext github.com/moby/buildkit/util/appcontext
# github.com/moby/patternmatcher v0.5.0 # github.com/moby/patternmatcher v0.6.0
## explicit; go 1.19 ## explicit; go 1.19
github.com/moby/patternmatcher github.com/moby/patternmatcher
github.com/moby/patternmatcher/ignorefile
# github.com/moby/swarmkit/v2 v2.0.0-20230713153928-bc71908479e5 # github.com/moby/swarmkit/v2 v2.0.0-20230713153928-bc71908479e5
## explicit; go 1.18 ## explicit; go 1.18
github.com/moby/swarmkit/v2/api github.com/moby/swarmkit/v2/api