-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree_entries.go
100 lines (80 loc) · 1.77 KB
/
tree_entries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gitquery
import (
"strconv"
"gopkg.in/sqle/sqle.v0/sql"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
type treeEntriesTable struct {
r *git.Repository
}
func newTreeEntriesTable(r *git.Repository) sql.Table {
return &treeEntriesTable{r: r}
}
func (treeEntriesTable) Resolved() bool {
return true
}
func (treeEntriesTable) Name() string {
return treeEntriesTableName
}
func (treeEntriesTable) Schema() sql.Schema {
return sql.Schema{
{Name: "tree_hash", Type: sql.String, Nullable: false},
{Name: "entry_hash", Type: sql.String, Nullable: false},
{Name: "mode", Type: sql.String, Nullable: false},
{Name: "name", Type: sql.String, Nullable: false},
}
}
func (r *treeEntriesTable) TransformUp(f func(sql.Node) sql.Node) sql.Node {
return f(r)
}
func (r *treeEntriesTable) TransformExpressionsUp(f func(sql.Expression) sql.Expression) sql.Node {
return r
}
func (r treeEntriesTable) RowIter() (sql.RowIter, error) {
cIter, err := r.r.TreeObjects()
if err != nil {
return nil, err
}
iter := &treeEntryIter{i: cIter}
return iter, nil
}
func (treeEntriesTable) Children() []sql.Node {
return []sql.Node{}
}
type treeEntryIter struct {
i *object.TreeIter
t *object.Tree
ei int
}
func (i *treeEntryIter) Next() (sql.Row, error) {
for {
if i.t == nil {
tree, err := i.i.Next()
if err != nil {
return nil, err
}
i.t = tree
i.ei = 0
}
if i.ei >= len(i.t.Entries) {
i.t = nil
continue
}
e := i.t.Entries[i.ei]
i.ei++
return treeEntryToRow(i.t, e), nil
}
}
func (i *treeEntryIter) Close() error {
i.i.Close()
return nil
}
func treeEntryToRow(t *object.Tree, e object.TreeEntry) sql.Row {
return sql.NewRow(
t.ID().String(),
e.Hash.String(),
strconv.FormatInt(int64(e.Mode), 8),
e.Name,
)
}