From 54b76a6fded50808631225459b2ead3bcf36dcce Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:38:45 +0800 Subject: [PATCH 1/5] git: end the submodule at the next section header A header for a section that is not a submodule left the parser pointing at the previous submodule, so every key after it was assigned there. Git reads those keys as belonging to that section and leaves the submodule untouched. Flush the current submodule when a line starts with '[' and is not a submodule header, then ignore keys until the next one. --- internal/git/git.go | 10 ++++++++++ internal/git/git_test.go | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/internal/git/git.go b/internal/git/git.go index 25e7d2a..924a5fc 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -45,6 +45,16 @@ func (p *gitmodulesParser) Parse(filename string, content []byte) (*core.Result, continue } + // 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 + } + if current == nil { continue } diff --git a/internal/git/git_test.go b/internal/git/git_test.go index ffeae13..d788d25 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -176,3 +176,25 @@ func TestGitmodulesBackslashBeforeTrailingSpace(t *testing.T) { 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) + } +} From 880f0b676a9af9280d9c3ae8a36e5ca664b22159 Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:22:20 +0800 Subject: [PATCH 2/5] git: match submodule headers regardless of case Section names are case-insensitive in git config, so a `[Submodule "x"]` header opens a submodule just like `[submodule "x"]`. Now that any `[` line ends the current submodule, a mixed-case header would otherwise be dropped entirely along with its keys. Make the header regex case-insensitive. The quoted subsection name stays case-sensitive, so it is still captured verbatim. --- internal/git/git.go | 5 ++++- internal/git/git_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/git/git.go b/internal/git/git.go index 924a5fc..86a9b89 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -14,7 +14,10 @@ func init() { // gitmodulesParser parses .gitmodules files. type gitmodulesParser struct{} -var submoduleHeaderRegex = regexp.MustCompile(`^\[submodule\s+"([^"]+)"\]`) +// Section names are case-insensitive in git config, so `[Submodule "..."]` is +// as valid as `[submodule "..."]`. The quoted subsection name is case-sensitive +// and is captured verbatim. +var submoduleHeaderRegex = regexp.MustCompile(`(?i)^\[submodule\s+"([^"]+)"\]`) type submodule struct { name string diff --git a/internal/git/git_test.go b/internal/git/git_test.go index d788d25..41da21f 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -177,6 +177,25 @@ func TestGitmodulesBackslashBeforeTrailingSpace(t *testing.T) { } } +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" + From d67606c3e7d5da665bb4041b49c210bafdc5620d Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:41:08 +0800 Subject: [PATCH 3/5] git: ignore a continued value that looks like a submodule header A value continued with a backslash newline can begin with a line that looks like a [submodule "..."] header. Outside a submodule the parser skipped such assignments without consuming their value, so the continuation was taken as a real header and a bogus submodule appeared. Consume the full value through configValue before skipping. --- internal/git/git.go | 8 ++++++++ internal/git/git_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/internal/git/git.go b/internal/git/git.go index 86a9b89..63235a4 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -59,6 +59,14 @@ func (p *gitmodulesParser) Parse(filename string, content []byte) (*core.Result, } if current == nil { + // A value continued with a backslash newline may continue with a + // line that looks like a submodule header. Consume the whole value + // so that line is not mistaken for the start of a submodule. + if eq := strings.IndexByte(physical, '='); eq >= 0 { + if _, n, ok := configValue(s[start+eq+1:]); ok { + pos = start + eq + 1 + n + } + } continue } diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 41da21f..517ca5f 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -217,3 +217,29 @@ func TestGitmodulesOtherSectionEndsSubmodule(t *testing.T) { 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) + } +} From 7e65b218d630c1a2d2513fd7008d0e851ab5bf9d Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:00:30 +0800 Subject: [PATCH 4/5] git: advance past rejected foreign values in one step --- internal/git/git.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/git/git.go b/internal/git/git.go index 63235a4..e1f463e 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -63,9 +63,8 @@ func (p *gitmodulesParser) Parse(filename string, content []byte) (*core.Result, // line that looks like a submodule header. Consume the whole value // so that line is not mistaken for the start of a submodule. if eq := strings.IndexByte(physical, '='); eq >= 0 { - if _, n, ok := configValue(s[start+eq+1:]); ok { - pos = start + eq + 1 + n - } + _, n, _ := configValue(s[start+eq+1:]) + pos = start + eq + 1 + n } continue } From ee7950d19bccad1979444de2fa2447d7d59abebe Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:31:31 +0800 Subject: [PATCH 5/5] git: consume every key's value in one place Drop the duplicated IndexByte/configValue/pos branch for the case with no open submodule: advance past the value first, then skip the key if the value is rejected or no submodule is open. Shorten the comment above submoduleHeaderRegex. --- internal/git/git.go | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/internal/git/git.go b/internal/git/git.go index e1f463e..4963a80 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -14,9 +14,7 @@ func init() { // gitmodulesParser parses .gitmodules files. type gitmodulesParser struct{} -// Section names are case-insensitive in git config, so `[Submodule "..."]` is -// as valid as `[submodule "..."]`. The quoted subsection name is case-sensitive -// and is captured verbatim. +// Section names are case-insensitive; the quoted subsection name is not. var submoduleHeaderRegex = regexp.MustCompile(`(?i)^\[submodule\s+"([^"]+)"\]`) type submodule struct { @@ -58,17 +56,6 @@ func (p *gitmodulesParser) Parse(filename string, content []byte) (*core.Result, continue } - if current == nil { - // A value continued with a backslash newline may continue with a - // line that looks like a submodule header. Consume the whole value - // so that line is not mistaken for the start of a submodule. - if eq := strings.IndexByte(physical, '='); eq >= 0 { - _, n, _ := configValue(s[start+eq+1:]) - pos = start + eq + 1 + n - } - continue - } - eq := strings.IndexByte(physical, '=') if eq < 0 { continue @@ -76,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 {