Skip to content

Commit 342dffe

Browse files
committed
Fix submodule parsing
1 parent 0d5abd9 commit 342dffe

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

modules/git/commit.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717

1818
"code.gitea.io/gitea/modules/log"
1919
"code.gitea.io/gitea/modules/util"
20+
21+
"github.com/go-git/go-git/v5/config"
2022
)
2123

2224
// Commit represents a git commit.
@@ -371,37 +373,32 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
371373
return nil, err
372374
}
373375

374-
rd, err := entry.Blob().DataAsync()
376+
content, err := entry.Blob().GetBlobContent(10 * 1024)
375377
if err != nil {
376378
return nil, err
377379
}
378380

379-
defer rd.Close()
380-
scanner := bufio.NewScanner(rd)
381-
c.submoduleCache = newObjectCache()
382-
var ismodule bool
383-
var path string
384-
for scanner.Scan() {
385-
if strings.HasPrefix(scanner.Text(), "[submodule") {
386-
ismodule = true
387-
continue
388-
}
389-
if ismodule {
390-
fields := strings.Split(scanner.Text(), "=")
391-
k := strings.TrimSpace(fields[0])
392-
if k == "path" {
393-
path = strings.TrimSpace(fields[1])
394-
} else if k == "url" {
395-
c.submoduleCache.Set(path, &SubModule{path, strings.TrimSpace(fields[1])})
396-
ismodule = false
397-
}
398-
}
381+
c.submoduleCache, err = parseSubmoduleContent([]byte(content))
382+
if err != nil {
383+
return nil, err
399384
}
400-
if err = scanner.Err(); err != nil {
401-
return nil, fmt.Errorf("GetSubModules scan: %w", err)
385+
return c.submoduleCache, nil
386+
}
387+
388+
func parseSubmoduleContent(bs []byte) (*ObjectCache, error) {
389+
cfg := config.NewModules()
390+
if err := cfg.Unmarshal(bs); err != nil {
391+
return nil, err
392+
}
393+
submoduleCache := newObjectCache()
394+
if len(cfg.Submodules) == 0 {
395+
return nil, fmt.Errorf("no submodules found")
396+
}
397+
for _, subModule := range cfg.Submodules {
398+
submoduleCache.Set(subModule.Path, subModule.URL)
402399
}
403400

404-
return c.submoduleCache, nil
401+
return submoduleCache, nil
405402
}
406403

407404
// GetSubModule get the sub module according entryname

modules/git/commit_test.go

Lines changed: 31 additions & 1 deletion
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+
139139
iQGzBAABCgAdFiEE9HRrbqvYxPT8PXbefPSEkrowAa8FAmYGg7IACgkQfPSEkrow
140140
Aa9olwv+P0HhtCM6CRvlUmPaqswRsDPNR4i66xyXGiSxdI9V5oJL7HLiQIM7KrFR
141141
gizKa2COiGtugv8fE+TKqXKaJx6uJUJEjaBd8E9Af9PrAzjWj+A84lU6/PgPS8hq
@@ -362,3 +362,33 @@ func Test_GetCommitBranchStart(t *testing.T) {
362362
assert.NotEmpty(t, startCommitID)
363363
assert.EqualValues(t, "9c9aef8dd84e02bc7ec12641deb4c930a7c30185", startCommitID)
364364
}
365+
366+
func Test_parseSubmoduleContent(t *testing.T) {
367+
submoduleFiles := []struct {
368+
fileContent string
369+
expectedPath string
370+
expectedURL string
371+
}{
372+
{
373+
fileContent: `[submodule "jakarta-servlet"]
374+
url = ../../ALP-pool/jakarta-servlet
375+
path = jakarta-servlet`,
376+
expectedPath: "jakarta-servlet",
377+
expectedURL: "../../ALP-pool/jakarta-servlet",
378+
},
379+
{
380+
fileContent: `[submodule "jakarta-servlet"]
381+
path = jakarta-servlet
382+
url = ../../ALP-pool/jakarta-servlet`,
383+
expectedPath: "jakarta-servlet",
384+
expectedURL: "../../ALP-pool/jakarta-servlet",
385+
},
386+
}
387+
for _, kase := range submoduleFiles {
388+
submodule, err := parseSubmoduleContent([]byte(kase.fileContent))
389+
assert.NoError(t, err)
390+
v, ok := submodule.Get(kase.expectedPath)
391+
assert.True(t, ok)
392+
assert.Equal(t, kase.expectedURL, v)
393+
}
394+
}

0 commit comments

Comments
 (0)