Skip to content

Commit 35a8d3b

Browse files
authored
Merge pull request #421 from erizocosmico/fix/siva-squash-refs-iter
gitbase: do not skip repo when it has no head in squash
2 parents 80a2662 + 41a5cf4 commit 35a8d3b

File tree

2 files changed

+89
-9
lines changed

2 files changed

+89
-9
lines changed

Diff for: squash_iterator.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,16 @@ func (i *squashRemoteIter) Advance() error {
220220
config = remote.Config()
221221
}
222222

223+
fetch := i.urlPos
224+
if fetch >= len(config.Fetch) {
225+
fetch = len(config.Fetch) - 1
226+
}
227+
223228
i.remote = &Remote{
224229
RepoID: i.repo.ID,
225230
Name: config.Name,
226231
URL: config.URLs[i.urlPos],
227-
Fetch: config.Fetch[i.urlPos].String(),
232+
Fetch: config.Fetch[fetch].String(),
228233
}
229234

230235
i.row = sql.NewRow(
@@ -344,11 +349,16 @@ func (i *squashRepoRemotesIter) Advance() error {
344349
config = remote.Config()
345350
}
346351

352+
fetch := i.urlPos
353+
if fetch >= len(config.Fetch) {
354+
fetch = len(config.Fetch) - 1
355+
}
356+
347357
i.remote = &Remote{
348358
RepoID: i.repos.Repository().ID,
349359
Name: config.Name,
350360
URL: config.URLs[i.urlPos],
351-
Fetch: config.Fetch[i.urlPos].String(),
361+
Fetch: config.Fetch[fetch].String(),
352362
}
353363

354364
i.urlPos++
@@ -481,13 +491,10 @@ func (i *squashRefIter) Advance() error {
481491
}
482492

483493
i.head, err = i.repo.Repo.Head()
484-
if err != nil {
485-
if err == plumbing.ErrReferenceNotFound || i.skipGitErrors {
486-
i.repo = nil
487-
continue
488-
}
489-
494+
if err == plumbing.ErrReferenceNotFound {
490495
logrus.WithField("repo", i.repo.ID).Debug("unable to get HEAD of repository")
496+
} else if err != nil && !i.skipGitErrors {
497+
return err
491498
}
492499
}
493500

@@ -636,7 +643,6 @@ func (i *squashRepoRefsIter) Advance() error {
636643

637644
logrus.WithField("repo", i.repos.Repository().ID).
638645
Debug("unable to get HEAD of repository")
639-
continue
640646
}
641647
}
642648

Diff for: squash_iterator_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitbase
22

33
import (
44
"context"
5+
"path/filepath"
56
"testing"
67

78
"github.com/stretchr/testify/require"
@@ -902,6 +903,79 @@ func setupWithIndex(
902903
return ctx, index, cleanup
903904
}
904905

906+
func TestRefsIterSiva(t *testing.T) {
907+
path := filepath.Join("_testdata", "05893125684f2d3943cd84a7ab2b75e53668fba1.siva")
908+
pool := NewRepositoryPool()
909+
require.NoError(t, pool.AddSivaFile(path))
910+
911+
session := NewSession(pool)
912+
ctx := sql.NewContext(context.Background(), sql.WithSession(session))
913+
914+
cases := []struct {
915+
name string
916+
iter ChainableIter
917+
}{
918+
{"all refs", NewAllRefsIter(nil, false)},
919+
{"repo refs", NewRepoRefsIter(NewAllReposIter(nil), nil, false)},
920+
}
921+
922+
expected := []sql.Row{
923+
{
924+
path,
925+
"refs/heads/HEAD/015da2f4-6d89-7ec8-5ac9-a38329ea875b",
926+
"dbfab055c70379219cbcf422f05316fdf4e1aed3",
927+
},
928+
{
929+
path,
930+
"refs/heads/master/015da2f4-6d89-7ec8-5ac9-a38329ea875b",
931+
"dbfab055c70379219cbcf422f05316fdf4e1aed3",
932+
},
933+
}
934+
935+
for _, tt := range cases {
936+
t.Run(tt.name, func(t *testing.T) {
937+
require := require.New(t)
938+
it, err := NewChainableRowIter(ctx, tt.iter)
939+
require.NoError(err)
940+
941+
rows, err := sql.RowIterToRows(it)
942+
require.NoError(err)
943+
944+
// remove all non-ref columns
945+
for i := range rows {
946+
rows[i] = rows[i][len(rows[i])-3:]
947+
}
948+
949+
require.ElementsMatch(expected, rows)
950+
})
951+
}
952+
953+
t.Run("remote refs", func(t *testing.T) {
954+
require := require.New(t)
955+
it, err := NewChainableRowIter(
956+
ctx,
957+
NewRemoteRefsIter(NewAllRemotesIter(nil), nil),
958+
)
959+
require.NoError(err)
960+
961+
rows, err := sql.RowIterToRows(it)
962+
require.NoError(err)
963+
964+
// remove all non-ref columns
965+
for i := range rows {
966+
rows[i] = rows[i][len(rows[i])-3:]
967+
}
968+
969+
expected := []sql.Row{
970+
expected[0], expected[1],
971+
expected[0], expected[1],
972+
expected[0], expected[1],
973+
}
974+
975+
require.ElementsMatch(expected, rows)
976+
})
977+
}
978+
905979
type lookup struct {
906980
values sql.IndexValueIter
907981
}

0 commit comments

Comments
 (0)