-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
354 lines (294 loc) · 7.96 KB
/
git.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package db
import (
"fmt"
"io"
"log/slog"
"os"
"path"
"strings"
"sync"
"time"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/filesystem"
"go.starlark.net/starlark"
)
var gitMutex sync.Mutex
type gitTreeIterator struct {
tree *object.Tree
name string
ents []object.TreeEntry
index int
}
// Done implements starlark.Iterator.
func (g *gitTreeIterator) Done() {
g.index = len(g.ents)
}
// Next implements starlark.Iterator.
func (g *gitTreeIterator) Next(p *starlark.Value) bool {
if g.index == len(g.ents) {
return false
}
node := g.ents[g.index]
if node.Mode.IsFile() {
// assume a file.
file, err := g.tree.File(node.Name)
if err != nil {
*p = starlark.None
return false
}
*p = NewFile(nil, path.Join(g.name, node.Name), func() (io.ReadCloser, error) {
return file.Reader()
}, nil)
} else {
// assume a directory.
tree, err := g.tree.Tree(node.Name)
if err != nil {
*p = starlark.None
return false
}
*p = &GitTree{tree: tree, name: path.Join(g.name, node.Name)}
}
g.index += 1
return g.index != len(g.ents)
}
var (
_ starlark.Iterator = &gitTreeIterator{}
)
type GitTree struct {
tree *object.Tree
name string
}
// Attr implements starlark.HasAttrs.
func (g *GitTree) Attr(name string) (starlark.Value, error) {
if name == "name" {
return starlark.String(g.name), nil
} else if name == "base" {
return starlark.String(path.Base(g.name)), nil
} else {
return nil, nil
}
}
// AttrNames implements starlark.HasAttrs.
func (g *GitTree) AttrNames() []string {
return []string{"name", "base"}
}
// Iterate implements starlark.Iterable.
func (g *GitTree) Iterate() starlark.Iterator {
return &gitTreeIterator{tree: g.tree, name: g.name, ents: g.tree.Entries}
}
// Get implements starlark.Mapping.
func (g *GitTree) Get(k starlark.Value) (v starlark.Value, found bool, err error) {
name, _ := starlark.AsString(k)
f, err := g.tree.File(name)
if err == object.ErrFileNotFound {
child, err := g.tree.Tree(name)
if err == object.ErrDirectoryNotFound {
return starlark.None, false, nil
} else if err != nil {
return starlark.None, false, err
}
return &GitTree{tree: child, name: path.Join(g.name, name)}, true, nil
} else if err != nil {
return starlark.None, false, err
}
return NewFile(nil, path.Join(g.name, name), func() (io.ReadCloser, error) {
return f.Reader()
}, nil), true, nil
}
func (t *GitTree) String() string { return fmt.Sprintf("GitTree{%s}", t.name) }
func (*GitTree) Type() string { return "GitTree" }
func (*GitTree) Hash() (uint32, error) {
return 0, fmt.Errorf("GitTree is not hashable")
}
func (*GitTree) Truth() starlark.Bool { return starlark.True }
func (*GitTree) Freeze() {}
var (
_ starlark.Value = &GitTree{}
_ starlark.HasAttrs = &GitTree{}
_ starlark.Mapping = &GitTree{}
_ starlark.Iterable = &GitTree{}
)
type GitRepository struct {
repo *git.Repository
}
// Attr implements starlark.HasAttrs.
func (g *GitRepository) Attr(name string) (starlark.Value, error) {
if name == "tag" {
return starlark.NewBuiltin("GitRepository.tag", func(
thread *starlark.Thread,
fn *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
tag string
)
if err := starlark.UnpackArgs("GitRepository.tag", args, kwargs,
"tag", &tag,
); err != nil {
return starlark.None, err
}
obj, err := g.repo.Tag(tag)
if err != nil {
return starlark.None, err
}
tagObj, err := g.repo.TagObject(obj.Hash())
if err != nil {
return starlark.None, err
}
commit, err := tagObj.Commit()
if err != nil {
return starlark.None, err
}
tree, err := commit.Tree()
if err != nil {
return starlark.None, err
}
return &GitTree{tree: tree}, nil
}), nil
} else if name == "branch" {
return starlark.NewBuiltin("GitRepository.branch", func(
thread *starlark.Thread,
fn *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
branch string
)
if err := starlark.UnpackArgs("GitRepository.branch", args, kwargs,
"branch", &branch,
); err != nil {
return starlark.None, err
}
var ref *plumbing.Reference
obj, err := g.repo.Branch(branch)
if err != nil {
if err.Error() == "branch not found" {
// Check to see if the branch exists on the origin and fetch it.
refs, err := g.repo.Storer.IterReferences()
if err != nil {
return nil, err
}
err = refs.ForEach(func(r *plumbing.Reference) error {
if strings.HasSuffix(r.Name().String(), branch) {
ref = r
}
return nil
})
if err != nil {
return nil, err
}
} else {
return starlark.None, err
}
} else {
ref, err = g.repo.Reference(obj.Merge, true)
if err != nil {
return starlark.None, err
}
}
if ref == nil {
return starlark.None, fmt.Errorf("failed to find reference for: %s", branch)
}
commit, err := g.repo.CommitObject(ref.Hash())
if err != nil {
return starlark.None, err
}
tree, err := commit.Tree()
if err != nil {
return starlark.None, err
}
return &GitTree{tree: tree}, nil
}), nil
} else {
return nil, nil
}
}
// AttrNames implements starlark.HasAttrs.
func (g *GitRepository) AttrNames() []string {
return []string{"tag", "branch"}
}
// Get implements starlark.Mapping.
func (g *GitRepository) Get(k starlark.Value) (v starlark.Value, found bool, err error) {
commit, _ := starlark.AsString(k)
obj, err := g.repo.CommitObject(plumbing.NewHash(commit))
if err == plumbing.ErrObjectNotFound {
return starlark.None, false, nil
} else if err != nil {
return starlark.None, false, err
}
tree, err := obj.Tree()
if err != nil {
return starlark.None, false, err
}
return &GitTree{tree: tree}, true, nil
}
func (*GitRepository) String() string { return "GitRepository" }
func (*GitRepository) Type() string { return "GitRepository" }
func (*GitRepository) Hash() (uint32, error) {
return 0, fmt.Errorf("GitRepository is not hashable")
}
func (*GitRepository) Truth() starlark.Bool { return starlark.True }
func (*GitRepository) Freeze() {}
var (
_ starlark.Value = &GitRepository{}
_ starlark.Mapping = &GitRepository{}
_ starlark.HasAttrs = &GitRepository{}
)
func (db *PackageDatabase) fetchGit(url string) (*GitRepository, error) {
cachePath, err := db.Eif.GetCachePath(url)
if err != nil {
return nil, err
}
info, err := os.Stat(cachePath)
if err == nil {
modTime := info.ModTime()
if time.Since(modTime) < 8*time.Hour {
store := osfs.New(cachePath)
s := filesystem.NewStorage(store, cache.NewObjectLRUDefault())
repo, err := git.Open(s, nil)
if err != nil {
return nil, fmt.Errorf("failed to open: %s", err)
}
return &GitRepository{repo: repo}, nil
}
}
gitMutex.Lock()
defer gitMutex.Unlock()
slog.Info("downloading with git", "url", url)
repo, err := git.PlainClone(cachePath, true, &git.CloneOptions{
URL: url,
Progress: os.Stdout,
})
if err == git.ErrRepositoryAlreadyExists {
store := osfs.New(cachePath)
s := filesystem.NewStorage(store, cache.NewObjectLRUDefault())
repo, err = git.Open(s, nil)
if err != nil {
return nil, fmt.Errorf("failed to open: %s", err)
}
err = repo.Fetch(&git.FetchOptions{
RemoteURL: url,
Progress: os.Stdout,
Tags: git.AllTags,
})
if err == git.NoErrAlreadyUpToDate {
// fallthrough
} else if err != nil {
slog.Warn("could not update git repo", "url", url, "error", err)
// Return anyway since the repository already exists.
return &GitRepository{repo: repo}, nil
}
} else if err != nil {
return nil, fmt.Errorf("failed to clone: %s", err)
}
if err := os.Chtimes(cachePath, time.Now(), time.Now()); err != nil {
return nil, err
}
return &GitRepository{repo: repo}, nil
}