replace dockerfile/dockerignore with patternmatcher/ignorefile

The BuildKit dockerignore package was migrated to the patternmatcher
repository / module. This patch updates our uses of the BuildKit package
with its new location.

A small local change was made to keep the format of the existing error message,
because the "ignorefile" package is slightly more agnostic in that respect
and doesn't include ".dockerignore" in the error message.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-08-23 00:05:32 +02:00
parent 55ff9e6093
commit 5bff12354d
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
3 changed files with 25 additions and 12 deletions

View File

@ -1,11 +1,12 @@
package build
import (
"fmt"
"os"
"path/filepath"
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
"github.com/moby/patternmatcher"
"github.com/moby/patternmatcher/ignorefile"
)
// ReadDockerignore reads the .dockerignore file in the context directory and
@ -22,7 +23,11 @@ func ReadDockerignore(contextDir string) ([]string, error) {
}
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

View File

@ -1,4 +1,4 @@
package dockerignore
package ignorefile
import (
"bufio"
@ -6,23 +6,31 @@ import (
"io"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// ReadAll reads a .dockerignore file and returns the list of file patterns
// to ignore. Note this will trim whitespace from each line as well
// as use GO's "clean" func to get the shortest/cleanest path for each.
// ReadAll reads an ignore file from a reader and returns the list of file
// patterns to ignore, applying the following rules:
//
// - 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) {
if reader == nil {
return nil, nil
}
scanner := bufio.NewScanner(reader)
var excludes []string
currentLine := 0
utf8bom := []byte{0xEF, 0xBB, 0xBF}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
scannedBytes := scanner.Bytes()
// We trim UTF8 BOM
@ -59,7 +67,7 @@ func ReadAll(reader io.Reader) ([]string, error) {
excludes = append(excludes, pattern)
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrap(err, "error reading .dockerignore")
return nil, err
}
return excludes, nil
}

2
vendor/modules.txt vendored
View File

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