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
13 changes: 10 additions & 3 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
67 changes: 67 additions & 0 deletions internal/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading