From 145a26a2a2b9f5f8e5fe0e01f048b400bf721a6d Mon Sep 17 00:00:00 2001 From: kuba-- Date: Tue, 9 Apr 2019 16:23:43 +0200 Subject: [PATCH 1/3] Don't skip URLs for remotes table Signed-off-by: kuba-- --- common_test.go | 37 +++++++++++++++++++++++++++++++++++- remotes.go | 50 ++++++++++++++++++++++++++++--------------------- remotes_test.go | 5 +++-- 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/common_test.go b/common_test.go index 2a3fe933f..f809838a7 100644 --- a/common_test.go +++ b/common_test.go @@ -11,7 +11,7 @@ import ( "testing" "github.com/stretchr/testify/require" - "gopkg.in/src-d/go-billy-siva.v4" + sivafs "gopkg.in/src-d/go-billy-siva.v4" billy "gopkg.in/src-d/go-billy.v4" "gopkg.in/src-d/go-billy.v4/osfs" fixtures "gopkg.in/src-d/go-git-fixtures.v3" @@ -243,6 +243,41 @@ func testTableIndexIterClosed(t *testing.T, table sql.IndexableTable) { require.True(closed.Check()) } +func testTableIterators(t *testing.T, table sql.IndexableTable, columns []string) { + t.Helper() + + require := require.New(t) + ctx, closed := setupSivaCloseRepos(t, "_testdata") + + rows, _ := tableToRows(ctx, table) + expected := len(rows) + + iter, err := table.IndexKeyValues(ctx, columns) + require.NoError(err) + actual := 0 + for { + _, i, err := iter.Next() + if err != nil { + require.Equal(io.EOF, err) + break + } + for { + _, _, err := i.Next() + if err != nil { + require.Equal(io.EOF, err) + break + } + actual++ + } + + i.Close() + } + iter.Close() + require.True(closed.Check()) + + require.EqualValues(expected, actual) +} + func testTableIterClosed(t *testing.T, table sql.IndexableTable) { t.Helper() diff --git a/remotes.go b/remotes.go index 883bda89b..2558dbbb5 100644 --- a/remotes.go +++ b/remotes.go @@ -144,28 +144,25 @@ type remotesRowIter struct { } func (i *remotesRowIter) Next() (sql.Row, error) { - if i.remotePos >= len(i.remotes) { - return nil, io.EOF - } - - remote := i.remotes[i.remotePos] - config := remote.Config() - - if i.urlPos >= len(config.URLs) || i.urlPos >= len(config.Fetch) { - i.remotePos++ + for { if i.remotePos >= len(i.remotes) { return nil, io.EOF } - remote = i.remotes[i.remotePos] - config = remote.Config() - i.urlPos = 0 - } + remote := i.remotes[i.remotePos] + config := remote.Config() + + if i.urlPos >= len(config.URLs) && i.urlPos >= len(config.Fetch) { + i.remotePos++ + i.urlPos = 0 + continue + } - row := remoteToRow(i.repo.ID, config, i.urlPos) - i.urlPos++ + row := remoteToRow(i.repo.ID, config, i.urlPos) + i.urlPos++ - return row, nil + return row, nil + } } func (i *remotesRowIter) Close() error { @@ -177,13 +174,23 @@ func (i *remotesRowIter) Close() error { } func remoteToRow(repoID string, config *config.RemoteConfig, pos int) sql.Row { + url := "" + if pos < len(config.URLs) { + url = config.URLs[pos] + } + + fetch := "" + if pos < len(config.Fetch) { + fetch = config.Fetch[pos].String() + } + return sql.NewRow( repoID, config.Name, - config.URLs[pos], - config.URLs[pos], - config.Fetch[pos].String(), - config.Fetch[pos].String(), + url, + url, + fetch, + fetch, ) } @@ -256,7 +263,8 @@ func (i *remotesKeyValueIter) Next() ([]interface{}, []byte, error) { } cfg := i.remotes[i.pos].Config() - if i.urlPos >= len(cfg.URLs) { + if i.urlPos >= len(cfg.URLs) && i.urlPos >= len(cfg.Fetch) { + i.urlPos = 0 i.pos++ continue } diff --git a/remotes_test.go b/remotes_test.go index 5987fc816..a89cc6353 100644 --- a/remotes_test.go +++ b/remotes_test.go @@ -150,6 +150,7 @@ func TestRemotesIndexIterClosed(t *testing.T) { testTableIndexIterClosed(t, new(remotesTable)) } -func TestRemotesIterClosed(t *testing.T) { - testTableIterClosed(t, new(remotesTable)) +func TestRemotesIterators(t *testing.T) { + // columns names just for debugging + testTableIterators(t, new(remotesTable), []string{"remote_name", "remote_push_url"}) } From 5ccdcac875f56f0b28eef6c50f1cdacca3558415 Mon Sep 17 00:00:00 2001 From: kuba-- Date: Wed, 10 Apr 2019 09:56:02 +0200 Subject: [PATCH 2/3] Add more iterator tests for other tables Signed-off-by: kuba-- --- commits_test.go | 5 +++++ references_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/commits_test.go b/commits_test.go index b12082d3f..2cd2add12 100644 --- a/commits_test.go +++ b/commits_test.go @@ -304,3 +304,8 @@ func TestCommitsIndexIterClosed(t *testing.T) { func TestCommitsIterClosed(t *testing.T) { testTableIterClosed(t, new(commitsTable)) } + +func TestCommitsIterators(t *testing.T) { + // columns names just for debugging + testTableIterators(t, new(commitsTable), []string{"commit_hash", "commit_author_email"}) +} diff --git a/references_test.go b/references_test.go index da7105ddd..eb400e54f 100644 --- a/references_test.go +++ b/references_test.go @@ -140,3 +140,8 @@ func TestReferencesIndexIterClosed(t *testing.T) { func TestReferencesIterClosed(t *testing.T) { testTableIterClosed(t, new(referencesTable)) } + +func TestReferencesIterators(t *testing.T) { + // columns names just for debugging + testTableIterators(t, new(referencesTable), []string{"ref_name", "commit_hash"}) +} From 8d358e2e27924c3879215a0275da59a63e458181 Mon Sep 17 00:00:00 2001 From: kuba-- Date: Wed, 10 Apr 2019 10:22:05 +0200 Subject: [PATCH 3/3] return NULL if URL has no value Signed-off-by: kuba-- --- remotes.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/remotes.go b/remotes.go index 2558dbbb5..b1ef56df9 100644 --- a/remotes.go +++ b/remotes.go @@ -174,12 +174,12 @@ func (i *remotesRowIter) Close() error { } func remoteToRow(repoID string, config *config.RemoteConfig, pos int) sql.Row { - url := "" + var url interface{} if pos < len(config.URLs) { url = config.URLs[pos] } - fetch := "" + var fetch interface{} if pos < len(config.Fetch) { fetch = config.Fetch[pos].String() }