Skip to content

Commit 81ec66c

Browse files
authored
Fix submodule parsing (#32571) (#32577)
A quick fix for #32568 Partially backport from #32571
1 parent 3661b14 commit 81ec66c

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

modules/git/commit.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,31 +377,43 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
377377
}
378378

379379
defer rd.Close()
380+
return configParseSubModules(rd)
381+
}
382+
383+
func configParseSubModules(rd io.Reader) (*ObjectCache, error) {
380384
scanner := bufio.NewScanner(rd)
381-
c.submoduleCache = newObjectCache()
382-
var ismodule bool
383-
var path string
385+
submoduleCache := newObjectCache()
386+
var subModule *SubModule
384387
for scanner.Scan() {
385-
if strings.HasPrefix(scanner.Text(), "[submodule") {
386-
ismodule = true
388+
line := strings.TrimSpace(scanner.Text())
389+
if strings.HasPrefix(line, "[") {
390+
if subModule != nil {
391+
submoduleCache.Set(subModule.Name, subModule)
392+
subModule = nil
393+
}
394+
if strings.HasPrefix(line, "[submodule") {
395+
subModule = &SubModule{}
396+
}
387397
continue
388398
}
389-
if ismodule {
390-
fields := strings.Split(scanner.Text(), "=")
399+
if subModule != nil {
400+
fields := strings.Split(line, "=")
391401
k := strings.TrimSpace(fields[0])
392402
if k == "path" {
393-
path = strings.TrimSpace(fields[1])
403+
subModule.Name = strings.TrimSpace(fields[1])
394404
} else if k == "url" {
395-
c.submoduleCache.Set(path, &SubModule{path, strings.TrimSpace(fields[1])})
396-
ismodule = false
405+
subModule.URL = strings.TrimSpace(fields[1])
397406
}
398407
}
399408
}
400-
if err = scanner.Err(); err != nil {
409+
if subModule != nil {
410+
submoduleCache.Set(subModule.Name, subModule)
411+
}
412+
if err := scanner.Err(); err != nil {
401413
return nil, fmt.Errorf("GetSubModules scan: %w", err)
402414
}
403415

404-
return c.submoduleCache, nil
416+
return submoduleCache, nil
405417
}
406418

407419
// GetSubModule get the sub module according entryname

modules/git/commit_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ author KN4CK3R <[email protected]> 1711702962 +0100
135135
committer KN4CK3R <[email protected]> 1711702962 +0100
136136
encoding ISO-8859-1
137137
gpgsig -----BEGIN PGP SIGNATURE-----
138-
138+
<SPACE>
139139
iQGzBAABCgAdFiEE9HRrbqvYxPT8PXbefPSEkrowAa8FAmYGg7IACgkQfPSEkrow
140140
Aa9olwv+P0HhtCM6CRvlUmPaqswRsDPNR4i66xyXGiSxdI9V5oJL7HLiQIM7KrFR
141141
gizKa2COiGtugv8fE+TKqXKaJx6uJUJEjaBd8E9Af9PrAzjWj+A84lU6/PgPS8hq
@@ -150,7 +150,7 @@ gpgsig -----BEGIN PGP SIGNATURE-----
150150
-----END PGP SIGNATURE-----
151151
152152
ISO-8859-1`
153-
153+
commitString = strings.ReplaceAll(commitString, "<SPACE>", " ")
154154
sha := &Sha1Hash{0xfe, 0xaf, 0x4b, 0xa6, 0xbc, 0x63, 0x5f, 0xec, 0x44, 0x2f, 0x46, 0xdd, 0xd4, 0x51, 0x24, 0x16, 0xec, 0x43, 0xc2, 0xc2}
155155
gitRepo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare"))
156156
assert.NoError(t, err)
@@ -362,3 +362,41 @@ func Test_GetCommitBranchStart(t *testing.T) {
362362
assert.NotEmpty(t, startCommitID)
363363
assert.EqualValues(t, "9c9aef8dd84e02bc7ec12641deb4c930a7c30185", startCommitID)
364364
}
365+
366+
func TestConfigSubModule(t *testing.T) {
367+
input := `
368+
[core]
369+
path = test
370+
371+
[submodule "submodule1"]
372+
path = path1
373+
url = https://gitea.io/foo/foo
374+
#branch = b1
375+
376+
[other1]
377+
branch = master
378+
379+
[submodule "submodule2"]
380+
path = path2
381+
url = https://gitea.io/bar/bar
382+
branch = b2
383+
384+
[other2]
385+
branch = main
386+
387+
[submodule "submodule3"]
388+
path = path3
389+
url = https://gitea.io/xxx/xxx
390+
`
391+
392+
subModules, err := configParseSubModules(strings.NewReader(input))
393+
assert.NoError(t, err)
394+
assert.Len(t, subModules.cache, 3)
395+
396+
sm1, _ := subModules.Get("path1")
397+
assert.Equal(t, &SubModule{Name: "path1", URL: "https://gitea.io/foo/foo"}, sm1)
398+
sm2, _ := subModules.Get("path2")
399+
assert.Equal(t, &SubModule{Name: "path2", URL: "https://gitea.io/bar/bar"}, sm2)
400+
sm3, _ := subModules.Get("path3")
401+
assert.Equal(t, &SubModule{Name: "path3", URL: "https://gitea.io/xxx/xxx"}, sm3)
402+
}

0 commit comments

Comments
 (0)