2018-02-05 16:05:59 -05:00
|
|
|
package dockerignore // import "github.com/docker/docker/builder/dockerignore"
|
2015-12-14 05:15:00 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-06-03 10:26:36 -04:00
|
|
|
"bytes"
|
2015-12-14 05:15:00 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2016-06-24 23:57:21 -04:00
|
|
|
func ReadAll(reader io.Reader) ([]string, error) {
|
2015-12-14 05:15:00 -05:00
|
|
|
if reader == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
|
2015-12-14 05:15:00 -05:00
|
|
|
scanner := bufio.NewScanner(reader)
|
|
|
|
var excludes []string
|
2016-06-03 10:26:36 -04:00
|
|
|
currentLine := 0
|
2015-12-14 05:15:00 -05:00
|
|
|
|
2016-06-03 10:26:36 -04:00
|
|
|
utf8bom := []byte{0xEF, 0xBB, 0xBF}
|
2015-12-14 05:15:00 -05:00
|
|
|
for scanner.Scan() {
|
2016-06-03 10:26:36 -04:00
|
|
|
scannedBytes := scanner.Bytes()
|
|
|
|
// We trim UTF8 BOM
|
|
|
|
if currentLine == 0 {
|
|
|
|
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
|
|
|
|
}
|
|
|
|
pattern := string(scannedBytes)
|
|
|
|
currentLine++
|
2016-06-01 18:20:54 -04:00
|
|
|
// Lines starting with # (comments) are ignored before processing
|
|
|
|
if strings.HasPrefix(pattern, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pattern = strings.TrimSpace(pattern)
|
2015-12-14 05:15:00 -05:00
|
|
|
if pattern == "" {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-24 10:39:47 -04:00
|
|
|
// normalize absolute paths to paths relative to the context
|
|
|
|
// (taking care of '!' prefix)
|
|
|
|
invert := pattern[0] == '!'
|
|
|
|
if invert {
|
|
|
|
pattern = strings.TrimSpace(pattern[1:])
|
|
|
|
}
|
|
|
|
if len(pattern) > 0 {
|
|
|
|
pattern = filepath.Clean(pattern)
|
|
|
|
pattern = filepath.ToSlash(pattern)
|
|
|
|
if len(pattern) > 1 && pattern[0] == '/' {
|
|
|
|
pattern = pattern[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if invert {
|
|
|
|
pattern = "!" + pattern
|
|
|
|
}
|
|
|
|
|
2015-12-14 05:15:00 -05:00
|
|
|
excludes = append(excludes, pattern)
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error reading .dockerignore: %v", err)
|
|
|
|
}
|
|
|
|
return excludes, nil
|
|
|
|
}
|