Skip to content

Commit 47c8d30

Browse files
committed
fix
1 parent dc7ff6c commit 47c8d30

20 files changed

+491
-452
lines changed

modules/git/commit.go

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Commit struct {
2828
Signature *CommitSignature
2929

3030
Parents []ObjectID // ID strings
31-
submoduleCache *ObjectCache
31+
submoduleCache *ObjectCache[*SubModule]
3232
}
3333

3434
// CommitSignature represents a git commit signature part.
@@ -356,50 +356,6 @@ func (c *Commit) GetFileContent(filename string, limit int) (string, error) {
356356
return string(bytes), nil
357357
}
358358

359-
// GetSubModules get all the sub modules of current revision git tree
360-
func (c *Commit) GetSubModules() (*ObjectCache, error) {
361-
if c.submoduleCache != nil {
362-
return c.submoduleCache, nil
363-
}
364-
365-
entry, err := c.GetTreeEntryByPath(".gitmodules")
366-
if err != nil {
367-
if _, ok := err.(ErrNotExist); ok {
368-
return nil, nil
369-
}
370-
return nil, err
371-
}
372-
373-
rd, err := entry.Blob().DataAsync()
374-
if err != nil {
375-
return nil, err
376-
}
377-
defer rd.Close()
378-
379-
r := io.LimitReader(rd, 100*1024) // limit to 100KB
380-
c.submoduleCache, err = parseSubmoduleContent(r)
381-
if err != nil {
382-
return nil, err
383-
}
384-
return c.submoduleCache, nil
385-
}
386-
387-
// GetSubModule get the sub module according entryname
388-
func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
389-
modules, err := c.GetSubModules()
390-
if err != nil {
391-
return nil, err
392-
}
393-
394-
if modules != nil {
395-
module, has := modules.Get(entryname)
396-
if has {
397-
return module.(*SubModule), nil
398-
}
399-
}
400-
return nil, nil
401-
}
402-
403359
// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
404360
func (c *Commit) GetBranchName() (string, error) {
405361
cmd := NewCommand(c.repo.Ctx, "name-rev")

modules/git/commit_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ package git
77
type CommitInfo struct {
88
Entry *TreeEntry
99
Commit *Commit
10-
SubModuleFile *SubModuleFile
10+
SubModuleFile *CommitSubModuleFile
1111
}

modules/git/commit_info_gogit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
7171
commitsInfo[i].Commit = entryCommit
7272
}
7373

74-
// If the entry if a submodule add a submodule file for this
74+
// If the entry is a submodule add a submodule file for this
7575
if entry.IsSubModule() {
7676
subModuleURL := ""
7777
var fullPath string
@@ -85,7 +85,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
8585
} else if subModule != nil {
8686
subModuleURL = subModule.URL
8787
}
88-
subModuleFile := NewSubModuleFile(commitsInfo[i].Commit, subModuleURL, entry.ID.String())
88+
subModuleFile := NewCommitSubModuleFile(subModuleURL, entry.ID.String())
8989
commitsInfo[i].SubModuleFile = subModuleFile
9090
}
9191
}

modules/git/commit_info_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath
7979
} else if subModule != nil {
8080
subModuleURL = subModule.URL
8181
}
82-
subModuleFile := NewSubModuleFile(commitsInfo[i].Commit, subModuleURL, entry.ID.String())
82+
subModuleFile := NewCommitSubModuleFile(subModuleURL, entry.ID.String())
8383
commitsInfo[i].SubModuleFile = subModuleFile
8484
}
8585
}

modules/git/commit_submodule.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package git
5+
6+
// GetSubModules get all the submodules of current revision git tree
7+
func (c *Commit) GetSubModules() (*ObjectCache[*SubModule], error) {
8+
if c.submoduleCache != nil {
9+
return c.submoduleCache, nil
10+
}
11+
12+
entry, err := c.GetTreeEntryByPath(".gitmodules")
13+
if err != nil {
14+
if _, ok := err.(ErrNotExist); ok {
15+
return nil, nil
16+
}
17+
return nil, err
18+
}
19+
20+
rd, err := entry.Blob().DataAsync()
21+
if err != nil {
22+
return nil, err
23+
}
24+
defer rd.Close()
25+
26+
// at the moment we do not strictly limit the size of the .gitmodules file because some users would have huge .gitmodules files (>1MB)
27+
c.submoduleCache, err = configParseSubModules(rd)
28+
if err != nil {
29+
return nil, err
30+
}
31+
return c.submoduleCache, nil
32+
}
33+
34+
// GetSubModule get the submodule according entry name
35+
func (c *Commit) GetSubModule(entryName string) (*SubModule, error) {
36+
modules, err := c.GetSubModules()
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
if modules != nil {
42+
if module, has := modules.Get(entryName); has {
43+
return module, nil
44+
}
45+
}
46+
return nil, nil
47+
}

modules/git/submodule.go renamed to modules/git/commit_submodule_file.go

Lines changed: 8 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
package git
66

