-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
293 lines (259 loc) · 6.25 KB
/
local.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package gogh
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/apex/log"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/saracen/walker"
)
const DefaultRootDirName = "Projects"
func NewLocalController(root string) *LocalController {
return &LocalController{root: root}
}
type LocalController struct {
// UNDONE: support fs.FS
// UNDONE: support fs.FS
// NOTE: v1 -> v2 diferrence
// if we wanna manage multiple root, create multiple controller instances.
root string
}
type LocalExistOption struct {
}
func (l *LocalController) Exist(
ctx context.Context,
spec Spec,
opt *LocalExistOption,
) (bool, error) {
project := NewProject(l.root, spec)
_, err := git.PlainOpen(project.FullFilePath())
switch {
case err == nil:
return true, nil
case errors.Is(err, git.ErrRepositoryNotExists):
return false, nil
default:
return false, err
}
}
type LocalCreateOption struct { // UNDONE: support isBare
}
func (l *LocalController) Create(
ctx context.Context,
spec Spec,
opt *LocalCreateOption,
) (Project, error) {
p := NewProject(l.root, spec)
if err := CreateLocalProject(ctx, p, spec.URL(), opt); err != nil {
return Project{}, err
}
return p, nil
}
func CreateLocalProject(
_ context.Context,
project Project,
remoteURL string,
_ *LocalCreateOption,
) error {
repo, err := git.PlainInit(project.FullFilePath(), false)
if err != nil {
return err
}
if _, err := repo.CreateRemote(&config.RemoteConfig{
Name: git.DefaultRemoteName,
URLs: []string{remoteURL},
}); err != nil {
return err
}
return nil
}
func (l *LocalController) SetRemoteSpecs(
ctx context.Context,
spec Spec,
remotes map[string][]Spec,
) error {
urls := map[string][]string{}
for name, specs := range remotes {
for _, spec := range specs {
urls[name] = append(urls[name], spec.URL())
}
}
return l.SetRemoteURLs(ctx, spec, urls)
}
func (l *LocalController) SetRemoteURLs(
ctx context.Context,
spec Spec,
remotes map[string][]string,
) error {
return SetRemoteURLsOnLocalProject(ctx, NewProject(l.root, spec), remotes)
}
func SetRemoteURLsOnLocalProject(
_ context.Context,
project Project,
remotes map[string][]string,
) error {
repo, err := git.PlainOpen(project.FullFilePath())
if err != nil {
return err
}
cfg, err := repo.Config()
if err != nil {
return err
}
cfg.Remotes = map[string]*config.RemoteConfig{}
for name, urls := range remotes {
cfg.Remotes[name] = &config.RemoteConfig{
Name: name,
URLs: urls,
}
}
return repo.SetConfig(cfg)
}
func (l *LocalController) GetRemoteURLs(
ctx context.Context,
spec Spec,
name string,
) ([]string, error) {
return GetRemoteURLsFromLocalProject(ctx, NewProject(l.root, spec), name)
}
func GetRemoteURLsFromLocalProject(
_ context.Context,
project Project,
name string,
) ([]string, error) {
repo, err := git.PlainOpen(project.FullFilePath())
if err != nil {
return nil, fmt.Errorf("open local repository: %w", err)
}
remote, err := repo.Remote(name)
if err != nil {
return nil, fmt.Errorf("get remote %s: %w", name, err)
}
return remote.Config().URLs, nil
}
func GetDefaultRemoteURLFromLocalProject(_ context.Context, project Project) (string, error) {
urls, err := GetRemoteURLsFromLocalProject(context.Background(), project, git.DefaultRemoteName)
if err != nil {
return "", err
}
return urls[0], nil
}
type LocalCloneOption struct {
Alias *Spec
// UNDONE: support isBare
// UNDONE: support *git.CloneOptions
}
func (l *LocalController) Clone(
ctx context.Context,
spec Spec,
token string,
opt *LocalCloneOption,
) (Project, error) {
var auth transport.AuthMethod
if token != "" {
auth = &http.BasicAuth{
Username: spec.Owner(),
Password: token,
}
}
p := NewProject(l.root, spec)
path := p.FullFilePath()
url := spec.URL()
if opt != nil && opt.Alias != nil {
alias := NewProject(l.root, *opt.Alias)
alias.spec.host = p.spec.host
path = alias.FullFilePath()
p = alias
}
if _, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
URL: url,
Auth: auth,
}); err != nil {
return Project{}, err
}
return p, nil
}
type LocalWalkFunc func(Project) error
type LocalWalkOption struct {
Query string
}
var mu sync.Mutex
func (l *LocalController) Walk(
ctx context.Context,
opt *LocalWalkOption,
walkFn LocalWalkFunc,
) error {
if _, err := os.Lstat(l.root); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return walker.WalkWithContext(ctx, l.root, func(pathname string, _ os.FileInfo) (retErr error) {
rel, _ := filepath.Rel(l.root, pathname)
parts := strings.Split(rel, string(filepath.Separator))
if len(parts) < 3 {
return nil
}
defer func() {
if retErr == nil {
retErr = filepath.SkipDir
}
}()
if _, err := git.PlainOpen(pathname); err != nil {
log.FromContext(ctx).
WithFields(log.Fields{"error": err, "rel": rel}).
Debug("skip a dir that is not a git directory")
return nil
}
// NOTE: Case of len(parts) > 3 never happens because it returns filepath.SkipDir
spec, err := NewSpec(parts[0], parts[1], parts[2])
if err != nil {
log.FromContext(ctx).
WithFields(log.Fields{"error": err, "rel": rel}).
Debug("skip invalid entity")
return nil
}
p := NewProject(l.root, spec)
if opt != nil && !strings.Contains(p.RelPath(), opt.Query) {
return nil
}
mu.Lock()
defer mu.Unlock()
return walkFn(p)
})
}
type LocalListOption struct {
Query string
}
func (l *LocalController) List(ctx context.Context, opt *LocalListOption) ([]Project, error) {
var list []Project
var woption *LocalWalkOption
if opt != nil {
woption = &LocalWalkOption{Query: opt.Query}
}
if err := l.Walk(ctx, woption, func(p Project) error {
list = append(list, p)
return nil
}); err != nil {
return nil, err
}
return list, nil
}
type LocalDeleteOption struct{}
func (l *LocalController) Delete(ctx context.Context, spec Spec, opt *LocalDeleteOption) error {
return DeleteLocalProject(ctx, NewProject(l.root, spec), opt)
}
func DeleteLocalProject(_ context.Context, project Project, _ *LocalDeleteOption) error {
if err := project.CheckEntity(); err != nil {
return err
}
return os.RemoveAll(project.FullFilePath())
}