Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,9 @@ func (m *Matcher) addPatterns(data []byte, dir, source string) {
if dir != "" {
prefix = strings.Split(dir, "/")
}
// git skips a UTF-8 byte order mark at the start of an ignore file
// (skip_utf8_bom in dir.c), so it doesn't become part of the first line.
data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))
lineNum := 0
for len(data) > 0 {
var raw []byte
Expand Down
35 changes: 35 additions & 0 deletions gitignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,41 @@ func TestMatchBlankLines(t *testing.T) {
}
}

func TestMatchUTF8BOMSkipped(t *testing.T) {
// git skips a UTF-8 byte order mark at the start of an ignore file
// (skip_utf8_bom in dir.c), so the first line is read as written.
const bom = "\xef\xbb\xbf"

tests := []struct {
name string
patterns string
path string
want bool
}{
{"first pattern", bom + "*.log\n", "debug.log", true},
{"first line is a comment", bom + "# comment\n", bom + "# comment", false},
{"pattern after a comment", bom + "# comment\nfoo\n", "foo", true},
{"only at the start of the file", "foo\n" + bom + "bar\n", "bar", false},
{"kept on a later line", "foo\n" + bom + "bar\n", bom + "bar", true},
{"only one is skipped", bom + bom + "bar\n", "bar", false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := setupMatcher(t, tt.patterns)
if got := m.Match(tt.path); got != tt.want {
t.Errorf("file: patterns %q, Match(%q) = %v, want %v", tt.patterns, tt.path, got, tt.want)
}

m = gitignore.New("")
m.AddPatterns([]byte(tt.patterns), "")
if got := m.Match(tt.path); got != tt.want {
t.Errorf("AddPatterns: patterns %q, Match(%q) = %v, want %v", tt.patterns, tt.path, got, tt.want)
}
})
}
}

// Verify edge cases against git check-ignore
func TestMatchEdgeCasesVsGitCheckIgnore(t *testing.T) {
tests := []struct {
Expand Down
2 changes: 2 additions & 0 deletions testdata/conformance/utf8-bom/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.log
build/
5 changes: 5 additions & 0 deletions testdata/conformance/utf8-bom/paths
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
debug.log
sub/debug.log
build/
build/out
keep.txt
Loading