Skip to content

Commit b31c52b

Browse files
committed
skip references pointing to objects that are not commits
Fixes #745 On all iterators where references are used only hash references are used. But we're not taking into account that hash references can point to any kind of object, not only commit objects, which are the only objects we care about for the references table. This fix was already applied on the new commits iterator that walks the history skipping unreachable commits, but it was not consistent with the rest of the iterators, which only check if the reference was a hash reference or not. Now this behavior is consistent in all iterators. `isIgnoredReference` function has been added to abstract all this logic away so if we ever need to change the logic of this we will only have to replace it in one place instead of being scattered all over the codebase. Signed-off-by: Miguel Molina <[email protected]>
1 parent f61ac19 commit b31c52b

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

commits.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,7 @@ func (i *commitIter) loadNextRef() (err error) {
230230
return err
231231
}
232232

233-
if i.ref.Type() != plumbing.HashReference {
234-
i.ref = nil
235-
continue
236-
}
237-
238-
obj, err := i.repo.Object(plumbing.AnyObject, i.ref.Hash())
233+
ignored, err := isIgnoredReference(i.repo.Repository, i.ref)
239234
if err != nil {
240235
if i.skipGitErrors {
241236
continue
@@ -244,7 +239,7 @@ func (i *commitIter) loadNextRef() (err error) {
244239
return err
245240
}
246241

247-
if obj.Type() != plumbing.CommitObject {
242+
if ignored {
248243
continue
249244
}
250245

integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func TestMissingHeadRefs(t *testing.T) {
594594

595595
rows, err := sql.RowIterToRows(iter)
596596
require.NoError(err)
597-
require.Len(rows, 56)
597+
require.Len(rows, 54)
598598
}
599599

600600
func BenchmarkQueries(b *testing.B) {

ref_commits.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,16 @@ func (i *refCommitsRowIter) next() (sql.Row, error) {
306306
return nil, err
307307
}
308308

309-
if ref.Type() != plumbing.HashReference {
309+
ignored, err := isIgnoredReference(i.repo.Repository, ref)
310+
if err != nil {
311+
if i.skipGitErrors {
312+
continue
313+
}
314+
315+
return nil, err
316+
}
317+
318+
if ignored {
310319
continue
311320
}
312321
} else {

references.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"strings"
88

9+
git "gopkg.in/src-d/go-git.v4"
910
"gopkg.in/src-d/go-mysql-server.v0/sql"
1011

1112
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -299,7 +300,16 @@ func (i *refRowIter) next() (sql.Row, error) {
299300
return nil, err
300301
}
301302

302-
if o.Type() != plumbing.HashReference {
303+
ignored, err := isIgnoredReference(i.repo.Repository, o)
304+
if err != nil {
305+
if i.skipGitErrors {
306+
continue
307+
}
308+
309+
return nil, err
310+
}
311+
312+
if ignored {
303313
continue
304314
}
305315

@@ -338,3 +348,16 @@ func referenceToRow(repositoryID string, c *plumbing.Reference) sql.Row {
338348
hash,
339349
)
340350
}
351+
352+
func isIgnoredReference(repo *git.Repository, ref *plumbing.Reference) (bool, error) {
353+
if ref.Type() != plumbing.HashReference {
354+
return true, nil
355+
}
356+
357+
obj, err := repo.Object(plumbing.AnyObject, ref.Hash())
358+
if err != nil {
359+
return false, err
360+
}
361+
362+
return obj.Type() != plumbing.CommitObject, nil
363+
}

squash_iterator.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,16 @@ func (i *squashRefIter) Advance() error {
474474
}
475475
}
476476

477-
if ref.Type() != plumbing.HashReference {
477+
ignored, err := isIgnoredReference(i.repo.Repository, ref)
478+
if err != nil {
479+
if i.skipGitErrors {
480+
continue
481+
}
482+
483+
return err
484+
}
485+
486+
if ignored {
478487
continue
479488
}
480489

@@ -715,11 +724,16 @@ func (i *squashRepoRefsIter) Advance() error {
715724
}
716725
}
717726

718-
if ref.Type() != plumbing.HashReference {
719-
logrus.WithFields(logrus.Fields{
720-
"type": ref.Type(),
721-
"ref": ref.Name(),
722-
}).Debug("ignoring reference, it's not a hash reference")
727+
ignored, err := isIgnoredReference(i.repos.Repository().Repository, ref)
728+
if err != nil {
729+
if i.skipGitErrors {
730+
continue
731+
}
732+
733+
return err
734+
}
735+
736+
if ignored {
723737
continue
724738
}
725739

@@ -859,7 +873,16 @@ func (i *squashRemoteRefsIter) Advance() error {
859873
}
860874
}
861875

862-
if ref.Type() != plumbing.HashReference {
876+
ignored, err := isIgnoredReference(i.Repository().Repository, ref)
877+
if err != nil {
878+
if i.skipGitErrors {
879+
continue
880+
}
881+
882+
return err
883+
}
884+
885+
if ignored {
863886
continue
864887
}
865888

0 commit comments

Comments
 (0)