mirrored from https://chromium.googlesource.com/infra/luci/luci-go
/
gitiles_fake.go
197 lines (180 loc) · 5.43 KB
/
gitiles_fake.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
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
// Copyright 2020 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gitiles
import (
context "context"
"fmt"
"go.chromium.org/luci/common/errors"
git "go.chromium.org/luci/common/proto/git"
grpc "google.golang.org/grpc"
)
const defaultPageSize = 100
// key: ref name, value: list of commits
type fakeRepository struct {
refs map[string]string
commits map[string]*git.Commit
}
// Fake allows testing of Gitiles API without using actual Gitiles
// server. User can set data using SetRepository method.
type Fake struct {
// key: repository name, value fakeRepository
m map[string]fakeRepository
callLogs []interface{}
}
// Log retrieves commit log. Merge commits are supported, but it implements
// simple logic and likely won't return results in the same order as Gitiles.
func (f *Fake) Log(ctx context.Context, in *LogRequest, opts ...grpc.CallOption) (*LogResponse, error) {
f.addCallLog(in)
repository, ok := f.m[in.GetProject()]
if !ok {
return nil, errors.New("Repository not found")
}
committish := in.GetCommittish()
// Check if committish is a ref
if commitID, ok := repository.refs[committish]; ok {
committish = commitID
}
commit, ok := repository.commits[committish]
if !ok {
return nil, fmt.Errorf("Commit %s not found", committish)
}
size := int(in.GetPageSize())
if size == 0 {
size = defaultPageSize
}
resp := &LogResponse{
Log: []*git.Commit{},
}
startAdding := in.GetPageToken() == ""
q := []*git.Commit{commit}
visited := map[string]struct{}{}
for size > len(resp.Log) && len(q) > 0 {
commit = q[0]
q = q[1:]
if _, ok := visited[commit.GetId()]; ok {
continue
}
visited[commit.GetId()] = struct{}{}
if startAdding {
resp.Log = append(resp.Log, commit)
} else if commit.GetId() == in.GetPageToken() {
startAdding = true
}
for _, commitID := range commit.GetParents() {
if in.GetExcludeAncestorsOf() == commitID {
break
}
c, ok := repository.commits[commitID]
if !ok {
panic(fmt.Sprintf(
"Broken git chain, commit %s has parent %s which doesn't exist",
commit.GetId(), commitID))
}
q = append(q, c)
}
}
if len(resp.Log) == size {
resp.NextPageToken = commit.GetId()
}
return resp, nil
}
// Refs retrieves repo refs.
func (f *Fake) Refs(ctx context.Context, in *RefsRequest, opts ...grpc.CallOption) (*RefsResponse, error) {
f.addCallLog(in)
p, ok := f.m[in.GetProject()]
if !ok {
return nil, errors.New("Repository not found")
}
resp := &RefsResponse{
Revisions: p.refs,
}
return resp, nil
}
// Archive retrieves archived contents of the project. This is not implemented.
//
// An archive is a shallow bundle of the contents of a repository.
//
// DEPRECATED: Use DownloadFile to obtain plain text files.
// TODO(pprabhu): Migrate known users to DownloadFile and delete this RPC.
func (f *Fake) Archive(ctx context.Context, in *ArchiveRequest, opts ...grpc.CallOption) (*ArchiveResponse, error) {
f.addCallLog(in)
panic("not implemented")
}
// DownloadFile retrieves a file from the project. This is not implemented.
func (f *Fake) DownloadFile(ctx context.Context, in *DownloadFileRequest, opts ...grpc.CallOption) (*DownloadFileResponse, error) {
f.addCallLog(in)
panic("not implemented")
}
// Projects retrieves list of available Gitiles projects
func (f *Fake) Projects(ctx context.Context, in *ProjectsRequest, opts ...grpc.CallOption) (*ProjectsResponse, error) {
f.addCallLog(in)
resp := &ProjectsResponse{
Projects: make([]string, len(f.m)),
}
i := 0
for projectName := range f.m {
resp.Projects[i] = projectName
i++
}
return resp, nil
}
// SetRepository stores provided references and commits to desired repository.
// If repository is previously set, it will override it.
//
// refs keys are references, keys are revisions.
// Example:
// f.SetRepository(
// "foo",
// []string{"refs/heads/master", "rev1"},
// []*git.Commit{ {Id: "rev1", Parents: []string{"rev0"}}, {Id: "rev0"} }
// )
// Represents following repository:
// name: foo
// references:
// * refs/heads/master points to rev1
// commits:
// rev1 --> rev0 (root commit)
func (f *Fake) SetRepository(repository string, refs map[string]string, commits []*git.Commit) {
if f.m == nil {
f.m = map[string]fakeRepository{}
}
commitMap := make(map[string]*git.Commit, len(commits))
for _, commit := range commits {
if _, ok := commitMap[commit.GetId()]; ok {
panic(fmt.Sprintf("Duplicated commit with commit hash: %s", commit.GetId()))
}
commitMap[commit.GetId()] = commit
}
// Sanity check
for refs, rev := range refs {
if rev == "" {
// empty repository
continue
}
if _, ok := commitMap[rev]; !ok {
panic(fmt.Sprintf("Ref %s points to invalid revision %s", refs, rev))
}
}
f.m[repository] = fakeRepository{
refs: refs,
commits: commitMap,
}
}
// GetCallLogs returns callLogs.
func (f *Fake) GetCallLogs() []interface{} {
return f.callLogs
}
func (f *Fake) addCallLog(in interface{}) {
f.callLogs = append(f.callLogs, in)
}