forked from src-d/gitbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit_file_stats.go
114 lines (95 loc) · 2.78 KB
/
commit_file_stats.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package function
import (
"fmt"
"github.com/src-d/gitbase/internal/commitstats"
"github.com/src-d/go-mysql-server/sql"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// CommitFileStats calculates the diff stats of all files for a given commit.
// Vendored files are ignored in the output of this function.
type CommitFileStats struct {
Repository sql.Expression
From sql.Expression
To sql.Expression
}
// NewCommitFileStats creates a new COMMIT_FILE_STATS function.
func NewCommitFileStats(args ...sql.Expression) (sql.Expression, error) {
var f CommitFileStats
switch len(args) {
case 2:
f.Repository, f.To = args[0], args[1]
case 3:
f.Repository, f.From, f.To = args[0], args[1], args[2]
default:
return nil, sql.ErrInvalidArgumentNumber.New("COMMIT_FILE_STATS", "2 or 3", len(args))
}
return &f, nil
}
func (f *CommitFileStats) String() string {
if f.From == nil {
return fmt.Sprintf("commit_file_stats(%s, %s)", f.Repository, f.To)
}
return fmt.Sprintf("commit_file_stats(%s, %s, %s)", f.Repository, f.From, f.To)
}
// Type implements the Expression interface.
func (CommitFileStats) Type() sql.Type {
return sql.Array(sql.JSON)
}
// WithChildren implements the Expression interface.
func (f *CommitFileStats) WithChildren(children ...sql.Expression) (sql.Expression, error) {
expected := 2
if f.From != nil {
expected = 3
}
if len(children) != expected {
return nil, sql.ErrInvalidChildrenNumber.New(f, len(children), expected)
}
repo := children[0]
var from, to sql.Expression
if f.From != nil {
from = children[1]
to = children[2]
} else {
to = children[1]
}
return &CommitStats{repo, from, to}, nil
}
// Children implements the Expression interface.
func (f *CommitFileStats) Children() []sql.Expression {
if f.From == nil {
return []sql.Expression{f.Repository, f.To}
}
return []sql.Expression{f.Repository, f.From, f.To}
}
// IsNullable implements the Expression interface.
func (*CommitFileStats) IsNullable() bool {
return true
}
// Resolved implements the Expression interface.
func (f *CommitFileStats) Resolved() bool {
return f.Repository.Resolved() &&
f.To.Resolved() &&
(f.From == nil || f.From.Resolved())
}
// Eval implements the Expression interface.
func (f *CommitFileStats) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return evalStatsFunc(
ctx,
"commit_file_stats",
row,
f.Repository, f.From, f.To,
func(r *git.Repository, from, to *object.Commit) (interface{}, error) {
stats, err := commitstats.CalculateByFile(r, from, to)
if err != nil {
return nil, err
}
// Since the type is an array, it must be converted to []interface{}.
var result = make([]interface{}, len(stats))
for i, s := range stats {
result[i] = s
}
return result, nil
},
)
}