+47
-7
knotserver/git/git.go
+47
-7
knotserver/git/git.go
···
142
return &g, nil
143
}
144
145
-
func (g *GitRepo) Commits() ([]*object.Commit, error) {
146
-
ci, err := g.r.Log(&git.LogOptions{From: g.h})
147
if err != nil {
148
return nil, fmt.Errorf("commits from ref: %w", err)
149
}
150
151
-
commits := []*object.Commit{}
152
-
ci.ForEach(func(c *object.Commit) error {
153
-
commits = append(commits, c)
154
-
return nil
155
-
})
156
157
return commits, nil
158
}
159
160
func (g *GitRepo) Commit(h plumbing.Hash) (*object.Commit, error) {
···
142
return &g, nil
143
}
144
145
+
func (g *GitRepo) Commits(offset, limit int) ([]*object.Commit, error) {
146
+
commits := []*object.Commit{}
147
+
148
+
output, err := g.revList(
149
+
fmt.Sprintf("--skip=%d", offset),
150
+
fmt.Sprintf("--max-count=%d", limit),
151
+
)
152
if err != nil {
153
return nil, fmt.Errorf("commits from ref: %w", err)
154
}
155
156
+
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
157
+
if len(lines) == 1 && lines[0] == "" {
158
+
return commits, nil
159
+
}
160
+
161
+
for _, item := range lines {
162
+
obj, err := g.r.CommitObject(plumbing.NewHash(item))
163
+
if err != nil {
164
+
continue
165
+
}
166
+
commits = append(commits, obj)
167
+
}
168
169
return commits, nil
170
+
}
171
+
172
+
func (g *GitRepo) TotalCommits() (int, error) {
173
+
output, err := g.revList(
174
+
fmt.Sprintf("--count"),
175
+
)
176
+
if err != nil {
177
+
return 0, fmt.Errorf("failed to run rev-list", err)
178
+
}
179
+
180
+
count, err := strconv.Atoi(strings.TrimSpace(string(output)))
181
+
if err != nil {
182
+
return 0, err
183
+
}
184
+
185
+
return count, nil
186
+
}
187
+
188
+
func (g *GitRepo) revList(extraArgs ...string) ([]byte, error) {
189
+
var args []string
190
+
args = append(args, "rev-list")
191
+
args = append(args, g.h.String())
192
+
args = append(args, extraArgs...)
193
+
194
+
cmd := exec.Command("git", args...)
195
+
cmd.Dir = g.path
196
+
197
+
return cmd.Output()
198
}
199
200
func (g *GitRepo) Commit(h plumbing.Hash) (*object.Commit, error) {
+17
-22
knotserver/routes.go
+17
-22
knotserver/routes.go
···
87
}
88
}
89
90
-
commits, err := gr.Commits()
91
-
total := len(commits)
92
if err != nil {
93
writeError(w, err.Error(), http.StatusInternalServerError)
94
l.Error("fetching commits", "error", err.Error())
95
return
96
}
97
-
if len(commits) > 10 {
98
-
commits = commits[:10]
99
}
100
101
branches, err := gr.Branches()
···
349
return
350
}
351
352
-
commits, err := gr.Commits()
353
-
if err != nil {
354
-
writeError(w, err.Error(), http.StatusInternalServerError)
355
-
l.Error("fetching commits", "error", err.Error())
356
-
return
357
-
}
358
-
359
// Get page parameters
360
page := 1
361
pageSize := 30
···
372
}
373
}
374
375
-
// Calculate pagination
376
-
start := (page - 1) * pageSize
377
-
end := start + pageSize
378
-
total := len(commits)
379
380
-
if start >= total {
381
-
commits = []*object.Commit{}
382
-
} else {
383
-
if end > total {
384
-
end = total
385
-
}
386
-
commits = commits[start:end]
387
}
388
389
resp := types.RepoLogResponse{
390
Commits: commits,
···
87
}
88
}
89
90
+
commits, err := gr.Commits(0, 60) // a good preview of commits in this repo
91
if err != nil {
92
writeError(w, err.Error(), http.StatusInternalServerError)
93
l.Error("fetching commits", "error", err.Error())
94
return
95
}
96
+
97
+
total, err := gr.TotalCommits()
98
+
if err != nil {
99
+
writeError(w, err.Error(), http.StatusInternalServerError)
100
+
l.Error("fetching commits", "error", err.Error())
101
+
return
102
}
103
104
branches, err := gr.Branches()
···
352
return
353
}
354
355
// Get page parameters
356
page := 1
357
pageSize := 30
···
368
}
369
}
370
371
+
// convert to offset/limit
372
+
offset := (page - 1) * pageSize
373
+
limit := pageSize
374
375
+
commits, err := gr.Commits(offset, limit)
376
+
if err != nil {
377
+
writeError(w, err.Error(), http.StatusInternalServerError)
378
+
l.Error("fetching commits", "error", err.Error())
379
+
return
380
}
381
+
382
+
total := len(commits)
383
384
resp := types.RepoLogResponse{
385
Commits: commits,