Skip to content

Commit 01ace9e

Browse files
authored
git: Add blob table (#42)
1 parent e7b5677 commit 01ace9e

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

Diff for: git/blobs.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package git
2+
3+
import (
4+
"github.com/gitql/gitql/sql"
5+
6+
"gopkg.in/src-d/go-git.v4"
7+
)
8+
9+
type blobsRelation struct {
10+
r *git.Repository
11+
}
12+
13+
func newBlobsRelation(r *git.Repository) sql.PhysicalRelation {
14+
return &blobsRelation{r: r}
15+
}
16+
17+
func (blobsRelation) Resolved() bool {
18+
return true
19+
}
20+
21+
func (blobsRelation) Name() string {
22+
return blobsRelationName
23+
}
24+
25+
func (blobsRelation) Schema() sql.Schema {
26+
return sql.Schema{
27+
sql.Field{"hash", sql.String},
28+
sql.Field{"size", sql.BigInteger},
29+
}
30+
}
31+
32+
func (r *blobsRelation) TransformUp(f func(sql.Node) sql.Node) sql.Node {
33+
return f(r)
34+
}
35+
36+
func (r *blobsRelation) TransformExpressionsUp(f func(sql.Expression) sql.Expression) sql.Node {
37+
return r
38+
}
39+
40+
func (r blobsRelation) RowIter() (sql.RowIter, error) {
41+
bIter, err := r.r.Blobs()
42+
if err != nil {
43+
return nil, err
44+
}
45+
iter := &blobIter{i: bIter}
46+
return iter, nil
47+
}
48+
49+
func (blobsRelation) Children() []sql.Node {
50+
return []sql.Node{}
51+
}
52+
53+
type blobIter struct {
54+
i *git.BlobIter
55+
}
56+
57+
func (i *blobIter) Next() (sql.Row, error) {
58+
blob, err := i.i.Next()
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
return blobToRow(blob), nil
64+
}
65+
66+
func blobToRow(c *git.Blob) sql.Row {
67+
return sql.NewMemoryRow(
68+
c.Hash.String(),
69+
c.Size,
70+
)
71+
}

Diff for: git/blobs_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package git
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"gopkg.in/src-d/go-git.v4"
8+
"gopkg.in/src-d/go-git.v4/fixtures"
9+
)
10+
11+
func TestBlobsRelation(t *testing.T) {
12+
assert := assert.New(t)
13+
14+
f := fixtures.Basic().One()
15+
r, err := git.NewFilesystemRepository(f.DotGit().Base())
16+
assert.Nil(err)
17+
18+
db := NewDatabase("foo", r)
19+
assert.NotNil(db)
20+
21+
relations := db.Relations()
22+
rel, ok := relations[blobsRelationName]
23+
assert.True(ok)
24+
assert.NotNil(rel)
25+
assert.Equal(blobsRelationName, rel.Name())
26+
assert.Equal(0, len(rel.Children()))
27+
28+
iter, err := rel.RowIter()
29+
assert.Nil(err)
30+
assert.NotNil(iter)
31+
32+
row, err := iter.Next()
33+
assert.Nil(err)
34+
assert.NotNil(row)
35+
36+
fields := row.Fields()
37+
assert.NotNil(fields)
38+
assert.IsType("", fields[0])
39+
assert.IsType(int64(0), fields[1])
40+
}

Diff for: git/database.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
referencesRelationName = "references"
1111
commitsRelationName = "commits"
1212
tagsRelationName = "tags"
13+
blobsRelationName = "blobs"
1314
treeEntriesRelationName = "tree_entries"
1415
)
1516

@@ -19,6 +20,7 @@ type Database struct {
1920
tr sql.PhysicalRelation
2021
rr sql.PhysicalRelation
2122
ter sql.PhysicalRelation
23+
br sql.PhysicalRelation
2224
}
2325

2426
func NewDatabase(name string, r *git.Repository) sql.Database {
@@ -27,6 +29,7 @@ func NewDatabase(name string, r *git.Repository) sql.Database {
2729
cr: newCommitsRelation(r),
2830
rr: newReferencesRelation(r),
2931
tr: newTagsRelation(r),
32+
br: newBlobsRelation(r),
3033
ter: newTreeEntriesRelation(r),
3134
}
3235
}
@@ -40,6 +43,7 @@ func (d *Database) Relations() map[string]sql.PhysicalRelation {
4043
commitsRelationName: d.cr,
4144
tagsRelationName: d.tr,
4245
referencesRelationName: d.rr,
46+
blobsRelationName: d.br,
4347
treeEntriesRelationName: d.ter,
4448
}
4549
}

0 commit comments

Comments
 (0)