77
import (
8-
"bufio"
98
"fmt"
10-
"io"
119
"net"
1210
"net/url"
1311
"path"
@@ -17,90 +15,15 @@ import (
1715

1816
var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)
1917

20-
// SubModule submodule is a reference on git repository
21-
type SubModule struct {
22-
Path string
23-
URL string
24-
Branch string
25-
}
26-
27-
// parseSubmoduleContent this is not a complete parse for gitmodules file, it only
28-
// parses the url and path of submodules
29-
func parseSubmoduleContent(r io.Reader) (*ObjectCache, error) {
30-
var path, url, branch string
31-
var state int // 0: find section, 1: find path and url
32-
subModules := newObjectCache()
33-
scanner := bufio.NewScanner(r)
34-
for scanner.Scan() {
35-
line := strings.TrimSpace(scanner.Text())
36-
37-
// Skip empty lines and comments
38-
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
39-
continue
40-
}
41-
42-
// Section header [section]
43-
if strings.HasPrefix(line, "[submodule") && strings.HasSuffix(line, "]") {
44-
if path != "" && url != "" {
45-
subModules.Set(path, &SubModule{
46-
Path: path,
47-
URL: url,
48-
Branch: branch,
49-
})
50-
}
51-
state = 1
52-
path = ""
53-
url = ""
54-
branch = ""
55-
continue
56-
}
57-
58-
if state != 1 {
59-
continue
60-
}
61-
62-
parts := strings.SplitN(line, "=", 2)
63-
if len(parts) != 2 {
64-
continue
65-
}
66-
key := strings.TrimSpace(parts[0])
67-
value := strings.TrimSpace(parts[1])
68-
switch key {
69-
case "path":
70-
path = value
71-
case "url":
72-
url = value
73-
case "branch":
74-
branch = value
75-
}
76-
}
77-
78-
if err := scanner.Err(); err != nil {
79-
return nil, fmt.Errorf("error reading file: %w", err)
80-
}
81-
if path != "" && url != "" {
82-
subModules.Set(path, &SubModule{
83-
Path: path,
84-
URL: url,
85-
Branch: branch,
86-
})
87-
}
88-
89-
return subModules, nil
90-
}
91-
92-
// SubModuleFile represents a file with submodule type.
93-
type SubModuleFile struct {
94-
*Commit
95-
18+
// CommitSubModuleFile represents a file with submodule type.
19+
type CommitSubModuleFile struct {
9620
refURL string
9721
refID string
9822
}
9923

100-
// NewSubModuleFile create a new submodule file
101-
func NewSubModuleFile(c *Commit, refURL, refID string) *SubModuleFile {
102-
return &SubModuleFile{
103-
Commit: c,
24+
// NewCommitSubModuleFile create a new submodule file
25+
func NewCommitSubModuleFile(refURL, refID string) *CommitSubModuleFile {
26+
return &CommitSubModuleFile{
10427
refURL: refURL,
10528
refID: refID,
10629
}
@@ -177,11 +100,12 @@ func getRefURL(refURL, urlPrefix, repoFullName, sshDomain string) string {
177100
}
178101

179102
// RefURL guesses and returns reference URL.
180-
func (sf *SubModuleFile) RefURL(urlPrefix, repoFullName, sshDomain string) string {
103+
// FIXME: template passes AppURL as urlPrefix, it needs to figure out the correct approach (no hard-coded AppURL anymore)
104+
func (sf *CommitSubModuleFile) RefURL(urlPrefix, repoFullName, sshDomain string) string {
181105
return getRefURL(sf.refURL, urlPrefix, repoFullName, sshDomain)
182106
}
183107

184108
// RefID returns reference ID.
185-
func (sf *SubModuleFile) RefID() string {
109+
func (sf *CommitSubModuleFile) RefID() string {
186110
return sf.refID
187111
}

modules/git/submodule_test.go renamed to modules/git/commit_submodule_file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12-
func TestGetRefURL(t *testing.T) {
12+
func TestCommitSubModuleFileGetRefURL(t *testing.T) {
1313
kases := []struct {
1414
refURL string
1515
prefixURL string

modules/git/commit_test.go

Lines changed: 1 addition & 46 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+
139139
iQGzBAABCgAdFiEE9HRrbqvYxPT8PXbefPSEkrowAa8FAmYGg7IACgkQfPSEkrow
140140
Aa9olwv+P0HhtCM6CRvlUmPaqswRsDPNR4i66xyXGiSxdI9V5oJL7HLiQIM7KrFR
141141
gizKa2COiGtugv8fE+TKqXKaJx6uJUJEjaBd8E9Af9PrAzjWj+A84lU6/PgPS8hq
@@ -362,48 +362,3 @@ 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-
expectedBranch string
372-
}{
373-
{
374-
fileContent: `[submodule "jakarta-servlet"]
375-
url = ../../ALP-pool/jakarta-servlet
376-
path = jakarta-servlet`,
377-
expectedPath: "jakarta-servlet",
378-
expectedURL: "../../ALP-pool/jakarta-servlet",
379-
expectedBranch: "",
380-
},
381-
{
382-
fileContent: `[submodule "jakarta-servlet"]
383-
path = jakarta-servlet
384-
url = ../../ALP-pool/jakarta-servlet`,
385-
expectedPath: "jakarta-servlet",
386-
expectedURL: "../../ALP-pool/jakarta-servlet",
387-
expectedBranch: "",
388-
},
389-
{
390-
fileContent: `[submodule "jakarta-servlet"]
391-
path = jakarta-servlet
392-
url = ../../ALP-pool/jakarta-servlet
393-
branch = stable`,
394-
expectedPath: "jakarta-servlet",
395-
expectedURL: "../../ALP-pool/jakarta-servlet",
396-
expectedBranch: "stable",
397-
},
398-
}
399-
for _, kase := range submoduleFiles {
400-
submodule, err := parseSubmoduleContent(strings.NewReader(kase.fileContent))
401-
assert.NoError(t, err)
402-
v, ok := submodule.Get(kase.expectedPath)
403-
assert.True(t, ok)
404-
subModule := v.(*SubModule)
405-
assert.Equal(t, kase.expectedPath, subModule.Path)
406-
assert.Equal(t, kase.expectedURL, subModule.URL)
407-
assert.Equal(t, kase.expectedBranch, subModule.Branch)
408-
}
409-
}

0 commit comments

Comments
 (0)