Skip to content

Commit 12905b9

Browse files
authored
Merge pull request #230 from jfontan/improvement/skip-git-errors
Add env var to skip go-git errors
2 parents 0bd1c13 + c86dad0 commit 12905b9

File tree

10 files changed

+911
-103
lines changed

10 files changed

+911
-103
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ SELECT hash, author_email, author_name FROM commits LIMIT 2;
5353

5454
### Environment variables
5555

56-
| Name | Description |
57-
|:-----------------------------|:--------------------------------------------------|
58-
| `BBLFSH_ENDPOINT` | bblfshd endpoint, default "127.0.0.1:9432" |
59-
| `GITBASE_BLOBS_MAX_SIZE` | maximum blob size to return in MiB, default 5 MiB |
60-
| `GITBASE_BLOBS_ALLOW_BINARY` | enable retrieval of binary blobs, default `false` |
61-
| `UNSTABLE_SQUASH_ENABLE` | **UNSTABLE** check *Unstable features* |
56+
| Name | Description |
57+
|:-----------------------------|:----------------------------------------------------|
58+
| `BBLFSH_ENDPOINT` | bblfshd endpoint, default "127.0.0.1:9432" |
59+
| `GITBASE_BLOBS_MAX_SIZE` | maximum blob size to return in MiB, default 5 MiB |
60+
| `GITBASE_BLOBS_ALLOW_BINARY` | enable retrieval of binary blobs, default `false` |
61+
| `UNSTABLE_SQUASH_ENABLE` | **UNSTABLE** check *Unstable features* |
62+
| `GITBASE_SKIP_GIT_ERRORS` | do not stop queries on git errors, default disabled |
6263

6364
## Tables
6465

