-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathrepository_pool.go
303 lines (243 loc) · 6.98 KB
/
repository_pool.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
package gitbase
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"sync/atomic"
"gopkg.in/src-d/go-billy-siva.v4"
billy "gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-billy.v4/osfs"
errors "gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
)
var (
errRepoAlreadyRegistered = errors.NewKind("the repository is already registered: %s")
gitStorerOptions = filesystem.Options{
ExclusiveAccess: true,
KeepDescriptors: true,
}
)
// Repository struct holds an initialized repository and its ID
type Repository struct {
*git.Repository
ID string
// function used to close underlying file descriptors
closeFunc func()
}
// NewRepository creates and initializes a new Repository structure
func NewRepository(id string, repo *git.Repository, closeFunc func()) *Repository {
return &Repository{
Repository: repo,
ID: id,
closeFunc: closeFunc,
}
}
// Close closes all opened files in the repository.
func (r *Repository) Close() {
if r != nil {
if r.Repository != nil {
f, ok := r.Storer.(*filesystem.Storage)
if ok {
// The only type of error returned is "file already closed" and
// we don't want to do anything with it.
f.Close()
}
}
if r.closeFunc != nil {
r.closeFunc()
}
}
}
// NewRepositoryFromPath creates and initializes a new Repository structure
// and initializes a go-git repository
func NewRepositoryFromPath(id, path string, cache cache.Object) (*Repository, error) {
var wt billy.Filesystem
fs := osfs.New(path)
f, err := fs.Stat(git.GitDirName)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if f != nil && f.IsDir() {
wt = fs
fs, err = fs.Chroot(git.GitDirName)
if err != nil {
return nil, err
}
}
sto := filesystem.NewStorageWithOptions(fs, cache, gitStorerOptions)
repo, err := git.Open(sto, wt)
if err != nil {
return nil, err
}
return NewRepository(id, repo, nil), nil
}
// NewSivaRepositoryFromPath creates and initializes a new Repository structure
// and initializes a go-git repository backed by a siva file.
func NewSivaRepositoryFromPath(id, path string, cache cache.Object) (*Repository, error) {
localfs := osfs.New(filepath.Dir(path))
tmpDir, err := ioutil.TempDir(os.TempDir(), "gitbase-siva")
if err != nil {
return nil, err
}
tmpfs := osfs.New(tmpDir)
fs, err := sivafs.NewFilesystem(localfs, filepath.Base(path), tmpfs)
if err != nil {
return nil, err
}
sto := filesystem.NewStorageWithOptions(fs, cache, gitStorerOptions)
repo, err := git.Open(sto, nil)
if err != nil {
return nil, err
}
closeFunc := func() { fs.Sync() }
return NewRepository(id, repo, closeFunc), nil
}
type repository interface {
ID() string
Repo() (*Repository, error)
FS() (billy.Filesystem, error)
Path() string
Cache() cache.Object
}
type gitRepository struct {
id string
path string
cache cache.Object
}
func gitRepo(id, path string, cache cache.Object) repository {
return &gitRepository{id, path, cache}
}
func (r *gitRepository) ID() string {
return r.id
}
func (r *gitRepository) Repo() (*Repository, error) {
return NewRepositoryFromPath(r.id, r.path, r.cache)
}
func (r *gitRepository) FS() (billy.Filesystem, error) {
return osfs.New(r.path), nil
}
func (r *gitRepository) Path() string {
return r.path
}
func (r *gitRepository) Cache() cache.Object {
return r.cache
}
type sivaRepository struct {
id string
path string
cache cache.Object
}
func sivaRepo(id, path string, cache cache.Object) repository {
return &sivaRepository{id, path, cache}
}
func (r *sivaRepository) ID() string {
return r.id
}
func (r *sivaRepository) Repo() (*Repository, error) {
return NewSivaRepositoryFromPath(r.id, r.path, r.cache)
}
func (r *sivaRepository) FS() (billy.Filesystem, error) {
localfs := osfs.New(filepath.Dir(r.path))
tmpDir, err := ioutil.TempDir(os.TempDir(), "gitbase-siva")
if err != nil {
return nil, err
}
tmpfs := osfs.New(tmpDir)
return sivafs.NewFilesystem(localfs, filepath.Base(r.path), tmpfs)
}
func (r *sivaRepository) Path() string {
return r.path
}
func (r *sivaRepository) Cache() cache.Object {
return r.cache
}
// RepositoryPool holds a pool git repository paths and
// functionality to open and iterate them.
type RepositoryPool struct {
repositories map[string]repository
idOrder []string
cache cache.Object
}
// NewRepositoryPool initializes a new RepositoryPool with LRU cache.
func NewRepositoryPool(maxCacheSize cache.FileSize) *RepositoryPool {
return &RepositoryPool{
repositories: make(map[string]repository),
cache: cache.NewObjectLRU(maxCacheSize),
}
}
// Add inserts a new repository in the pool.
func (p *RepositoryPool) Add(repo repository) error {
id := repo.ID()
if r, ok := p.repositories[id]; ok {
return errRepoAlreadyRegistered.New(r.Path())
}
p.idOrder = append(p.idOrder, id)
p.repositories[id] = repo
return nil
}
// AddGit adds a git repository to the pool. It also sets its path as ID.
func (p *RepositoryPool) AddGit(path string) error {
return p.AddGitWithID(path, path)
}
// AddGitWithID adds a git repository to the pool. ID should be specified.
func (p *RepositoryPool) AddGitWithID(id, path string) error {
return p.Add(gitRepo(id, path, p.cache))
}
// AddSivaFile adds a siva file to the pool. It also sets its path as ID.
func (p *RepositoryPool) AddSivaFile(path string) error {
return p.Add(sivaRepo(path, path, p.cache))
}
// AddSivaFileWithID adds a siva file to the pool. ID should be specified.
func (p *RepositoryPool) AddSivaFileWithID(id, path string) error {
return p.Add(sivaRepo(id, path, p.cache))
}
// GetPos retrieves a repository at a given position. If the position is
// out of bounds it returns io.EOF.
func (p *RepositoryPool) GetPos(pos int) (*Repository, error) {
if pos >= len(p.repositories) {
return nil, io.EOF
}
id := p.idOrder[pos]
if id == "" {
return nil, io.EOF
}
return p.GetRepo(id)
}
// ErrPoolRepoNotFound is returned when a repository id is not present in the pool.
var ErrPoolRepoNotFound = errors.NewKind("repository id %s not found in the pool")
// GetRepo returns a repository with the given id from the pool.
func (p *RepositoryPool) GetRepo(id string) (*Repository, error) {
r, ok := p.repositories[id]
if !ok {
return nil, ErrPoolRepoNotFound.New(id)
}
return r.Repo()
}
// RepoIter creates a new Repository iterator
func (p *RepositoryPool) RepoIter() (*RepositoryIter, error) {
iter := &RepositoryIter{
pool: p,
}
atomic.StoreInt32(&iter.pos, 0)
return iter, nil
}
// RepositoryIter iterates over all repositories in the pool
type RepositoryIter struct {
pos int32
pool *RepositoryPool
}
// Next retrieves the next Repository. It returns io.EOF as error
// when there are no more Repositories to retrieve.
func (i *RepositoryIter) Next() (*Repository, error) {
pos := int(atomic.LoadInt32(&i.pos))
r, err := i.pool.GetPos(pos)
atomic.AddInt32(&i.pos, 1)
return r, err
}
// Close finished iterator. It's no-op.
func (i *RepositoryIter) Close() error {
return nil
}