Fix submodule parsing

(cherry picked from commit 33850a83fe4ebd23a762a7aac81614c42e303bfa)

This really is just the cherry pick of 407b6e6dfc7ee9ebb8a16c7f1a786e4c24d0516e
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
This commit is contained in:
Lunny Xiao 2024-11-19 11:44:21 -08:00 committed by Earl Warren
parent ddabba5f89
commit 6482429091
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
2 changed files with 51 additions and 24 deletions

View file

@ -17,6 +17,8 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"github.com/go-git/go-git/v5/config"
) )
// Commit represents a git commit. // Commit represents a git commit.
@ -365,37 +367,32 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
return nil, err return nil, err
} }
rd, err := entry.Blob().DataAsync() content, err := entry.Blob().GetBlobContent(10 * 1024)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rd.Close() c.submoduleCache, err = parseSubmoduleContent([]byte(content))
scanner := bufio.NewScanner(rd) if err != nil {
c.submoduleCache = newObjectCache() return nil, err
var ismodule bool
var path string
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "[submodule") {
ismodule = true
continue
} }
if ismodule { return c.submoduleCache, nil
fields := strings.Split(scanner.Text(), "=") }
k := strings.TrimSpace(fields[0])
if k == "path" { func parseSubmoduleContent(bs []byte) (*ObjectCache, error) {
path = strings.TrimSpace(fields[1]) cfg := config.NewModules()
} else if k == "url" { if err := cfg.Unmarshal(bs); err != nil {
c.submoduleCache.Set(path, &SubModule{path, strings.TrimSpace(fields[1])}) return nil, err
ismodule = false
} }
submoduleCache := newObjectCache()
if len(cfg.Submodules) == 0 {
return nil, fmt.Errorf("no submodules found")
} }
} for _, subModule := range cfg.Submodules {
if err = scanner.Err(); err != nil { submoduleCache.Set(subModule.Path, subModule.URL)
return nil, fmt.Errorf("GetSubModules scan: %w", err)
} }
return c.submoduleCache, nil return submoduleCache, nil
} }
// GetSubModule get the sub module according entryname // GetSubModule get the sub module according entryname

View file

@ -369,3 +369,33 @@ func TestParseCommitRenames(t *testing.T) {
assert.Equal(t, testcase.renames, renames) assert.Equal(t, testcase.renames, renames)
} }
} }
func Test_parseSubmoduleContent(t *testing.T) {
submoduleFiles := []struct {
fileContent string
expectedPath string
expectedURL string
}{
{
fileContent: `[submodule "jakarta-servlet"]
url = ../../ALP-pool/jakarta-servlet
path = jakarta-servlet`,
expectedPath: "jakarta-servlet",
expectedURL: "../../ALP-pool/jakarta-servlet",
},
{
fileContent: `[submodule "jakarta-servlet"]
path = jakarta-servlet
url = ../../ALP-pool/jakarta-servlet`,
expectedPath: "jakarta-servlet",
expectedURL: "../../ALP-pool/jakarta-servlet",
},
}
for _, kase := range submoduleFiles {
submodule, err := parseSubmoduleContent([]byte(kase.fileContent))
require.NoError(t, err)
v, ok := submodule.Get(kase.expectedPath)
assert.True(t, ok)
assert.Equal(t, kase.expectedURL, v)
}
}