From bb4c6813306832a583dea12ec698385e5aa9854b Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 7 Nov 2026 23:29:08 +0100 Subject: [PATCH] Only recognize .gitmodules keys and section headers at the start of a line If a submodule's url contains "path=", for example "https://example.com/repo?path=other", lazygit takes "other" for the submodule's path and loses the url. Also, a commented-out section in .gitmodules shows up as a submodule. The regexes that parse .gitmodules aren't anchored, so they find a key or a section header anywhere in a line. Anchor them at the start of the line. Co-Authored-By: Claude Opus 5.5 --- pkg/commands/git_commands/submodule.go | 6 +++--- pkg/commands/git_commands/submodule_test.go | 11 ----------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/pkg/commands/git_commands/submodule.go b/pkg/commands/git_commands/submodule.go index 982f9416c..a31824e95 100644 --- a/pkg/commands/git_commands/submodule.go +++ b/pkg/commands/git_commands/submodule.go @@ -94,7 +94,7 @@ func parseGitmodules(reader io.Reader, parentModule *models.SubmoduleConfig) ([] for scanner.Scan() { line := scanner.Text() - if name, ok := firstMatch(line, `\[submodule "(.*)"\]`); ok { + if name, ok := firstMatch(line, `^\s*\[submodule "(.*)"\]`); ok { configs = append(configs, &models.SubmoduleConfig{ Name: name, ParentModule: parentModule, }) @@ -103,9 +103,9 @@ func parseGitmodules(reader io.Reader, parentModule *models.SubmoduleConfig) ([] if len(configs) > 0 { config := configs[len(configs)-1] - if path, ok := firstMatch(line, `\s*path\s*=\s*(.*)\s*`); ok { + if path, ok := firstMatch(line, `^\s*path\s*=\s*(.*)\s*`); ok { config.Path = path - } else if url, ok := firstMatch(line, `\s*url\s*=\s*(.*)\s*`); ok { + } else if url, ok := firstMatch(line, `^\s*url\s*=\s*(.*)\s*`); ok { config.Url = url } } diff --git a/pkg/commands/git_commands/submodule_test.go b/pkg/commands/git_commands/submodule_test.go index c1bf458c0..554d0175f 100644 --- a/pkg/commands/git_commands/submodule_test.go +++ b/pkg/commands/git_commands/submodule_test.go @@ -42,14 +42,9 @@ func TestParseGitmodules(t *testing.T) { "\tpath = sub", "\turl = https://example.com/repo?path=other", }, - /* EXPECTED: expected: []*models.SubmoduleConfig{ {Name: "sub", Path: "sub", Url: "https://example.com/repo?path=other"}, }, - ACTUAL: */ - expected: []*models.SubmoduleConfig{ - {Name: "sub", Path: "other"}, - }, }, { testName: "commented-out section", @@ -61,15 +56,9 @@ func TestParseGitmodules(t *testing.T) { "#\tpath = old", "#\turl = ../old", }, - /* EXPECTED: expected: []*models.SubmoduleConfig{ {Name: "sub", Path: "sub", Url: "../sub"}, }, - ACTUAL: */ - expected: []*models.SubmoduleConfig{ - {Name: "sub", Path: "sub", Url: "../sub"}, - {Name: "old", Path: "old", Url: "../old"}, - }, }, }