Skip to content

Commit fc4cb07

Browse files
authored
Fix submodule nil check (#35096)
Fix #35095
1 parent e1e4815 commit fc4cb07

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

modules/git/commit_info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ 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 err != nil {
18+
if submodule == nil || err != nil {
1919
return nil, err
2020
}
2121
return NewCommitSubmoduleFile(repoLink, fullPath, submodule.URL, entry.ID.String()), nil

modules/git/commit_info_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
const (
@@ -120,6 +121,16 @@ func TestEntries_GetCommitsInfo(t *testing.T) {
120121
defer clonedRepo1.Close()
121122

122123
testGetCommitsInfo(t, clonedRepo1)
124+
125+
t.Run("NonExistingSubmoduleAsNil", func(t *testing.T) {
126+
commit, err := bareRepo1.GetCommit("HEAD")
127+
require.NoError(t, err)
128+
tree, err := commit.GetTreeEntryByPath("file1.txt")
129+
require.NoError(t, err)
130+
cisf, err := getCommitInfoSubmoduleFile("/any/repo-link", tree, commit, "")
131+
require.NoError(t, err)
132+
assert.Nil(t, cisf)
133+
})
123134
}
124135

125136
func BenchmarkEntries_GetCommitsInfo(b *testing.B) {

modules/git/commit_submodule.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func (c *Commit) GetSubModules() (*ObjectCache[*SubModule], error) {
3535
return c.submoduleCache, nil
3636
}
3737

38-
// GetSubModule get the submodule according entry name
38+
// GetSubModule gets the submodule by the entry name.
39+
// It returns "nil, nil" if the submodule does not exist, caller should always remember to check the "nil"
3940
func (c *Commit) GetSubModule(entryName string) (*SubModule, error) {
4041
modules, err := c.GetSubModules()
4142
if err != nil {

0 commit comments

Comments
 (0)