diff --git a/internal/git/git.go b/internal/git/git.go index 25e7d2a..4963a80 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -14,7 +14,8 @@ func init() { // gitmodulesParser parses .gitmodules files. type gitmodulesParser struct{} -var submoduleHeaderRegex = regexp.MustCompile(`^\[submodule\s+"([^"]+)"\]`) +// Section names are case-insensitive; the quoted subsection name is not. +var submoduleHeaderRegex = regexp.MustCompile(`(?i)^\[submodule\s+"([^"]+)"\]`) type submodule struct { name string @@ -45,7 +46,13 @@ func (p *gitmodulesParser) Parse(filename string, content []byte) (*core.Result, continue } - if current == nil { + // Any other section header ends the current submodule. Its keys belong + // to that section, not to the submodule that came before it. + if strings.HasPrefix(line, "[") { + if current != nil { + deps = append(deps, current.dependency()) + current = nil + } continue } @@ -56,7 +63,7 @@ func (p *gitmodulesParser) Parse(filename string, content []byte) (*core.Result, key := strings.ToLower(strings.TrimSpace(physical[:eq])) val, n, ok := configValue(s[start+eq+1:]) pos = start + eq + 1 + n - if !ok { + if !ok || current == nil { continue } switch key { diff --git a/internal/git/git_test.go b/internal/git/git_test.go index ffeae13..517ca5f 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -176,3 +176,70 @@ func TestGitmodulesBackslashBeforeTrailingSpace(t *testing.T) { t.Errorf("got %+v, want %+v", res.Dependencies, want) } } + +func TestGitmodulesMixedCaseHeader(t *testing.T) { + content := "[Submodule \"Lib/Foo\"]\n" + + "\tpath = lib/foo\n" + + "\turl = https://github.com/example/foo.git\n" + + "[SUBMODULE \"Lib/NoURL\"]\n" + res, err := (&gitmodulesParser{}).Parse(".gitmodules", []byte(content)) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := []core.Dependency{ + {Name: "github.com/example/foo", Scope: core.Runtime, Direct: true, + Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/example/foo.git"}}, + {Name: "Lib/NoURL", Scope: core.Runtime, Direct: true, NoPURL: true}, + } + if !reflect.DeepEqual(res.Dependencies, want) { + t.Errorf("got %+v, want %+v", res.Dependencies, want) + } +} + +func TestGitmodulesOtherSectionEndsSubmodule(t *testing.T) { + content := "[submodule \"a\"]\n" + + "\tpath = a\n" + + "[foo]\n" + + "\turl = https://github.com/example/other.git\n" + + "[submodule \"b\"]\n" + + "\tpath = b\n" + + "\turl = https://github.com/example/b.git\n" + res, err := (&gitmodulesParser{}).Parse(".gitmodules", []byte(content)) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := []core.Dependency{ + {Name: "a", Scope: core.Runtime, Direct: true, NoPURL: true}, + {Name: "github.com/example/b", Scope: core.Runtime, Direct: true, + Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/example/b.git"}}, + } + if !reflect.DeepEqual(res.Dependencies, want) { + t.Errorf("got %+v, want %+v", res.Dependencies, want) + } +} + +// A value continued with a backslash newline belongs to the key it started on, +// even when the continuation reads like a submodule header. +func TestGitmodulesContinuedForeignValueLooksLikeHeader(t *testing.T) { + content := "[submodule \"a\"]\n" + + "\tpath = a\n" + + "[foo]\n" + + "\tnote = continued\\\n" + + "[submodule \"fake\"]\n" + + "\turl = https://github.com/example/other.git\n" + + "[submodule \"b\"]\n" + + "\tpath = b\n" + + "\turl = https://github.com/example/b.git\n" + res, err := (&gitmodulesParser{}).Parse(".gitmodules", []byte(content)) + if err != nil { + t.Fatalf("Parse failed: %v", err) + } + want := []core.Dependency{ + {Name: "a", Scope: core.Runtime, Direct: true, NoPURL: true}, + {Name: "github.com/example/b", Scope: core.Runtime, Direct: true, + Source: core.Source{Kind: core.SourceGit, Value: "https://github.com/example/b.git"}}, + } + if !reflect.DeepEqual(res.Dependencies, want) { + t.Errorf("got %+v, want only the real submodules %+v", res.Dependencies, want) + } +}