1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Skip UTF-8 BOM bytes from Dockerignore if exist

This fix tries to address issues related to #23221 where Dockerignore
may consists of UTF-8 BOM. This likely happens when Notepad
tries to save a file as UTF-8 in Windows.

This fix skips the UTF-8 BOM bytes from the beginning of the
Dockerignore if exists.

Additional tests has been added to cover the changes in this fix.

This fix is related to #23221 (UTF-8 BOM in Dockerfile).

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-06-03 07:26:36 -07:00
parent 678c80f925
commit ea86320fcc
2 changed files with 34 additions and 1 deletions

View file

@ -2,6 +2,7 @@ package dockerignore
import (
"bufio"
"bytes"
"fmt"
"io"
"path/filepath"
@ -18,10 +19,18 @@ func ReadAll(reader io.ReadCloser) ([]string, error) {
defer reader.Close()
scanner := bufio.NewScanner(reader)
var excludes []string
currentLine := 0
utf8bom := []byte{0xEF, 0xBB, 0xBF}
for scanner.Scan() {
scannedBytes := scanner.Bytes()
// We trim UTF8 BOM
if currentLine == 0 {
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
}
pattern := string(scannedBytes)
currentLine++
// Lines starting with # (comments) are ignored before processing
pattern := scanner.Text()
if strings.HasPrefix(pattern, "#") {
continue
}