-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathcommit_diff.go
More file actions
209 lines (190 loc) · 5.43 KB
/
commit_diff.go
File metadata and controls
209 lines (190 loc) · 5.43 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/julien040/anyquery/rpc"
"github.com/julien040/anyquery/rpc/helper"
)
// A constructor to create a new table instance
// This function is called everytime a new connection is made to the plugin
//
// It should return a new table instance, the database schema and if there is an error
func commit_diffCreator(args rpc.TableCreatorArgs) (rpc.Table, *rpc.DatabaseSchema, error) {
return &commit_diffTable{}, &rpc.DatabaseSchema{
PrimaryKey: 1,
Columns: []rpc.DatabaseSchemaColumn{
{
Name: "repository",
Type: rpc.ColumnTypeString,
IsParameter: true,
IsRequired: true,
Description: "The path to the repository. Can be a local path (e.g. /path/to/repo) or a URL (e.g. https://github.com/julien040/anyquery.git)",
},
{
Name: "hash",
Type: rpc.ColumnTypeString,
Description: "The hash of the commit",
},
{
Name: "author_name",
Type: rpc.ColumnTypeString,
Description: "The name of the author of the commit",
},
{
Name: "author_email",
Type: rpc.ColumnTypeString,
Description: "The email of the author of the commit",
},
{
Name: "author_date",
Type: rpc.ColumnTypeString,
Description: "The date when the commit was authored",
},
{
Name: "committer_name",
Type: rpc.ColumnTypeString,
Description: "The name of the committer of the commit",
},
{
Name: "committer_email",
Type: rpc.ColumnTypeString,
Description: "The email of the committer of the commit",
},
{
Name: "committer_date",
Type: rpc.ColumnTypeString,
Description: "The date when the commit was committed",
},
{
Name: "message",
Type: rpc.ColumnTypeString,
Description: "The content of the message commit (title + body)",
},
{
Name: "file_name",
Type: rpc.ColumnTypeString,
Description: "The path of a file modified in the commit. One row per file modified per commit",
},
{
Name: "addition",
Type: rpc.ColumnTypeInt,
Description: "The number of lines added in the file compared to the previous commit",
},
{
Name: "deletion",
Type: rpc.ColumnTypeInt,
Description: "The number of lines deleted in the file compared to the previous commit",
},
{
Name: "parents",
Type: rpc.ColumnTypeString,
Description: "A JSON array of the hashes of the parent commits",
},
},
}, nil
}
type commit_diffTable struct {
}
type commit_diffCursor struct {
iter object.CommitIter
iterExhausted bool
repository *git.Repository
alreadyVisited map[string]bool
}
// Return a slice of rows that will be returned to Anyquery and filtered.
// The second return value is true if the cursor has no more rows to return
//
// The constraints are used for optimization purposes to "pre-filter" the rows
// If the rows returned don't match the constraints, it's not an issue. Anyquery will filter them out
func (t *commit_diffCursor) Query(constraints rpc.QueryConstraint) ([][]interface{}, bool, error) {
// Open the repository if it's not already opened
if t.repository == nil {
repoPath := ""
for _, c := range constraints.Columns {
if c.ColumnID == 0 {
if parsed, ok := c.Value.(string); ok {
repoPath = parsed
}
}
}
if repoPath == "" {
return nil, true, fmt.Errorf("a repository of type string is required")
}
repo, err := openRepository(repoPath)
if err != nil {
return nil, true, err
}
t.repository = repo
}
// Create the iterator if it's not already created
if t.iter == nil && !t.iterExhausted {
iter, err := t.repository.CommitObjects()
if err != nil {
return nil, true, fmt.Errorf("error getting commits: %s", err)
}
t.iter = iter
}
// Get the next 128 commits and return them
rows := make([][]interface{}, 0, 128)
for i := 0; i < 128; i++ {
commit, err := t.iter.Next()
if err != nil {
if err.Error() == "EOF" {
t.iterExhausted = true
break
}
return nil, true, fmt.Errorf("error getting next commit: %s", err)
}
if commit == nil {
t.iterExhausted = true
break
}
if t.alreadyVisited[commit.Hash.String()] {
continue
}
stats, err := commit.Stats()
if err != nil {
stats = nil
}
var parents []string
for _, parent := range commit.ParentHashes {
parents = append(parents, parent.String())
}
t.alreadyVisited[commit.Hash.String()] = true
for _, stat := range stats {
rows = append(rows, []interface{}{
commit.Hash.String(),
commit.Author.Name,
commit.Author.Email,
commit.Author.When.Format(time.RFC3339),
commit.Committer.Name,
commit.Committer.Email,
commit.Committer.When.Format(time.RFC3339),
commit.Message,
stat.Name,
stat.Addition,
stat.Deletion,
helper.Serialize(parents),
})
}
}
// Do a bit of cleanup if the iterator is exhausted
if t.iterExhausted {
t.iter.Close()
t.alreadyVisited = nil
t.repository = nil
}
return rows, t.iterExhausted, nil
}
// Create a new cursor that will be used to read rows
func (t *commit_diffTable) CreateReader() rpc.ReaderInterface {
return &commit_diffCursor{
alreadyVisited: make(map[string]bool),
}
}
// A destructor to clean up resources
func (t *commit_diffTable) Close() error {
return nil
}