/
repotest.go
228 lines (197 loc) · 5.57 KB
/
repotest.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// repotest implements a fake implementation of api.VersionControlSystem.
package repotest
import (
"context"
"fmt"
"maps"
"path/filepath"
"strings"
"github.com/JeffFaer/tmux-vcs-sync/api"
)
const DefaultWorkUnitName = "root"
// NewVCS creates a new, fake VersionControlSystem that requires all
// repositories to be in the given directory.
func NewVCS(dir string, repos ...RepoConfig) api.VersionControlSystem {
vcs := &fakeVCS{dir: dir, repos: make(map[string]*fakeRepo)}
ctx := context.Background()
seen := make(map[string]bool)
for _, cfg := range repos {
if seen[cfg.Name] {
panic(fmt.Errorf("repo %q configured multiple times", cfg.Name))
}
seen[cfg.Name] = true
repo, err := vcs.Repository(ctx, filepath.Join(dir, cfg.Name))
if err != nil {
panic(err)
}
if err := seedRepo(ctx, repo, cfg.WorkUnits); err != nil {
panic(err)
}
}
return vcs
}
type RepoConfig struct {
// Name is the name of the repo.
Name string
// WorkUnits is a map of work units keyed by parent work unit.
// You must have a DefaultWorkUnitName entry so we know where to start making
// work units from.
WorkUnits map[string][]string
}
func seedRepo(ctx context.Context, repo api.Repository, workUnits map[string][]string) error {
created, err := repo.List(ctx, "")
if err != nil {
return fmt.Errorf("could not list already created work units: %w", err)
}
workUnits = maps.Clone(workUnits)
for len(created) > 0 {
n := created[len(created)-1]
created = created[:len(created)-1]
for _, wu := range workUnits[n] {
if err := repo.Update(ctx, n); err != nil {
return fmt.Errorf("could not update repo to parent %q: %w", n, err)
}
if err := repo.Commit(ctx, wu); err != nil {
return fmt.Errorf("could not commit %q: %w", wu, err)
}
created = append(created, wu)
}
delete(workUnits, n)
}
if len(workUnits) > 0 {
return fmt.Errorf("unable to create all work units: %#v", workUnits)
}
return nil
}
type fakeVCS struct {
dir string
repos map[string]*fakeRepo
}
func (vcs *fakeVCS) Name() string { return fmt.Sprintf("fake(%s)", vcs.dir) }
func (*fakeVCS) WorkUnitName() string { return "work unit" }
func (vcs *fakeVCS) Repository(_ context.Context, dir string) (api.Repository, error) {
if !strings.HasPrefix(dir, vcs.dir) {
return nil, nil
}
if vcs.repos[dir] != nil {
return vcs.repos[dir], nil
}
repo := &fakeRepo{
vcs: vcs,
name: filepath.Base(dir),
dir: dir,
cur: DefaultWorkUnitName,
workUnits: map[string]string{DefaultWorkUnitName: ""},
children: map[string]map[string]bool{DefaultWorkUnitName: make(map[string]bool)},
}
vcs.repos[dir] = repo
return repo, nil
}
type fakeRepo struct {
vcs api.VersionControlSystem
name, dir string
cur string
workUnits map[string]string
children map[string]map[string]bool
}
func (repo *fakeRepo) VCS() api.VersionControlSystem {
return repo.vcs
}
func (repo *fakeRepo) Name() string {
return repo.name
}
func (repo *fakeRepo) RootDir() string {
return repo.dir
}
func (repo *fakeRepo) Current(context.Context) (string, error) {
return repo.cur, nil
}
func (repo *fakeRepo) List(_ context.Context, prefix string) ([]string, error) {
var ret []string
for n := range repo.workUnits {
if strings.HasPrefix(n, prefix) {
ret = append(ret, n)
}
}
return ret, nil
}
func (repo *fakeRepo) Sort(_ context.Context, workUnits []string) error {
allowed := make(map[string]bool)
for _, wu := range workUnits {
if _, ok := repo.workUnits[wu]; !ok {
return fmt.Errorf("work unit %q does not exist", wu)
}
allowed[wu] = true
}
var topo []string
s := map[string]bool{DefaultWorkUnitName: true}
for len(s) > 0 {
var n string
for m := range s {
if n == "" {
n = m
} else {
n = min(n, m)
}
}
delete(s, n)
topo = append(topo, n)
for child := range repo.children[n] {
s[child] = true
}
}
var i int
for _, n := range topo {
if allowed[n] {
workUnits[i] = n
i++
}
}
return nil
}
func (repo *fakeRepo) New(_ context.Context, workUnitName string) error {
return repo.commit(workUnitName, DefaultWorkUnitName)
}
func (repo *fakeRepo) Commit(_ context.Context, workUnitName string) error {
return repo.commit(workUnitName, repo.cur)
}
func (repo *fakeRepo) commit(workUnitName, parent string) error {
if _, ok := repo.workUnits[workUnitName]; ok {
return fmt.Errorf("work unit %q already exists", workUnitName)
}
if _, ok := repo.workUnits[parent]; !ok {
// Maybe someone renamed root?
return fmt.Errorf("parent work unit %q does not exist", parent)
}
repo.workUnits[workUnitName] = parent
repo.children[workUnitName] = make(map[string]bool)
repo.children[parent][workUnitName] = true
repo.cur = workUnitName
return nil
}
func (repo *fakeRepo) Rename(_ context.Context, workUnitName string) error {
if _, ok := repo.workUnits[workUnitName]; ok {
return fmt.Errorf("work unit %q already exists", workUnitName)
}
parent := repo.workUnits[repo.cur]
children := repo.children[repo.cur]
delete(repo.workUnits, repo.cur)
delete(repo.children, repo.cur)
delete(repo.children[parent], repo.cur)
repo.workUnits[workUnitName] = parent
repo.children[workUnitName] = children
repo.children[parent][workUnitName] = true
repo.cur = workUnitName
return nil
}
func (repo *fakeRepo) Exists(_ context.Context, workUnitName string) (bool, error) {
_, ok := repo.workUnits[workUnitName]
return ok, nil
}
func (repo *fakeRepo) Update(_ context.Context, workUnitName string) error {
if _, ok := repo.workUnits[workUnitName]; !ok {
return fmt.Errorf("work unit %q does not exist", workUnitName)
}
repo.cur = workUnitName
return nil
}