fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

zoekt-git-index: ignore UTF-8 BOM in .gitmodules

This is causing most of our persistent indexing errors, and fixing it
here is a better solution than explicitly blocking the strange
repositories.

+49 -18
+13 -1
gitindex/submodule.go
··· 29 29 30 30 // ParseGitModules parses the contents of a .gitmodules file. 31 31 func ParseGitModules(content []byte) (map[string]*SubmoduleEntry, error) { 32 - dec := config.NewDecoder(bytes.NewBuffer(content)) 32 + buf := bytes.NewBuffer(content) 33 + 34 + // Handle the possibility that .gitmodules has a UTF-8 BOM, which would 35 + // otherwise break the scanner. 36 + // https://stackoverflow.com/a/21375405 37 + r, _, err := buf.ReadRune() 38 + if err != nil { 39 + return nil, err 40 + } 41 + if r != '\uFEFF' { 42 + buf.UnreadRune() 43 + } 44 + dec := config.NewDecoder(buf) 33 45 cfg := &config.Config{} 34 46 35 47 if err := dec.Decode(cfg); err != nil {
+36 -17
gitindex/submodule_test.go
··· 20 20 ) 21 21 22 22 func TestParseGitModules(t *testing.T) { 23 - testData := `[submodule "plugins/abc"] 24 - path = plugins/abc 25 - url = ../plugins/abc 26 - branch = .` 27 - 28 - got, err := ParseGitModules([]byte(testData)) 29 - if err != nil { 30 - t.Fatalf("ParseGitModules: %T", err) 23 + cases := []struct { 24 + data string 25 + want map[string]*SubmoduleEntry 26 + }{{ 27 + `[submodule "plugins/abc"] 28 + path = plugins/abc 29 + url = ../plugins/abc 30 + branch = .`, 31 + map[string]*SubmoduleEntry{ 32 + "plugins/abc": { 33 + Path: "plugins/abc", 34 + URL: "../plugins/abc", 35 + Branch: ".", 36 + }, 37 + }}, 38 + { 39 + "\uFEFF" + `[submodule "plugins/abc"] 40 + path = plugins/abc 41 + url = ../plugins/abc 42 + branch = .`, 43 + map[string]*SubmoduleEntry{ 44 + "plugins/abc": { 45 + Path: "plugins/abc", 46 + URL: "../plugins/abc", 47 + Branch: ".", 48 + }, 49 + }}, 31 50 } 32 51 33 - want := map[string]*SubmoduleEntry{ 34 - "plugins/abc": &SubmoduleEntry{ 35 - Path: "plugins/abc", 36 - URL: "../plugins/abc", 37 - Branch: ".", 38 - }, 39 - } 40 - if !reflect.DeepEqual(got, want) { 41 - t.Fatalf("got %v, want %v", got, want) 52 + for _, tc := range cases { 53 + got, err := ParseGitModules([]byte(tc.data)) 54 + if err != nil { 55 + t.Fatalf("ParseGitModules: %T", err) 56 + } 57 + 58 + if !reflect.DeepEqual(got, tc.want) { 59 + t.Fatalf("got %v, want %v", got, tc.want) 60 + } 42 61 } 43 62 }