Skip to content

Commit 693d269

Browse files
GiteaBotwxiaoguang
andauthored
Fix submodule parsing when the gitmodules is missing (go-gitea#35109) (go-gitea#35118)
Backport go-gitea#35109 Co-authored-by: wxiaoguang <[email protected]>
1 parent 315f197 commit 693d269

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

modules/git/commit_info.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ type CommitInfo struct {
1515
func getCommitInfoSubmoduleFile(repoLink string, entry *TreeEntry, commit *Commit, treePathDir string) (*CommitSubmoduleFile, error) {
1616
fullPath := path.Join(treePathDir, entry.Name())
1717
submodule, err := commit.GetSubModule(fullPath)
18-
if submodule == nil || err != nil {
18+
if err != nil {
1919
return nil, err
2020
}
21+
if submodule == nil {
22+
// unable to find submodule from ".gitmodules" file
23+
return NewCommitSubmoduleFile(repoLink, fullPath, "", entry.ID.String()), nil
24+
}
2125
return NewCommitSubmoduleFile(repoLink, fullPath, submodule.URL, entry.ID.String()), nil
2226
}

modules/git/commit_info_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,14 @@ func TestEntries_GetCommitsInfo(t *testing.T) {
129129
require.NoError(t, err)
130130
cisf, err := getCommitInfoSubmoduleFile("/any/repo-link", tree, commit, "")
131131
require.NoError(t, err)
132-
assert.Nil(t, cisf)
132+
assert.Equal(t, &CommitSubmoduleFile{
133+
repoLink: "/any/repo-link",
134+
fullPath: "file1.txt",
135+
refURL: "",
136+
refID: "e2129701f1a4d54dc44f03c93bca0a2aec7c5449",
137+
}, cisf)
138+
// since there is no refURL, it means that the submodule info doesn't exist, so it won't have a web link
139+
assert.Nil(t, cisf.SubmoduleWebLinkTree(t.Context()))
133140
})
134141
}
135142

modules/git/commit_submodule_file.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ func NewCommitSubmoduleFile(repoLink, fullPath, refURL, refID string) *CommitSub
2929
return &CommitSubmoduleFile{repoLink: repoLink, fullPath: fullPath, refURL: refURL, refID: refID}
3030
}
3131

32+
// RefID returns the commit ID of the submodule, it returns empty string for nil receiver
3233
func (sf *CommitSubmoduleFile) RefID() string {
34+
if sf == nil {
35+
return ""
36+
}
3337
return sf.refID
3438
}
3539

3640
func (sf *CommitSubmoduleFile) getWebLinkInTargetRepo(ctx context.Context, moreLinkPath string) *SubmoduleWebLink {
37-
if sf == nil {
41+
if sf == nil || sf.refURL == "" {
3842
return nil
3943
}
4044
if strings.HasPrefix(sf.refURL, "../") {
@@ -53,14 +57,13 @@ func (sf *CommitSubmoduleFile) getWebLinkInTargetRepo(ctx context.Context, moreL
5357
}
5458

5559
// SubmoduleWebLinkTree tries to make the submodule's tree link in its own repo, it also works on "nil" receiver
60+
// It returns nil if the submodule does not have a valid URL or is nil
5661
func (sf *CommitSubmoduleFile) SubmoduleWebLinkTree(ctx context.Context, optCommitID ...string) *SubmoduleWebLink {
57-
if sf == nil {
58-
return nil
59-
}
60-
return sf.getWebLinkInTargetRepo(ctx, "/tree/"+util.OptionalArg(optCommitID, sf.refID))
62+
return sf.getWebLinkInTargetRepo(ctx, "/tree/"+util.OptionalArg(optCommitID, sf.RefID()))
6163
}
6264

6365
// SubmoduleWebLinkCompare tries to make the submodule's compare link in its own repo, it also works on "nil" receiver
66+
// It returns nil if the submodule does not have a valid URL or is nil
6467
func (sf *CommitSubmoduleFile) SubmoduleWebLinkCompare(ctx context.Context, commitID1, commitID2 string) *SubmoduleWebLink {
6568
return sf.getWebLinkInTargetRepo(ctx, "/compare/"+commitID1+"..."+commitID2)
6669
}

modules/git/commit_submodule_file_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
func TestCommitSubmoduleLink(t *testing.T) {
1313
assert.Nil(t, (*CommitSubmoduleFile)(nil).SubmoduleWebLinkTree(t.Context()))
1414
assert.Nil(t, (*CommitSubmoduleFile)(nil).SubmoduleWebLinkCompare(t.Context(), "", ""))
15+
assert.Nil(t, (&CommitSubmoduleFile{}).SubmoduleWebLinkTree(t.Context()))
16+
assert.Nil(t, (&CommitSubmoduleFile{}).SubmoduleWebLinkCompare(t.Context(), "", ""))
1517

1618
t.Run("GitHubRepo", func(t *testing.T) {
1719
sf := NewCommitSubmoduleFile("/any/repo-link", "full-path", "[email protected]:user/repo.git", "aaaa")

routers/web/repo/view_home.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,10 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
310310
}
311311

312312
func handleRepoViewSubmodule(ctx *context.Context, submodule *git.SubModule) {
313+
// TODO: it needs to use git.NewCommitSubmoduleFile and SubmoduleWebLinkTree to correctly handle relative paths
313314
submoduleRepoURL, err := giturl.ParseRepositoryURL(ctx, submodule.URL)
314315
if err != nil {
315-
HandleGitError(ctx, "prepareToRenderDirOrFile: ParseRepositoryURL", err)
316+
HandleGitError(ctx, "handleRepoViewSubmodule: ParseRepositoryURL", err)
316317
return
317318
}
318319
submoduleURL := giturl.MakeRepositoryWebLink(submoduleRepoURL)

services/repository/files/tree.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ func newTreeViewNodeFromEntry(ctx context.Context, repoLink string, renderedIcon
178178
} else if subModule != nil {
179179
submoduleFile := git.NewCommitSubmoduleFile(repoLink, node.FullPath, subModule.URL, entry.ID.String())
180180
webLink := submoduleFile.SubmoduleWebLinkTree(ctx)
181-
node.SubmoduleURL = webLink.CommitWebLink
181+
if webLink != nil {
182+
node.SubmoduleURL = webLink.CommitWebLink
183+
}
182184
}
183185
}
184186

0 commit comments

Comments
 (0)