Skip to content

Commit f98ba56

Browse files
authored
Merge pull request #220 from erizocosmico/fix/avoid-missing-ref-hashes
gitbase: skip hash references that do not point to commits
2 parents 4e19a4a + 9ec6256 commit f98ba56

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

iterator.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66
"strings"
77

8+
errors "gopkg.in/src-d/go-errors.v1"
89
git "gopkg.in/src-d/go-git.v4"
910
"gopkg.in/src-d/go-git.v4/plumbing"
1011
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
@@ -786,12 +787,21 @@ func (i *refCommitsIter) Advance() error {
786787

787788
if i.commits == nil {
788789
err := i.refs.Advance()
789-
if err == io.EOF {
790-
i.refs = nil
791-
return io.EOF
790+
if err != nil {
791+
if err == io.EOF {
792+
i.refs = nil
793+
return io.EOF
794+
}
795+
796+
return err
792797
}
793798

799+
_, err = resolveCommit(i.repo, i.refs.Ref().Hash())
794800
if err != nil {
801+
if errInvalidCommit.Is(err) {
802+
continue
803+
}
804+
795805
return err
796806
}
797807

@@ -882,17 +892,20 @@ func (i *refHeadCommitsIter) Advance() error {
882892
}
883893

884894
err := i.refs.Advance()
885-
if err == io.EOF {
886-
i.refs = nil
887-
return io.EOF
888-
}
889-
890895
if err != nil {
896+
if err == io.EOF {
897+
i.refs = nil
898+
return io.EOF
899+
}
900+
891901
return err
892902
}
893903

894-
i.commit, err = i.repo.CommitObject(i.refs.Ref().Hash())
904+
i.commit, err = resolveCommit(i.repo, i.refs.Ref().Hash())
895905
if err != nil {
906+
if errInvalidCommit.Is(err) {
907+
continue
908+
}
896909
return err
897910
}
898911

@@ -1591,3 +1604,21 @@ func evalFilters(ctx *sql.Context, row sql.Row, filters sql.Expression) (bool, e
15911604

15921605
return v.(bool), nil
15931606
}
1607+
1608+
var errInvalidCommit = errors.NewKind("invalid commit of type: %T")
1609+
1610+
func resolveCommit(repo *git.Repository, hash plumbing.Hash) (*object.Commit, error) {
1611+
obj, err := repo.Object(plumbing.AnyObject, hash)
1612+
if err != nil {
1613+
return nil, err
1614+
}
1615+
1616+
switch obj := obj.(type) {
1617+
case *object.Commit:
1618+
return obj, nil
1619+
case *object.Tag:
1620+
return resolveCommit(repo, obj.Target)
1621+
default:
1622+
return nil, errInvalidCommit.New(obj)
1623+
}
1624+
}

0 commit comments

Comments
 (0)