Skip to content

Fix submodule parsing #32571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 5 additions & 25 deletions modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os/exec"
"strconv"
Expand Down Expand Up @@ -375,32 +374,13 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
if err != nil {
return nil, err
}

defer rd.Close()
scanner := bufio.NewScanner(rd)
c.submoduleCache = newObjectCache()
var ismodule bool
var path string
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "[submodule") {
ismodule = true
continue
}
if ismodule {
fields := strings.Split(scanner.Text(), "=")
k := strings.TrimSpace(fields[0])
if k == "path" {
path = strings.TrimSpace(fields[1])
} else if k == "url" {
c.submoduleCache.Set(path, &SubModule{path, strings.TrimSpace(fields[1])})
ismodule = false
}
}
}
if err = scanner.Err(); err != nil {
return nil, fmt.Errorf("GetSubModules scan: %w", err)
}

r := io.LimitReader(rd, 100*1024) // limit to 100KB
c.submoduleCache, err = parseSubmoduleContent(r)
if err != nil {
return nil, err
}
return c.submoduleCache, nil
}

Expand Down
45 changes: 45 additions & 0 deletions modules/git/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,48 @@ func Test_GetCommitBranchStart(t *testing.T) {
assert.NotEmpty(t, startCommitID)
assert.EqualValues(t, "9c9aef8dd84e02bc7ec12641deb4c930a7c30185", startCommitID)
}

func Test_parseSubmoduleContent(t *testing.T) {
submoduleFiles := []struct {
fileContent string
expectedPath string
expectedURL string
expectedBranch string
}{
{
fileContent: `[submodule "jakarta-servlet"]
url = ../../ALP-pool/jakarta-servlet
path = jakarta-servlet`,
expectedPath: "jakarta-servlet",
expectedURL: "../../ALP-pool/jakarta-servlet",
expectedBranch: "",
},
{
fileContent: `[submodule "jakarta-servlet"]
path = jakarta-servlet
url = ../../ALP-pool/jakarta-servlet`,
expectedPath: "jakarta-servlet",
expectedURL: "../../ALP-pool/jakarta-servlet",
expectedBranch: "",
},
{
fileContent: `[submodule "jakarta-servlet"]
path = jakarta-servlet
url = ../../ALP-pool/jakarta-servlet
branch = stable`,
expectedPath: "jakarta-servlet",
expectedURL: "../../ALP-pool/jakarta-servlet",
expectedBranch: "stable",
},
}
for _, kase := range submoduleFiles {
submodule, err := parseSubmoduleContent(strings.NewReader(kase.fileContent))
assert.NoError(t, err)
v, ok := submodule.Get(kase.expectedPath)
assert.True(t, ok)
subModule := v.(*SubModule)
assert.Equal(t, kase.expectedPath, subModule.Path)
assert.Equal(t, kase.expectedURL, subModule.URL)
assert.Equal(t, kase.expectedBranch, subModule.Branch)
}
}
72 changes: 70 additions & 2 deletions modules/git/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package git

import (
"bufio"
"fmt"
"io"
"net"
"net/url"
"path"
Expand All @@ -17,8 +19,74 @@ var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)

// SubModule submodule is a reference on git repository
type SubModule struct {
Name string
URL string
Path string
URL string
Branch string
}

// parseSubmoduleContent this is not a complete parse for gitmodules file, it only
// parses the url and path of submodules
func parseSubmoduleContent(r io.Reader) (*ObjectCache, error) {
var path, url, branch string
var state int // 0: find section, 1: find path and url
subModules := newObjectCache()
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())

// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
continue
}

// Section header [section]
if strings.HasPrefix(line, "[submodule") && strings.HasSuffix(line, "]") {
if path != "" && url != "" {
subModules.Set(path, &SubModule{
Path: path,
URL: url,
Branch: branch,
})
}
state = 1
path = ""
url = ""
branch = ""
continue
}

if state != 1 {
continue
}

parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
switch key {
case "path":
path = value
case "url":
url = value
case "branch":
branch = value
}
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading file: %w", err)
}
if path != "" && url != "" {
subModules.Set(path, &SubModule{
Path: path,
URL: url,
Branch: branch,
})
}

return subModules, nil
}

// SubModuleFile represents a file with submodule type.
Expand Down
Loading