Skip to content

Commit 1c04f8f

Browse files
lunnyearl-warren
authored andcommitted
Fix submodule parsing
(cherry picked from commit 33850a8) This really is just the cherry pick of 407b6e6 which is the first commit of the pull request, the one with the change. The rest of the changes is a refactor that is unrelated to the bug fix. Conflicts: modules/git/commit_test.go trivial context conflict
1 parent bf520f5 commit 1c04f8f

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
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.
@@ -365,37 +367,32 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
365367
return nil, err
366368
}
367369

368-
rd, err := entry.Blob().DataAsync()
370+
content, err := entry.Blob().GetBlobContent(10 * 1024)
369371
if err != nil {
370372
return nil, err
371373
}
372374

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

398-
return c.submoduleCache, nil
395+
return submoduleCache, nil
399396
}
400397

401398
// GetSubModule get the sub module according entryname

modules/git/commit_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,33 @@ func TestParseCommitRenames(t *testing.T) {
369369
assert.Equal(t, testcase.renames, renames)
370370
}
371371
}
372+
373+
func Test_parseSubmoduleContent(t *testing.T) {
374+
submoduleFiles := []struct {
375+
fileContent string
376+
expectedPath string
377+
expectedURL string
378+
}{
379+
{
380+
fileContent: `[submodule "jakarta-servlet"]
381+
url = ../../ALP-pool/jakarta-servlet
382+
path = jakarta-servlet`,
383+
expectedPath: "jakarta-servlet",
384+
expectedURL: "../../ALP-pool/jakarta-servlet",
385+
},
386+
{
387+
fileContent: `[submodule "jakarta-servlet"]
388+
path = jakarta-servlet
389+
url = ../../ALP-pool/jakarta-servlet`,
390+
expectedPath: "jakarta-servlet",
391+
expectedURL: "../../ALP-pool/jakarta-servlet",
392+
},
393+
}
394+
for _, kase := range submoduleFiles {
395+
submodule, err := parseSubmoduleContent([]byte(kase.fileContent))
396+
require.NoError(t, err)
397+
v, ok := submodule.Get(kase.expectedPath)
398+
assert.True(t, ok)
399+
assert.Equal(t, kase.expectedURL, v)
400+
}
401+
}

0 commit comments

Comments
 (0)