/
get_commits_handler.go
142 lines (132 loc) · 3.04 KB
/
get_commits_handler.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
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
package apiserver
import (
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/wrgl/core/pkg/api"
"github.com/wrgl/core/pkg/api/payload"
"github.com/wrgl/core/pkg/objects"
"github.com/wrgl/core/pkg/ref"
)
func getQueryInt(query url.Values, key string, initial int) (res int, err error) {
res = initial
if v := query.Get(key); v != "" {
res, err = strconv.Atoi(v)
if err != nil {
return 0, fmt.Errorf("invalid " + key)
}
}
return
}
func CommitPayload(com *objects.Commit) *payload.Commit {
obj := &payload.Commit{
AuthorName: com.AuthorName,
AuthorEmail: com.AuthorEmail,
Message: com.Message,
Table: payload.BytesToHex(com.Table),
Time: com.Time,
}
for _, sum := range com.Parents {
obj.Parents = append(obj.Parents, payload.BytesToHex(sum))
}
return obj
}
func getCommitTree(db objects.Store, sum []byte, minDepth, maxDepth int) (map[string]*payload.Commit, error) {
if maxDepth < minDepth {
return nil, nil
}
current := [][]byte{sum}
for i := 0; i < minDepth; i++ {
next := [][]byte{}
for _, sum := range current {
com, err := objects.GetCommit(db, sum)
if err != nil {
return nil, err
}
next = append(next, com.Parents...)
}
if len(next) == 0 {
return nil, nil
}
current = next
}
result := map[string]*payload.Commit{}
commits := []*payload.Commit{}
for _, sum := range current {
com, err := objects.GetCommit(db, sum)
if err != nil {
return nil, err
}
obj := CommitPayload(com)
commits = append(commits, obj)
result[hex.EncodeToString(sum)] = obj
}
for i := minDepth + 1; i <= maxDepth; i++ {
next := []*payload.Commit{}
for _, com := range commits {
for _, sum := range com.Parents {
parent, err := objects.GetCommit(db, (*sum)[:])
if err != nil {
return nil, err
}
obj := CommitPayload(parent)
if com.ParentCommits == nil {
com.ParentCommits = map[string]*payload.Commit{}
}
com.ParentCommits[hex.EncodeToString((*sum)[:])] = obj
next = append(next, obj)
}
}
if len(next) == 0 {
break
}
commits = next
}
return result, nil
}
func (s *Server) handleGetCommits(rw http.ResponseWriter, r *http.Request) {
var sum []byte
var err error
rs := s.getRS(r)
query := r.URL.Query()
if s := query.Get("ref"); s == "" {
http.Error(rw, "missing ref query param", http.StatusBadRequest)
return
} else {
sum, err = ref.GetRef(rs, s)
if err != nil {
http.NotFound(rw, r)
return
}
}
minDepth, err := getQueryInt(query, "minDepth", 0)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
maxDepth, err := getQueryInt(query, "maxDepth", 0)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
db := s.getDB(r)
commits, err := getCommitTree(db, sum, minDepth, maxDepth)
if err != nil {
panic(err)
}
resp := &payload.GetCommitsResponse{
Commits: commits,
}
b, err := json.Marshal(resp)
if err != nil {
panic(err)
}
rw.Header().Set("Content-Type", api.CTJSON)
_, err = rw.Write(b)
if err != nil {
panic(err)
}
}