Skip to content

Commit adb668a

Browse files
committed
add tests
1 parent 52db4cb commit adb668a

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

modules/git/commit.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,17 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
377377
}
378378

379379
defer rd.Close()
380+
return configParseSubModules(rd)
381+
}
382+
383+
func configParseSubModules(rd io.Reader) (*ObjectCache, error) {
380384
scanner := bufio.NewScanner(rd)
381-
c.submoduleCache = newObjectCache()
385+
submoduleCache := newObjectCache()
382386
var subModule *SubModule
383387
for scanner.Scan() {
384388
if strings.HasPrefix(scanner.Text(), "[") {
385389
if subModule != nil {
386-
c.submoduleCache.Set(subModule.Name, subModule)
390+
submoduleCache.Set(subModule.Name, subModule)
387391
subModule = nil
388392
}
389393
if strings.HasPrefix(scanner.Text(), "[submodule") {
@@ -402,13 +406,13 @@ func (c *Commit) GetSubModules() (*ObjectCache, error) {
402406
}
403407
}
404408
if subModule != nil {
405-
c.submoduleCache.Set(subModule.Name, subModule)
409+
submoduleCache.Set(subModule.Name, subModule)
406410
}
407-
if err = scanner.Err(); err != nil {
411+
if err := scanner.Err(); err != nil {
408412
return nil, fmt.Errorf("GetSubModules scan: %w", err)
409413
}
410414

411-
return c.submoduleCache, nil
415+
return submoduleCache, nil
412416
}
413417

414418
// GetSubModule get the sub module according entryname

modules/git/commit_test.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ author KN4CK3R <admin@oldschoolhack.me> 1711702962 +0100
135135
committer KN4CK3R <admin@oldschoolhack.me> 1711702962 +0100
136136
encoding ISO-8859-1
137137
gpgsig -----BEGIN PGP SIGNATURE-----
138-
138+
<SPACE>
139139
iQGzBAABCgAdFiEE9HRrbqvYxPT8PXbefPSEkrowAa8FAmYGg7IACgkQfPSEkrow
140140
Aa9olwv+P0HhtCM6CRvlUmPaqswRsDPNR4i66xyXGiSxdI9V5oJL7HLiQIM7KrFR
141141
gizKa2COiGtugv8fE+TKqXKaJx6uJUJEjaBd8E9Af9PrAzjWj+A84lU6/PgPS8hq
@@ -150,7 +150,7 @@ gpgsig -----BEGIN PGP SIGNATURE-----
150150
-----END PGP SIGNATURE-----
151151
152152
ISO-8859-1`
153-
153+
commitString = strings.ReplaceAll(commitString, "<SPACE>", " ")
154154
sha := &Sha1Hash{0xfe, 0xaf, 0x4b, 0xa6, 0xbc, 0x63, 0x5f, 0xec, 0x44, 0x2f, 0x46, 0xdd, 0xd4, 0x51, 0x24, 0x16, 0xec, 0x43, 0xc2, 0xc2}
155155
gitRepo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare"))
156156
assert.NoError(t, err)
@@ -362,3 +362,41 @@ func Test_GetCommitBranchStart(t *testing.T) {
362362
assert.NotEmpty(t, startCommitID)
363363
assert.EqualValues(t, "9c9aef8dd84e02bc7ec12641deb4c930a7c30185", startCommitID)
364364
}
365+
366+
func TestConfigSubModule(t *testing.T) {
367+
input := `
368+
[core]
369+
path = test
370+
371+
[submodule "submodule1"]
372+
path = path1
373+
url = https://gitea.io/foo/foo
374+
#branch = b1
375+
376+
[other1]
377+
branch = master
378+
379+
[submodule "submodule2"]
380+
path = path2
381+
url = https://gitea.io/bar/bar
382+
branch = b2
383+
384+
[other2]
385+
branch = main
386+
387+
[submodule "submodule3"]
388+
path = path3
389+
url = https://gitea.io/xxx/xxx
390+
`
391+
392+
subModules, err := configParseSubModules(strings.NewReader(input))
393+
assert.NoError(t, err)
394+
assert.Len(t, subModules.cache, 3)
395+
396+
sm1, _ := subModules.Get("path1")
397+
assert.Equal(t, &SubModule{Name: "path1", URL: "https://gitea.io/foo/foo"}, sm1)
398+
sm2, _ := subModules.Get("path2")
399+
assert.Equal(t, &SubModule{Name: "path2", URL: "https://gitea.io/bar/bar"}, sm2)
400+
sm3, _ := subModules.Get("path3")
401+
assert.Equal(t, &SubModule{Name: "path3", URL: "https://gitea.io/xxx/xxx"}, sm3)
402+
}

0 commit comments

Comments
 (0)