cmd/gitbase/server.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import (
2121
// not be empty.
2222
var enableUnstableSquash = os.Getenv("UNSTABLE_SQUASH_ENABLE") != ""
2323

24+
// By default when gitbase encounters and error in a repository it stops
25+
// the query. With this parameter it won't complain and just skip those
26+
// rows or repositories.
27+
var skipGitErrors = os.Getenv("GITBASE_SKIP_GIT_ERRORS") != ""
28+
2429
type cmdServer struct {
2530
Verbose bool `short:"v" description:"Activates the verbose mode"`
2631

@@ -84,7 +89,9 @@ func (c *cmdServer) Execute(args []string) error {
8489
Auth: auth,
8590
},
8691
c.engine,
87-
gitbase.NewSessionBuilder(c.pool),
92+
gitbase.NewSessionBuilder(c.pool,
93+
gitbase.WithSkipGitErrors(skipGitErrors),
94+
),
8895
)
8996
if err != nil {
9097
return err

internal/function/commit_has_blob.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66

77
"github.com/hashicorp/golang-lru"
8+
"github.com/sirupsen/logrus"
89

910
"github.com/src-d/gitbase"
1011
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -40,10 +41,6 @@ func (CommitHasBlob) Type() sql.Type {
4041

4142
// Eval implements the Expression interface.
4243
func (f *CommitHasBlob) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
43-
s, ok := ctx.Session.(*gitbase.Session)
44-
if !ok {
45-
return nil, gitbase.ErrInvalidGitbaseSession.New(ctx.Session)
46-
}
4744

4845
commitHash, err := f.Left.Eval(ctx, row)
4946
if err != nil {
@@ -74,7 +71,7 @@ func (f *CommitHasBlob) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
7471
}
7572

7673
return f.commitHasBlob(
77-
s.Pool,
74+
ctx,
7875
plumbing.NewHash(commitHash.(string)),
7976
plumbing.NewHash(blob.(string)),
8077
)
@@ -85,25 +82,49 @@ type commitBlobKey struct {
8582
}
8683

8784
func (f *CommitHasBlob) commitHasBlob(
88-
pool *gitbase.RepositoryPool,
85+
ctx *sql.Context,
8986
commitHash, blob plumbing.Hash,
9087
) (bool, error) {
9188
if val, ok := f.cache.Get(commitBlobKey{commitHash, blob}); ok {
9289
return val.(bool), nil
9390
}
9491

95-
iter, err := pool.RepoIter()
92+
s, ok := ctx.Session.(*gitbase.Session)
93+
if !ok {
94+
return false, gitbase.ErrInvalidGitbaseSession.New(ctx.Session)
95+
}
96+
97+
log := logrus.WithFields(logrus.Fields{
98+
"function": "commit_hash_blob",
99+
"commit_hash": commitHash.String(),
100+
"blob": blob.String(),
101+
})
102+
103+
iter, err := s.Pool.RepoIter()
96104
if err != nil {
105+
log.WithField("error", err).Error("cannot create repository iterator")
97106
return false, err
98107
}
99108

100109
for {
110+
select {
111+
case <-ctx.Done():
112+
log.Debug("query canceled")
113+
return false, gitbase.ErrSessionCanceled.New()
114+
default:
115+
}
116+
101117
repository, err := iter.Next()
102118
if err == io.EOF {
103119
break
104120
}
105121

106122
if err != nil {
123+
log.WithField("error", err).Error("could not get repository")
124+
125+
if s.SkipGitErrors {
126+
continue
127+
}
107128
return false, err
108129
}
109130

@@ -113,17 +134,36 @@ func (f *CommitHasBlob) commitHasBlob(
113134
continue
114135
}
115136

137+
log = log.WithFields(logrus.Fields{
138+
"repo": repository.ID,
139+
})
140+
116141
if err != nil {
142+
logrus.WithField("error", err).Error("could not get commit")
143+
144+
if s.SkipGitErrors {
145+
continue
146+
}
117147
return false, err
118148
}
119149

120150
tree, err := commit.Tree()
121151
if err != nil {
152+
logrus.WithField("error", err).Error("could not get tree")
153+
154+
if s.SkipGitErrors {
155+
continue
156+
}
122157
return false, err
123158
}
124159

125160
contained, err := f.hashInTree(blob, commitHash, tree)
126161
if err != nil {
162+
logrus.WithField("error", err).Error("error searching hash in tree")
163+
164+
if s.SkipGitErrors {
165+
continue
166+
}
127167
return false, err
128168
}
129169

internal/function/commit_has_tree.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66

77
"github.com/hashicorp/golang-lru"
8+
"github.com/sirupsen/logrus"
89

910
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
1011
"gopkg.in/src-d/go-git.v4/plumbing/object"
@@ -49,13 +50,21 @@ func (f *CommitHasTree) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
4950
span, ctx := ctx.Span("gitbase.CommitHasTree")
5051
defer span.Finish()
5152

53+
log := logrus.WithFields(logrus.Fields{
54+
"function": "commit_hash_tree",
55+
"row": row,
56+
"left": f.Left.String(),
57+
"right": f.Right.String(),
58+
})
59+
5260
s, ok := ctx.Session.(*gitbase.Session)
5361
if !ok {
5462
return nil, gitbase.ErrInvalidGitbaseSession.New(ctx.Session)
5563
}
5664

5765
left, err := f.Left.Eval(ctx, row)
5866
if err != nil {
67+
log.WithField("error", err).Error("cannot eval left side")
5968
return nil, err
6069
}
6170

@@ -65,11 +74,13 @@ func (f *CommitHasTree) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
6574

6675
left, err = sql.Text.Convert(left)
6776
if err != nil {
77+
log.WithField("error", err).Error("cannot convert left side")
6878
return nil, err
6979
}
7080

7181
right, err := f.Right.Eval(ctx, row)
7282
if err != nil {
83+
log.WithField("error", err).Error("cannot eval right side")
7384
return nil, err
7485
}
7586

@@ -79,18 +90,25 @@ func (f *CommitHasTree) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
7990

8091
right, err = sql.Text.Convert(right)
8192
if err != nil {
93+
log.WithField("error", err).Error("cannot convert right side")
8294
return nil, err
8395
}
8496

8597
commitHash := plumbing.NewHash(left.(string))
8698
treeHash := plumbing.NewHash(right.(string))
8799

100+
log = log.WithFields(logrus.Fields{
101+
"commit_hash": commitHash.String(),
102+
"tree_hash": treeHash.String(),
103+
})
104+
88105
if val, ok := f.cache.Get(commitTreeKey{commitHash, treeHash}); ok {
89106
return val.(bool), nil
90107
}
91108

92109
iter, err := s.Pool.RepoIter()
93110
if err != nil {
111+
log.WithField("error", err).Error("cannot create repository iterator")
94112
return nil, err
95113
}
96114

@@ -101,6 +119,11 @@ func (f *CommitHasTree) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
101119
}
102120

103121
if err != nil {
122+
log.WithField("error", err).Error("could not get next repository")
123+
124+
if s.SkipGitErrors {
125+
continue
126+
}
104127
return nil, err
105128
}
106129

@@ -109,6 +132,18 @@ func (f *CommitHasTree) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
109132
continue
110133
}
111134

135+
if err != nil {
136+
log.WithFields(logrus.Fields{
137+
"repo": repo.ID,
138+
"error": err,
139+
}).Error("error searching commit in tree")
140+
141+
if s.SkipGitErrors {
142+
continue
143+
}
144+
return nil, err
145+
}
146+
112147
return ok, nil
113148
}
114149
}

0 commit comments

Comments
 (0)