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 <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-11-07 23:59:32 +01:00
co-authored by Claude Opus 5.5
parent aecd5e5bb4
commit bb4c681330
2 changed files with 3 additions and 14 deletions
+3 -3
View File
@@ -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
}
}
@@ -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"},
},
},
}